Conventional Iterative Algorithm

Design a conventional iterative algorithm to traverse a binary tree represented in linked lists in postorder.

Questions by Dheerendra_juli

Showing Answers 1 - 3 of 3 Answers

trivial

  • Aug 19th, 2009
 

We define int visit[SIZEOFTREE] such that visit[i]=1 if i th node is visited=0 otherwise
visit[NULL]=1 always
void posttrav_iter(NODEPTR treeroot){
NODEPTR p=pstree;
empty stack s;
initialise visit to all 0's;
while(!empty(s) OR p!=NULL){
while(p!=NULL){
push(s,p); p=p->left;
}
if(!empty(s)){
p=pop(s);
if( visit[p->left] AND visit[p->right]){
print(p->info); visit[p]=1; p=NULL;
}
else { push(s,p); p=p->right;
}//end of else
}//end of if
}//end of while
}//end of function posttrav_iter


This is just an idea. If something wrong please let me know if anyone has any
other idea of how to implement visit[i] Please tell me


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions