iterative traversal of a BST Get link Facebook X Pinterest Email Other Apps By Admin - June 19, 2011 Write the iterative version of inorder,preorder and postorder traversal of a binary search tree. Get link Facebook X Pinterest Email Other Apps Comments AdminJune 19, 2011 at 1:05 PMvoid inorder(struct node* p){ stack s; do { while(p != NULL) { s.push(p); p = p->left; } if(!s.empty()) { p = s.top(); s.pop(); cout<data<right; } }while(!s.empty() || p != NULL);}void preorder(struct node* p){ stack s; do { while(p != NULL) { s.push(p); cout<data<left; } if(!s.empty()) { p = s.top(); s.pop(); p = p->right; } }while(!s.empty() || p != NULL);}void postorder(struct node* p){ stack s; do { INSERT: while(p != NULL) { s.push(p); p = p->left; } if(!s.empty()) { p = s.top(); s.pop(); if(p->right == NULL) cout<data<<endl; else goto INSERT; } }while(!s.empty() || p != NULL);}ReplyDeleteRepliesReplyAdd commentLoad more... Post a Comment
void inorder(struct node* p)
ReplyDelete{
stack s;
do
{
while(p != NULL)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
cout<data<right;
}
}while(!s.empty() || p != NULL);
}
void preorder(struct node* p)
{
stack s;
do
{
while(p != NULL)
{
s.push(p);
cout<data<left;
}
if(!s.empty())
{
p = s.top();
s.pop();
p = p->right;
}
}while(!s.empty() || p != NULL);
}
void postorder(struct node* p)
{
stack s;
do
{
INSERT:
while(p != NULL)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
if(p->right == NULL)
cout<data<<endl;
else goto INSERT;
}
}while(!s.empty() || p != NULL);
}