-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab_DS_variable.cpp
57 lines (49 loc) · 1022 Bytes
/
lab_DS_variable.cpp
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
#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(10);
srand(time(nullptr));
for(int i = 0; i < 4; i++)
(root->child).push_back(newNode(rand() % 10));
for(int i = 0; i < 2; i++)
(root->child[0]->child).push_back(newNode(rand() % 10));
(root->child[2]->child).push_back(newNode(1));
for(int i = 0; i < 3; i++)
(root->child[3]->child).push_back(newNode(rand() % 10));
cout << "Level order traversal\n";
LevelOrderTraversal(root);
return 0;
}