0% found this document useful (0 votes)
109 views2 pages

Non Recusive Traversal

The document discusses three non-recursive algorithms for traversing binary trees: 1) Inorder traversal using a stack by pushing nodes to the stack until reaching the leftmost node, then popping and printing nodes. 2) Preorder traversal using a stack by pushing the root and its right child, then popping and printing nodes while pushing left children. 3) Postorder traversal using a stack by pushing the root's right child and root, popping nodes and printing if the right child was visited, otherwise visiting the right child.

Uploaded by

Kumar Satyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views2 pages

Non Recusive Traversal

The document discusses three non-recursive algorithms for traversing binary trees: 1) Inorder traversal using a stack by pushing nodes to the stack until reaching the leftmost node, then popping and printing nodes. 2) Preorder traversal using a stack by pushing the root and its right child, then popping and printing nodes while pushing left children. 3) Postorder traversal using a stack by pushing the root's right child and root, popping nodes and printing if the right child was visited, otherwise visiting the right child.

Uploaded by

Kumar Satyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Structure Using C

Topic – Non Recursive Algorithm for Inorder,


preorders and postorder

Inorder Tree Traversal without Recursion


Using Stack is the obvious way to traverse tree without recursion. Below is an
algorithm for traversing binary tree using stack. See this for step wise step
execution of the algorithm.
1) Create an empty stack S.
2) Initialize current node as root
3) Push the current node to S and set current = current->left until
current is NULL
4) If current is NULL and stack is not empty then
a) Pop the top item from stack.
b) Print the popped item, set current = popped_item->right
c) Go to step 3.
5) If current is NULL and stack is empty then we are done.

Iterative Preorder Traversal


Given a Binary Tree, write an iterative function to print Preorder traversal of
the given binary tree.

To convert an inherently recursive procedures to iterative, we need an


explicit stack. Following is a simple stack based iterative process to print
Preorder traversal.
1) Create an empty stack nodeStack and push root node to stack.
2) Do following while nodeStack is not empty.
….a) Pop an item from stack and print it.
….b) Push right child of popped item to stack
….c) Push left child of popped item to stack

Right child is pushed before left child to make sure that left subtree is
processed first.
Iterative Postorder Traversal
The idea is to move down to leftmost node using left pointer. While moving
down, push root and root’s right child to stack. Once we reach leftmost node,
print it if it doesn’t have a right child. If it has a right child, then change root
so that the right child is processed before.

Following is detailed algorithm.

1.1 Create an empty stack

2.1 Do following while root is not NULL

a) Push root's right child and then root to stack.

b) Set root as root's left child.

2.2 Pop an item from stack and set it as root.

a) If the popped item has a right child and the right child

is at top of stack, then remove the right child from stack,

push the root back and set root as root's right child.

b) Else print root's data and set root as NULL.

2.3 Repeat steps 2.1 and 2.2 while stack is not empty

You might also like