File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -122,6 +122,41 @@ void postorder(Node* root){
122
122
}
123
123
}
124
124
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
+
125
160
int main (){
126
161
127
162
Node* root=newNode (60 );
@@ -144,6 +179,8 @@ int main(){
144
179
preorder (root);
145
180
cout<<endl;
146
181
postorder (root);
182
+ cout<<endl;
183
+ adv_postorder (root);
147
184
148
185
printf (" \n Delete 75\n " );
149
186
root = deleteNode (root, 75 );
@@ -153,6 +190,8 @@ int main(){
153
190
preorder (root);
154
191
cout<<endl;
155
192
postorder (root);
193
+ cout<<endl;
194
+ adv_postorder (root);
156
195
157
196
printf (" \n Delete 30\n " );
158
197
root = deleteNode (root, 30 );
@@ -162,6 +201,8 @@ int main(){
162
201
preorder (root);
163
202
cout<<endl;
164
203
postorder (root);
204
+ cout<<endl;
205
+ adv_postorder (root);
165
206
166
207
printf (" \n Delete 60\n " );
167
208
root = deleteNode (root, 60 );
@@ -171,6 +212,8 @@ int main(){
171
212
preorder (root);
172
213
cout<<endl;
173
214
postorder (root);
215
+ cout<<endl;
216
+ adv_postorder (root);
174
217
175
218
cout<<endl;
176
219
return 0 ;
You can’t perform that action at this time.
0 commit comments