Postorder Traversal in C++

Last updated: June 25, 2023

In tree data structures, there are three commonly used types of traversals: pre-order, in-order, and post-order. These traversal techniques define the order in which the nodes of a tree are visited. A tree can be traversed in the following ways:

  • In-order Traversal
  • Pre-order Traversal
  • Post-order Traversal

Post-order Traversal in Trees

Post-order traversal is a way to visit all the nodes of a tree in a specific order. Traversing a binary tree in post-order means visiting the nodes in the following order:

1. Traverse the left subtree.
2. Traverse the right subtree.
3. Visit the root node.

Post-order traversal visits the current node after its children.

Post-order traversal is commonly used in scenarios where we need to perform operations that require visiting the subtrees before visiting the root node. It can be used, for example, to delete the tree or to perform certain calculations that rely on the values of the child nodes.

Here's an example of a post-order traversal of a tree:


      

Postorder Traversal in C++

 

The post-order traversal of this tree would result in the following sequence of numbers: 3, 1, 6, 4, 2, 7, 9, 8, 5.

 

Implementation of Post-order Traversal in C++:


#include <iostream>
using namespace std;
struct TreeNode {
   int data;
   TreeNode* left;
   TreeNode* right;
};
TreeNode* createNode(int data) {
   TreeNode* newNode = new TreeNode;
   newNode->data = data;
   newNode->left = nullptr;
   newNode->right = nullptr;
   return newNode;
}
void postOrderTraversal(TreeNode* root) {
   if (root != nullptr) {
       postOrderTraversal(root->left);
       postOrderTraversal(root->right);
       cout << root->data << " ";
   }
}
int main() {
   // Create the tree
   TreeNode* root = createNode(5);
   root->left = createNode(2);
   root->right = createNode(8);
   root->left->left = createNode(1);
   root->left->right = createNode(4);
   root->right->left = createNode(7);
   root->right->right = createNode(9);
   root->left->left->left = createNode(3);
   root->left->right->right = createNode(6);
   // Perform post-order traversal
   cout << "Post-order traversal: ";
   postOrderTraversal(root);
   cout << endl;
   return 0;
}

Output:

Post-order traversal: 3 1 6 4 2 7 9 8 5

In the above code, the 'postOrderTraversal' function is defined to perform the post-order traversal of the binary tree. It recursively calls itself on the left subtree, then on the right subtree, and finally prints the data of the current node.

In the `main` function, the binary tree is created using the 'createNode' function, similar to the previous example.

After creating the tree, the ‘postOrderTraversal’ function is called with the root of the tree as the argument. This will traverse the tree in post-order fashion and print the node values.

Post-order traversal is particularly useful when we need to perform operations on the subtrees before processing the root node. It ensures that the left and right subtrees are visited before the root, allowing us to implement complex operations efficiently.

 

Related post