Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

Postorder Tree Traversal in Python

Postorder Tree Traversal In Python

In this article, we will study the concept and algorithm for postorder tree traversal. Then we will implement the algorithm for postorder traversal in python and run it on a binary tree.

What is Postorder Tree Traversal?

Postorder traversal is a depth-first tree traversal algorithm. In depth-first traversal, we start at the root node and then we explore a branch of the tree till the end and then we backtrack and traverse another branch.

In the postorder traversal, first, we traverse the left child or left subtree of the current node and then we traverse the right child or right subtree of the current node. At last, we traverse the current node.

We perform this operation recursively till all the nodes are traversed. We use postorder traversal to delete a binary tree. We can also use postorder tree traversal to find postfix expression from an expression tree.

Postorder Traversal Algorithm

Following is the algorithm for postorder traversal.

  • Algorithm postorder:
  • Input: Reference to Root Node
  • Output: Prints All the nodes of the tree
  • Start.
  • If the root is empty, return.
  • Traverse left subtree of the root.// postorder(root.leftChild)
  • Traverse the right subtree of the root.// postorder(root.rightChild)
  • Traverse the root node. //print value at node
    End.

Postorder Traversal Algorithm implementation in python

Now we will implement the above algorithm to print nodes of the following binary tree in postorder traversal.

Askpython31 2
Binary Tree

In the following code, first the above binary tree has been created and then postorder traversal of the binary tree is printed.

class BinaryTreeNode:
  def __init__(self, data):
    self.data = data
    self.leftChild = None
    self.rightChild=None
    
def insert(root,newValue):
    #if binary search tree is empty, make a new node and declare it as root
    if root is None:
        root=BinaryTreeNode(newValue)
        return root
    #binary search tree is not empty, so we will insert it into the tree
    #if newValue is less than value of data in root, add it to left subtree and proceed recursively
    if newValue<root.data:
        root.leftChild=insert(root.leftChild,newValue)
    else:
        #if newValue is greater than value of data in root, add it to right subtree and proceed recursively
        root.rightChild=insert(root.rightChild,newValue)
    return root
def postorder(root):
    #if root is None return
        if root==None:
            return
        #traverse left subtree
        postorder(root.leftChild)
        #traverse right subtree
        postorder(root.rightChild)  
        #traverse root
        print(root.data)                 
root= insert(None,15)
insert(root,10)
insert(root,25)
insert(root,6)
insert(root,14)
insert(root,20)
insert(root,60)
print("Printing values of binary tree in postorder Traversal.")
postorder(root)

Output:

Printing values of binary tree in postorder Traversal.
6
14
10
20
60
25
15

Here, we can see that every child of a node is traversed before the current node is processed. So we can use postorder traversal to delete a binary tree as we can start deleting nodes from a leaf and go upto the root.

Conclusion

In this article, We have learned the concept of postorder tree traversal. We also studied the algorithm and implemented it in Python to traverse a binary tree. Stay tuned for more informative articles.