Skip to content

Commit ada6ce4

Browse files
committedFeb 7, 2018
implemented level order traversal in binary tree
1 parent 3e4250f commit ada6ce4

File tree

3 files changed

+80
-46
lines changed

3 files changed

+80
-46
lines changed
 

‎Data Structures/Queues/QueueUsing2Stacks.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void Queue::Dequeue() {
4141
}
4242

4343
//will enter this condition only at the first call to Dequeue() after inserting some data in queue
44-
//is stack 2 is empty , only then we transfer all items from stack1 to stack2
44+
//if stack 2 is empty , only then we transfer all items from stack1 to stack2
4545
if(s2.empty()){
4646

4747
//transfering from stack1 to stack2

‎Data Structures/Trees/Binary Trees/BinrayTree.cpp

+38-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include<iostream>
22
#include<stack>
3+
#include<queue>
34

45
//IMPLEMENTATION OF A BINARY TREE- a tree which can have atmost i.e <= 2 child nodes
56

@@ -138,18 +139,36 @@ void PostOrderRec(struct BinaryTreeNode *root)
138139

139140

140141

141-
142-
//void levelOrder(struct BinaryTreeNode *root)
143-
//{
144-
//
145-
//
146-
// if(root)
147-
// {
148-
// cout<<root->data;
149-
// levelOrder(root,root->left,root->right);
150-
// }
151-
//}
152-
142+
//In level order we will use a queue to store the level+1 nodes in queue
143+
void levelOrder(struct BinaryTreeNode *root)
144+
{
145+
struct BinaryTreeNode *temp;
146+
queue<BinaryTreeNode *>q;
147+
148+
if(!root) return;
149+
150+
q.push(root);
151+
152+
while(!q.empty())
153+
{
154+
temp = q.front();
155+
q.pop();
156+
157+
cout<<temp->data<<" ";
158+
159+
//pushing temp's left and right child if any to queue
160+
//we will keep (level+1) children in the queue
161+
if(temp->left)
162+
{
163+
q.push(temp->left);
164+
}
165+
166+
if(temp->right)
167+
{
168+
q.push(temp->right);
169+
}
170+
}
171+
} //TIME COMPLEXITY = O(n) , SPACE COMPLEXITY = O(n)
153172

154173
int main()
155174
{
@@ -196,11 +215,13 @@ int main()
196215

197216
cout<<"Post order traversal outputs-"<<endl;
198217
PostOrderRec(root);
199-
//
200-
// cout<<endl;
201-
//
202-
// cout<<"Level order traversal-"<<endl;
203-
//
218+
219+
cout<<endl;
220+
221+
cout<<"Level order traversal-"<<endl;
222+
223+
levelOrder(root);
224+
204225

205226

206227

‎STL/map.cpp

+41-28
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,35 @@ using namespace std;
1212

1313
int main()
1414
{
15-
map<int,string> m;
15+
map<string,int> m;
1616

17-
m.insert(pair <int,string> (1,"Anish"));
18-
m.insert(pair <int,string> (2,"mrinal"));
19-
m.insert(pair <int,string> (3,"Vaibhav"));
20-
m.insert(pair <int,string> (4,"Rasila"));
17+
m.insert(pair <string,int> ("Anish",10));
18+
m.insert(pair <string,int> ("mrinal",2));
19+
m.insert(pair <string,int> ("Vaibhav",3));
20+
m.insert(pair <string,int> ("Rasila",4));
2121
//can also insert using
22-
m.insert(make_pair(10,"Lucky"));
22+
// m.insert(make_pair(10,"Lucky"));
23+
string name;
24+
int marks;
25+
cin>>name;
26+
cin>>marks;
27+
map<string,int>::iterator i=m.find(name);
28+
int new_marks = (i->second + marks);
29+
i->second = new_marks;
2330

24-
map<int,string>::iterator i;
31+
map<string,int>::iterator it;
2532

2633

2734
//can also do random access using the key value as subscript-O(1)
28-
cout<<m[2]<<endl; //prints mrinal
35+
cout<<m["Anish"]<<endl; //prints mrinal
2936

3037

3138
//traversing the map using iterator
32-
for(i = m.begin(); i != m.end();i++)
39+
for(it = m.begin(); it != m.end();it++)
3340
{
34-
cout<<i->first;
41+
cout<<it->first;
3542
cout<<" | ";
36-
cout<<i->second;
43+
cout<<it->second;
3744

3845
cout<<endl;
3946
}
@@ -45,26 +52,32 @@ int main()
4552

4653
//will erase elements from begin of map till 2nd element , not 3
4754
// remove all elements up to element with key = 3 in found
48-
m.erase(m.begin(), m.find(3) );
49-
50-
for(i = m.begin(); i != m.end();i++)
51-
{
52-
cout<<i->first;
53-
cout<<" | ";
54-
cout<<i->second;
55-
56-
cout<<endl;
57-
}
55+
// m.erase(m.begin(), m.find(3) );
56+
//
57+
// for(i = m.begin(); i != m.end();i++)
58+
// {
59+
// cout<<i->first;
60+
// cout<<" | ";
61+
// cout<<i->second;
62+
//
63+
// cout<<endl;
64+
// }
5865

5966

6067
//find() method returns an iterator to key passed as argument
6168

62-
map<int,string>::iterator i1;
63-
i1 = m.find(3);
64-
cout<<i1->second; //prints "mrinal"
65-
66-
67-
68-
return 0;
69+
// map<int,string>::iterator i1;
70+
// int n;
71+
// cin>>n;
72+
// i1 = m.find(n);
73+
// if(i1->first)
74+
// cout<<i1->second; //prints "mrinal"
75+
//
76+
// else
77+
// cout<<"Not found"<<endl;
78+
//
79+
//
80+
//
81+
// return 0;
6982

7083
}

0 commit comments

Comments
 (0)
Please sign in to comment.