Postorder TraversalIn this article, we will discuss the postorder traversal in data structure. Linear data structures such as stack, array, queue, etc., only have one way to traverse the data. But in a hierarchical data structure such as tree, there are multiple ways to traverse the data. So, here we will discuss another way to traverse the tree data structure, i.e., postorder traversal. The postorder traversal is one of the traversing techniques used for visiting the node in the tree. It follows the principle LRN (Left-right-node). Postorder traversal is used to get the postfix expression of a tree. The following steps are used to perform the postorder traversal:
The post order traversal technique follows the Left Right Root policy. Here, Left Right Root means the left subtree of the root node is traversed first, then the right subtree, and finally, the root node is traversed. Here, the Postorder name itself suggests that the tree's root node would be traversed at last. AlgorithmNow, let's see the algorithm of postorder traversal. Example of postorder traversalNow, let's see an example of postorder traversal. It will be easier to understand the process of postorder traversal using an example. The nodes with yellow color are not visited yet. Now, we will traverse the nodes of above tree using postorder traversal.
The final output that we will get after postorder traversal is - {15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40} Complexity of Postorder traversalThe time complexity of postorder traversal is O(n), where 'n' is the size of binary tree. Whereas, the space complexity of postorder traversal is O(1), if we do not consider the stack size for function calls. Otherwise, the space complexity of postorder traversal is O(h), where 'h' is the height of tree. Implementation of Postorder traversalNow, let's see the implementation of postorder traversal in different programming languages. Program: Write a program to implement postorder traversal in C language. Output Program: Write a program to implement postorder traversal in C++. Output Program: Write a program to implement postorder traversal in C#. Output After the execution of the above code, the output will be - Program: Write a program to implement postorder traversal in Java. Output After the execution of the above code, the output will be - So, that's all about the article. Hope the article will be helpful and informative to you. Next TopicSparse Matrix |