-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-in-post-traversal.cpp
167 lines (142 loc) · 3.73 KB
/
pre-in-post-traversal.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
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
#include<iostream>
#include<stack>
#define endl "\n"
using namespace std;
// Basic Structure of a Tree
struct Node{
int data;
struct Node *left;
struct Node *right;
Node(int val){
data = val;
left = NULL;
right = NULL;
}
};
/* Pre, In and Post order Traversal of a Binary Tree using recusrion */
/* Complexity for each Traversal : O(n) */
// LST -> Left SubTree RST -> Right SubTree
// Pre-Order goes like :- Root, LST, RST (Recursively)
void pre_order(struct Node* root){
if(root == NULL)
return;
cout<<root->data<<" ";
pre_order(root->left);
pre_order(root->right);
}
// In-Order goes like :- LST, Root, RST (Recursively)
void in_order(struct Node* root){
if(root == NULL)
return;
in_order(root->left);
cout<<root->data<<" ";
in_order(root->right);
}
// Pre-Order goes like :- LST, RST, Root (Recursively)
void post_order(struct Node* root){
if(root == NULL)
return;
post_order(root->left);
post_order(root->right);
cout<<root->data<<" ";
}
/* Pre, In and Post order Traversal of a Binary Tree using Iteration */
/* Complexity for each Traversal : O(n) */
void NRpreorder(struct Node* root) //Iterative Preorder Traversal
{
stack <Node*> s;
while(!s.empty() || root!=NULL)
{
while(root!=NULL)
{
cout<<root->data<<" ";
s.push(root);
root=root->left;
}
root=s.top();
s.pop();
root=root->right;
}
}
void NRinorder(struct Node* root) //Iterative Inorder Traversal
{
stack <Node*> s;
while(!s.empty() || root!=NULL)
{
while(root!=NULL)
{
s.push(root);
root=root->left;
}
root=s.top();
s.pop();
cout<<root->data<<" ";
root=root->right;
}
}
void NRpostorder(struct Node* root) //Iterative Postorder Traversal
{
stack <Node*> s1;
stack <int> s2;
while(root!=NULL)
{
s1.push(root);
s2.push(0);
root=root->left;
}
while(!s1.empty())
{
root=s1.top();
if(s2.top()==1)
{
s1.pop();
s2.pop();
cout<<root->data<<" ";
}
else
{
s2.top()=1;
root=root->right;
while(root!=NULL)
{
s1.push(root);
s2.push(0);
root=root->left;
}
}
}
}
int main(){
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->right = new Node(7);
/*
1
/ \
2 3
/ \ \
4 5 7
Expected-> From Recursive calls :
pre-order : 1 2 4 5 3 7
in-order : 4 2 5 1 3 7
post-order : 4 5 2 7 3 1
From Non-Recursive/Iterative calls :
preorder : 1 2 4 5 3 7
inorder : 4 2 5 1 3 7
postorder : 4 5 2 7 3 1
*/
// Tree Traversal using recursive calls
cout<<"From Recursive calls : "; cout<<endl;
cout<<"pre-order : "; pre_order(root); cout<<endl;
cout<<"in-order : "; in_order(root); cout<<endl;
cout<<"post-order : "; post_order(root); cout<<endl<<endl;
// ~~~~~~ Tree Traversal using non-recusive/Iterative calls ~~~~~~
cout<<"From Non-Recursive/Iterative calls : "; cout<<endl;
cout<<"preorder : "; NRpreorder(root); cout<<endl;
cout<<"inorder : "; NRinorder(root); cout<<endl;
cout<<"postorder : "; NRpostorder(root); cout<<endl;
return 0;
}