-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Tree_Traversal.cpp
More file actions
225 lines (175 loc) · 4.81 KB
/
Copy pathBinary_Tree_Traversal.cpp
File metadata and controls
225 lines (175 loc) · 4.81 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include<iostream>
#include<iomanip>//for the use of setw
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
//Traversal of the Binary tree
struct Node{
struct Node* left;
struct Node* right;
int data;
Node(int val)
{
data=val;
left=right=NULL;
}
};
//Preorder Traversal with recursion
void Preorder_traversal(Node* root)//Root-Left-RIGHT
{
if(root==NULL)//if the Node is empty
return;
//recursion
cout<<setw(2)<<root->data<<" ";
Preorder_traversal(root->left);
Preorder_traversal(root->right);
}
//INorder Traversal with recursion
void Inorder_traversal(Node* root)//Left-root-right
{
if(root==NULL)//if the Node is empty
return;
Inorder_traversal(root->left);
cout<<setw(2)<<root->data<<" ";
Inorder_traversal(root->right);
}
//Postorder Traversal with recursion
void Post_order_traversal(Node* root)//Left-Right-Root
{
if(root==NULL)//if the Node is NULL
return;
Post_order_traversal(root->left);
Post_order_traversal(root->right);
cout<<setw(2)<<root->data<<" ";
}
//--------Level order Traverser--------------
void Level_order_Traversal(Node* root)
{
if(root==NULL)
return;
queue<Node*>q;
q.push(root);
while(!q.empty())//untill Level is not complete we need to check left and right for every node
{
int n=q.size();//level
for(int i=0;i<n;i++)
{
Node* node=q.front();
cout<<setw(2)<<node->data<<" ";
q.pop();
if(node->left!=NULL)
{
q.push(node->left);
}
if(node->right!=NULL)
{
q.push(node->right);
}
}
}
}
//------Iterative Preorder Traverser---(Root-Left-Right)-------
vector<int> Traversel(Node* root)//it is somewaht like Level order Traverser
{
vector<int>ans;
if(root==NULL)return ans ;
stack<Node*>st;
st.push(root);
while(!st.empty())
{
Node* temp=st.top();
ans.push_back(temp->data);
st.pop();
(temp->right!=NULL)?(st.push(temp->right)):void();
(temp->left!=NULL)?(st.push(temp->left)):void();
}
return ans;
}
//------Iterative Postorder Traverser---(Left-Right-Root)----------
//When you reverse the preorder it become Right-Left-Root but we need Left-Right-Root so swap again Left-Right
vector<int> TraverseP(Node* root)//Ittrative Postorder solution
{
vector<int>ans;
if(root==NULL)return ans;
stack<Node*>st;
st.push(root);
while(!st.empty())
{
Node* temp=st.top();
ans.push_back(temp->data);
st.pop();
(temp->left!=NULL)?(st.push(temp->left)):void();
(temp->right!=NULL)?(st.push(temp->right)):void();
}
reverse(ans.begin(),ans.end());//we get reverse ans so reverse it
return ans;
}
//---------- Iterative INorder Traversal ----(Left-Root-Right)-------------
vector<int> Traverse_IN(Node* root)
{
vector<int>ans;
if(root==NULL)return ans;
stack<Node*>st;
Node* node=root;
while(true)
{
if(node!=NULL)
{
st.push(node);
node=node->left;//check for left
}
else
{
if(st.empty())break;//complete
node=st.top();//pop from stak then check for next
ans.push_back(node->data);
st.pop();
node=node->right;//go to check right
}
}
return ans;
}
void Print_Solution(vector<int>&ans)
{
for(int i=0;i<ans.size();i++)
{
cout<<setw(2)<<ans[i]<<" ";
}
}
int main()
{
Node * root=new Node(1);//
root->left=new Node(2);
root->left->right=new Node(3);
root->left->left=new Node(4);
root->right=new Node(5);
root->right->left=new Node(6);
root->right->right=new Node(7);
//using function for traversal
cout<<setw(26)<<"Pre order Traversal:";
Preorder_traversal(root);
cout<<endl<<endl;
cout<<setw(26)<<"In order Traversal:";
Inorder_traversal(root);
cout<<endl<<endl;
cout<<setw(26)<<"Post order Traversal:";
Post_order_traversal(root);
cout<<endl<<endl;
cout<<setw(26)<<"Level order Traverser:";
Level_order_Traversal(root);
cout<<endl<<endl;
vector<int>nums= Traversel(root);
cout<<setw(26)<<"Iterative Traverser(pre):";
Print_Solution(nums);
cout<<endl<<endl;
vector<int>nums1=TraverseP(root);
cout<<setw(26)<<"Iterative Traverser(post):";
Print_Solution(nums1);
cout<<endl<<endl;
vector<int>nums3=Traverse_IN(root);
cout<<setw(26)<<"Iterative Traverser(in):";
Print_Solution(nums3);
cout<<endl<<endl;
return 0;
}