Skip to content

Commit 8497893

Browse files
committed
Traversals tree
1 parent a4fb0a6 commit 8497893

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

geekforgeeks/trees/BST/a.out

4.26 KB
Binary file not shown.

geekforgeeks/trees/BST/traversal.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,41 @@ void postorder(Node* root){
122122
}
123123
}
124124

125+
Node* peek(stack<Node*> stack)
126+
{
127+
if (stack.empty())
128+
return NULL;
129+
return stack.top();
130+
}
131+
132+
void adv_postorder(Node* root){
133+
if(root == NULL)
134+
return;
135+
stack<Node*>stack;
136+
do{
137+
while(root){
138+
if(root->right)
139+
stack.push(root->right);
140+
stack.push(root);
141+
142+
root=root->left;
143+
}
144+
145+
root=stack.top();
146+
stack.pop();
147+
148+
if(root->right && peek(stack)==root->right){
149+
stack.pop();
150+
stack.push(root);
151+
root=root->right;
152+
}
153+
else{
154+
cout<<root->data<<" ";
155+
root=NULL;
156+
}
157+
}while(!stack.empty());
158+
}
159+
125160
int main(){
126161

127162
Node* root=newNode(60);
@@ -144,6 +179,8 @@ int main(){
144179
preorder(root);
145180
cout<<endl;
146181
postorder(root);
182+
cout<<endl;
183+
adv_postorder(root);
147184

148185
printf("\nDelete 75\n");
149186
root = deleteNode(root, 75);
@@ -153,6 +190,8 @@ int main(){
153190
preorder(root);
154191
cout<<endl;
155192
postorder(root);
193+
cout<<endl;
194+
adv_postorder(root);
156195

157196
printf("\nDelete 30\n");
158197
root = deleteNode(root, 30);
@@ -162,6 +201,8 @@ int main(){
162201
preorder(root);
163202
cout<<endl;
164203
postorder(root);
204+
cout<<endl;
205+
adv_postorder(root);
165206

166207
printf("\nDelete 60\n");
167208
root = deleteNode(root, 60);
@@ -171,6 +212,8 @@ int main(){
171212
preorder(root);
172213
cout<<endl;
173214
postorder(root);
215+
cout<<endl;
216+
adv_postorder(root);
174217

175218
cout<<endl;
176219
return 0;

0 commit comments

Comments
 (0)