-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_order_print_BST.cpp
60 lines (52 loc) · 1.04 KB
/
level_order_print_BST.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
58
59
60
#include <iostream>
#include<queue>
using namespace std;
struct node{
int data ;
node * left ;
node *right ;
};
node * add_at_empty(int d ){
node * root = new node ;
root ->data = d ;
root ->right = NULL ;
root ->left = NULL ;
return root ;
}
node * add_in_order(int d , node *root ){
if(root == NULL){
root =add_at_empty(d) ;
}
else if (d<root ->data){
root ->left = add_in_order(d,root->left) ;
}
else {
root ->right = add_in_order(d,root->right ) ;
}
return root ;
}
void levelorder(node * root ){
if(root ==NULL){
return ;
}
queue<node*>q ;
q.push(root) ;
while(!q.empty()){
node * current = q.front() ;
cout<<current->data<<" ";
if(current->left !=NULL) q.push(current->left ) ;
if(current->right !=NULL) q.push(current->right ) ;
q.pop() ;
}
}
int main()
{
node * root = NULL ;
root =add_in_order(1,root ) ;
root =add_in_order(5,root ) ;
root =add_in_order(77,root ) ;
root =add_in_order(-9,root ) ;
root =add_in_order(66,root ) ;
levelorder(root) ;
return 0;
}