Postorder traversal of Binary tree is a traversal method, where left subtree is visited first, then right subtree, and finally root node. Unlike array and linked lists, being linear data structures, we have several ways of traversing binary trees due to their hierarchical nature. These tree traversal algorithms are divided into two types, i.e., Depth First Algorithms and Breadth-First Algorithms. In-Depth First Algorithm, trees are traversed downwards. On the other hand, preOrder, InOrder, PostOrder are actually depth-first traversal algorithms. PostOrder traversal is used to delete the binary tree. Let us dig deeper into the PostOrder traversal of a Binary tree and see how to preorder traversal is implemented and so on.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
Step 1: Traverse the left subtree, i.e., traverse recursively.
Step 2: Traverse the right subtree, i.e., traverse recursively.
Step 3: Finally, visit the root node.
PostOrder traversal is useful to get the postfix of an expression in a Binary tree. In this traversal, the root node is visited at last and hence the name.
Let us see one more Binary tree for implementing Traversal,
PostOrder Traversal: B F D E C A
The first element printed is B, as it is the left node.
Then traverses to the right subtree, which has left and right node, Node D has left node, i.e., F.
As there is no right node, it will traverse back to the root node of the subtree, i.e., D.
As there is no right node, it will traverse back to the root node of the subtree, i.e., C and checks if there is any right node, i.e., E.
Then traverses to the root node of the subtree, i.e., C., and then A, the main root node.
Below are some advantages and disadvantages of Postorder traversal:
Example: PostOrder Traversal in Python Language.
class BinaryNode:
def __init__(self, key):
self.leftNode = None
self.rightNode = None
self.value = key
def postOrder(rootnode):
if rootnode:
postOrder(rootnode.leftNode)
postOrder(rootnode.rightNode)
print(rootnode.value),
rootnode = BinaryNode(6)
rootnode.leftNode = BinaryNode(1)
rootnode.rightNode = BinaryNode(4)
rootnode.leftNode.leftNode = BinaryNode(2)
rootnode.leftNode.rightNode = BinaryNode(3)
rootnode.leftNode.rightNode = BinaryNode(5)
print("\nPostorder traversal of binary tree is")
postOrder(rootnode);
Output:
So here, according to the traversal algorithm, we shall design the tree manually and verify the output, which will make us understand the concept better.
Based on the input, we have designed the binary tree above.
Explanation:
Root Node: 6
Left Node: 1
Right Node: 4
Left Node of Node 1: 2
Right Node of Node 1: 3
Left Node of Node 3: 5
Hence, the traversal will be as follows,
[2, 5, 3, 1, 4, 6][2, 5, 3, 1, 4, 6]
With this, we shall conclude the topic ‘Postorder Traversal’. We have seen what traversal is and its algorithm. Also implemented few sample examples to illustrate the traversal algorithm practically. A binary tree can be traversed in two types, Breadth-first traversal and Depth-first traversal. Depth-first traversal includes preorder, inorder, and traversals. Out of which, we have seen traversal here and will get to know of the other types in coming sessions. We have also listed out the Advantages and Disadvantages of traversal in the Binary tree.
This is a guide to Postorder traversal. Here we discuss How PostOrder traversal of the Binary tree works along with the advantages and disadvantages. You may also have a look at the following articles to learn more –
Tree traversal refers to visiting all the nodes of a tree exactly once. Visiting means doing something to the node. It can be as basic as printing the node.
Post-order traversal is one of the multiple methods to traverse a tree. It is mainly used for tree deletion.
The following algorithm is specific to a binary tree but can be generalized to an n-ary tree (a tree where each node can have at most n children nodes).
Are you interested in learning more about china silver plated yarns company, emf blocking bed canopy, stainless steel fiber? Contact us today to secure an expert consultation!