Thursday, April 15, 2010

Iterative Inorder Traversal

Iterative inorder traversal is a little complicated than Preorder traversal. As for Preorder we need to use stack for Inorder traversal as well but the nodes can't be removed from stack until it's children are pushed to the stack and the node has been visited. For all nodes we need to push their children on the stack first before visiting it. We mark whether the children have been put into stack or not for a node my using another stack call marks. When adding children to the stack we set mark to true on the marks stack. Now when we pop the node again we check corresponding mark and if it is set to true we know that it's children has already been processed. Thus we visit the node (print the value in the code below). In Inorder traversal left child, parent and right child are processed respectively. So while pushing on the stack we do it on the reverse order so that Inorder requirements are maintained.

  
void inorder_traversal(Node *root) {
if(root == 0)
return;

std::vector<bool> marks;
std::vector<Node*>nodes;
nodes.push_back(root);
marks.push_back(false);

while(!nodes.empty()){

//read the top mark
bool mark = marks.back();
marks.pop_back();

//read the top node
Node *node = nodes.back();
nodes.pop_back();

if(mark) {
//if mark is set then the node is visited
std::cout << node->value_ << std::endl;
}
else {
//put the righ child on stack
if(node->right_ != NULL) {
nodes.push_back(node->right_);
marks.push_back(false);
}

//set mark to true
//put the node in the stack
marks.push_back(true);
nodes.push_back(node);

//put left child on the stack
if(node->left_ != NULL) {
nodes.push_back(node->left_);
marks.push_back(false);
}
}
}
}

No comments:

Post a Comment