forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level_Order_Traversal_in_BinaryTree.cpp
88 lines (78 loc) · 1.78 KB
/
Level_Order_Traversal_in_BinaryTree.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
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
// This program is to create a Binary Tree in C++ and then
// do Level Order traversal of the Binary tree
// Code by Nishit Anand
#include<iostream>
#include<queue>
using namespace std;
// Defining structure of elements of binary tree
struct node
{
int data;
struct node *left;
struct node *right;
};
// Function to create a new node of the binary tree
struct node* create_node(int key)
{
struct node* newnode= new node;
newnode->data=key;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
// Function to insert an node in the binary tree
void insert_node(struct node*& root,struct node* node)
{
char a;
if(root==NULL)
{
root=node;
cout<<"node Inserted\n";
}
else
{
cout<<"ENTER L OR R to insert in left or right of : "<<root->data<<" respectively : ";
cin>>a;
if(a=='L')
insert_node(root->left,node);
if(a=='R')
insert_node(root->right,node);
}
}
// Function for Level Order Traversal of Binary Tree
// I have used queue from C++ STL
void levelorder(struct node* root)
{
struct node* curr=NULL;
queue<struct node*> q;
q.push(root);
while(!q.empty())
{
curr=q.front();
q.pop();
cout<<" "<<curr->data;
if(curr->left)
q.push(curr->left);
if(curr->right)
q.push(curr->right);
}
}
// Main function
int main()
{
struct node* root=NULL;
int value;
char b='y';
do
{
cout<<"\nEnter value to be inserted into Binary Tree:";
cin>>value;
insert_node(root,create_node(value));
cout<<"\nWANT TO ENTER MORE(y/n):";
cin>>b;
}
while(b=='y');
cout<<"LEVEL ORDER TRAVERSAL"<<endl;
levelorder(root);
return 0;
}