General Tree
How can I transform my number output into letters by implementing general tree
A
/\
B C D E F G
/ \ \ \
[H] [IJ] [KLM] [N]
/\
[PQ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int key;
vector<Node *>child;
};
Node *newNode(int key)
{
Node *temp = new Node;
temp->key = key;
return temp;
}
void LevelOrderTraversal(Node * root)
{
if (root==NULL)
return;
queue<Node *> q;
q.push(root);
while (!q.empty())
{
int n = q.size();
while (n > 0)
{
Node * p = q.front();
q.pop();
cout << p->key << " ";
for (int i=0; i<p->child.size(); i++)
q.push(p->child[i]);
n--;
}
cout << endl;
}
}
int main()
{
Node *root = newNode('A');
(root->child).push_back(newNode('B'));
(root->child).push_back(newNode('C'));
(root->child).push_back(newNode('D'));
(root->child).push_back(newNode('E'));
(root->child).push_back(newNode('F'));
(root->child).push_back(newNode('G'));
(root->child[0]->child).push_back(newNode('H'));
(root->child[0]->child).push_back(newNode('I'));
(root->child[2]->child).push_back(newNode('J'));
(root->child[3]->child).push_back(newNode('K'));
(root->child[3]->child).push_back(newNode('L'));
(root->child[3]->child).push_back(newNode('M'));
(root->child[3]->child).push_back(newNode('N'));
LevelOrderTraversal(root);
return 0;
}
|
Last edited on
A
/\
B C D E F G
/ \ \ \
[H] [IJ][KLM][N]
/\
[PQ]
|
Last edited on
Topic archived. No new replies allowed.