GeekInterview.com
Series: Subject: Topic:
Question: 15 of 51

Conventional Iterative Algorithm

Design a conventional iterative algorithm to traverse a binary tree represented in linked lists in postorder.
Asked by: Dheerendra_juli | Member Since Jul-2009 | Asked on: Jul 22nd, 2009

View all questions by Dheerendra_juli

Showing Answers 1 - 1 of 1 Answers
trivial

Answered On : Aug 19th, 2009

View all answers by trivial

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


  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.