The idea is to create links to root from predecessor node in the left sub-tree for each node.
When traversal on the current node in done, move to the right child and make that node as current node and follow the same procedure.
1. Start from the root of the current tree, as current node.
2. If current node's left Child is NULL
print the node data.
make right of the current node as current.
else
If right child of the right most node of the left sub-tree is NULL
make it as a link to current node.
make left of the current node as current.
else
print the current node data.
make right of the current node as current.
When traversal on the current node in done, move to the right child and make that node as current node and follow the same procedure.
1. Start from the root of the current tree, as current node.
2. If current node's left Child is NULL
print the node data.
make right of the current node as current.
else
If right child of the right most node of the left sub-tree is NULL
make it as a link to current node.
make left of the current node as current.
else
print the current node data.
make right of the current node as current.
void MorrisInOrderTraversal(struct node* root){ struct node *curr,*pre; if(root==NULL){ return; } curr=root; while(curr!=NULL){ if(curr->left==NULL){ printf("%d ",curr->data); curr=curr->right; }else{ pre=curr->left; while(pre->right!=NULL && pre->right!=curr){ pre=pre->right; } if(pre->right==NULL){ pre->right=curr; curr=curr->left; }else{ pre->right=NULL; printf("%d ",curr->data); curr=curr->right; } } } }
No comments:
Post a Comment