0% found this document useful (0 votes)
11 views

traversal using stack

The document outlines algorithms for post-order, in-order, and pre-order traversal of a binary tree using stacks. Each traversal method is described step-by-step, detailing the initialization of stacks, handling of nodes, and the final collection of traversed elements. The algorithms emphasize the use of stacks to manage the order of node visits in a binary tree structure.

Uploaded by

harshkantiwal187
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)
11 views

traversal using stack

The document outlines algorithms for post-order, in-order, and pre-order traversal of a binary tree using stacks. Each traversal method is described step-by-step, detailing the initialization of stacks, handling of nodes, and the final collection of traversed elements. The algorithms emphasize the use of stacks to manage the order of node visits in a binary tree structure.

Uploaded by

harshkantiwal187
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/ 19

TRAVERSAL ALGORITHM USING STACK

1. Post-order traversal of a binary tree using stacks


Post-Order traversal is a way of traversing a binary tree and follows the pattern below:

1. Visit the left part of the node.


2. Visit the right part of the node.
3. Visit the node.

In this shot, we use two stacks to traverse the binary tree in post-order.

Algorithm

• Initialize two stacks and an array.


• Check if the root element is null. If it is null, then the tree is empty and returns an empty array.
• Otherwise, push the root element to stack 1.
• While stack 1 is not empty:
1. Remove the top element from stack 1.
2. Push that element to stack 2.
3. Check if the stack has left and right elements present. If present, push them onto stack 1, first
pushing the left child and then the right child.
• If stack 1 becomes empty, the while loop will end.
• Now pop all elements from stack 2 and add them into the array.
• The array list will contain the elements traversed in a post-order fashion.
• Return the array.

1.
2. Inorder traversal of a binary tree
An iterative inorder traversal of a binary tree is done using the stack data structure.

Algorithm

• Initialize an empty stack.


• Push the current node (starting from the root node) onto the stack. Continue pushing nodes to the left
of the current node until a NULL value is reached.
• If the current node is NULL and the stack is not empty:
1. Remove and print the last item from the stack.
2. Set the current node to be the node to the right of the removed node.
3. Repeat the second step of the algorithm.
• If the current node is NULL and the stack is empty, then the algorithm has finished.

The algorithm is illustrated on a sample binary tree below:


3. Binary tree preorder traversal iteratively
Traversing a tree means to visit all the nodes in a tree exactly once.

In a Pre-Order traversal, we visit all the nodes as follows:

1. Visit a node.
2. Visit the left child of the node.
3. Visit the right child of the node.

Algorithm

The algorithm for a pre-order traversal of a binary tree is as follows:

• Initialize an array to store the traversed elements.


• Initialize an empty stack.
• Take the root element and push it into the stack.
• Use a while loop to perform the following steps until the stack is empty:
1. Remove the element at the top of the stack and add it to the array.
2. Check if the removed element has left or right children.
3. If the removed element does have children nodes, then push the right child into the stack first
(if there is a right child), and then the left child (if a left child exists).
• When the stack becomes empty, the while loop will end, then the array that contains the elements in
pre-order can be returned.

The right child is added to the stack before the left child so that we can pop the left element first, since the
stack is a Last-in-First-Out (LIFO) data structure, and in a preorder traversal, the left child should be
traversed before the right child.

The slides below illustrate the traversal process:

You might also like