Postorder traversal is a type of depth-first traversal used in binary trees which starts from root node and process all the nodes of the tree. In postorder traversal, the nodes are recursively visited and processed in the following order:
- Traverse the left subtree in postorder- First go to the left child node if it is present, apply the postorder traversal on the left child node.
- Traverse the right subtree in postorder. - Once the left subtree has been processed, go to the right child node and apply postorder traversal on the right child node.
- Process data at current node- Once postorder has been applied on the left child node and the right child node, process the data stored in the current node( In starting it is root node).
Method 1: Using STACK
postorderByStack(root)
- Let ST1 and ST2 are two stacks
- ST1.push(root)
- While ST1 is not empty:
- {
-
item=ST1.pop()
-
if item.left is not null then ST1.push(item.left)
-
if item.right is not null then ST1.push(item.right)
-
ST2.push(item)
- }
- While ST2 is not empty:
- {
-
current=ST2.pop()
-
Process current.data(print current.data)
- }
- root node is pushed on stack ST1 in the starting.
- Stack ST1 completes the LR part of formula LRRt iteratively using while loop present at line no 3. In this loop left node is pushed first then right node.
- While popping stack ST1 at line no 5 always pushed right node comes first which is again pushed to stack ST2. That's why stack ST2 stores result in LIFO( Last In First Out) order.
- To get final result ST2 is popped one by one(First pushed record here means the last element in the postorder traversal and vice versa).
Method 2: Using Recursion
From the above figure we can easily see postorder traversal can be implemented recursively. As we know from Recursion chapter, while designing recursive solution there are 3 steps to follow which are given below:
Hypothesis - In designing recursive solution we should define a function and its necessary parameters which will solve the given problem. In our case while applying postorder traversal to any node we need the left child, node data and the right child node which will be accessed from the given node itself. So only the current node is sufficient input parameter where processing will start from the root node. We are defining below function which will solve our problem.
Postorder(root)
Inductive Step - Here we need to define the problem in terms of the subproblems of itself. Here we know from the above mentioned formula postorder works on formula LRRt(Apply postorder on the left subtree,apply postorder on the right subtree, process the data on current node ). So we can define postorder traversal as below in the inductive step.
Postorder(root)=Postorder(root.left)==>Postorder(root.right)==>(process data at root)
or
Postorder(root)=
Postorder(root.left)
Postorder(root.right)
(Process data at root)
Base Condition - Function body defined in the Inductive step will not work as after leaf node left or right child will be null so there must be some condition to handle this boundary which is called Base Condition. Here if current node is null means no subtree to process. So there must not be any processing when the current node is null as given below:
Postorder(root)=
if root is null do nothing and return
Postorder(root.left)
Postorder(root.right)
(Process data at root)
So below will be postorder tree traversal algorithm for a given binary tree using recursion
{
- If the root node is null then do nothing and return
- Postorder(root.left)
- Postorder(root.right)
- Process root.data(print root.data)