Postorder Traversal of a Binary Tree using Recursion in C++

This is a C++ Program to perform PostOrder Traversal of a given Binary Tree recursively.

Problem Description

We will be given a Binary Tree and we have to create a recursive C++ program to print all the nodes in a tree using PostOrder traversal. We have to create a separate recursive function which will traverse the tree using classes and objects.

Expected Input and Output

Case 1. Balanced Tree: When the weight on both the sides of root node is equal.

                    25
                  /    \
                 19     29
                / \     / \
              17  20   27 55

Output: 17 20 19 27 55 29 25

Case 2. Right Skewed Tree: When the internal nodes in a tree have just a right child.

advertisement
advertisement
                    1
                     \
                      2
                       \
                        3
                         \
                          4
                           \
                            5

Output: 5 4 3 2 1

Case 3. Tree having just one node

Note: Join free Sanfoundry classes at Telegram or Youtube
              15

Output: 15

Problem Solution

In PostOrder Traversal of a Binary Tree, we first traverse the left subtree, then the right subtree and finally we print the value of the node. We will write a recursive function which traverses the tree as mentioned above.

Program/Source Code

Here is source code of the C++ Program for PostOrder traversal of a Binary Tree. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on windows 10. The program output is also shown below.

advertisement
  1. /* PostOrder Traversal of a given Tree in C++ */
  2. #include<iostream>
  3. using namespace std;
  4. struct node
  5. {
  6.     int info;
  7.     struct node *left, *right;
  8. };
  9. class Tree
  10. {
  11.     public:
  12.         struct node *root;
  13.         struct node *createnode(int key);
  14.         void PostOrder(struct node *root);
  15.         Tree()
  16.         {
  17.             root = NULL;
  18.         }
  19. };
  20. /* Function to create nodes dynamically */
  21. struct node* Tree :: createnode(int key)
  22. {
  23.     struct node *newnode = new node;
  24.     newnode->info = key;
  25.     newnode->left = NULL;
  26.     newnode->right = NULL;
  27.     return(newnode);
  28. }
  29. /* PostOrder Traversal of a Tree */
  30. void Tree :: PostOrder(struct node *root)
  31. {
  32.      if(root != NULL)
  33.      {
  34.           PostOrder(root->left);
  35.           PostOrder(root->right);
  36.           cout<<root->info<<" ";
  37.      }
  38. }
  39. /*
  40.  * Main Function
  41.  */
  42. int main()
  43. {
  44.     Tree t;
  45.     struct node *newnode = t.createnode(25);
  46.     newnode->left = t.createnode(27);
  47.     newnode->right = t.createnode(19);
  48.     newnode->left->left = t.createnode(17);
  49.     newnode->left->right = t.createnode(91);
  50.     newnode->right->left = t.createnode(13);
  51.     newnode->right->right = t.createnode(55);
  52.  
  53.     /* 
  54.      * Sample Tree 1- Balanced Tree
  55.      *
  56.      *              25
  57.      *            /    \
  58.      *          27       19
  59.      *         /  \     /  \
  60.      *        17  91   13  55
  61.      */
  62.     cout<<"Post Order Traversal of  tree 1 is \n";
  63.     t.PostOrder(newnode);
  64.  
  65.     /* Creating second tree */
  66.     struct node *node = t.createnode(1);
  67.     node->right = t.createnode(2);
  68.     node->right->right = t.createnode(3);
  69.     node->right->right->right = t.createnode(4);
  70.     node->right->right->right->right = t.createnode(5);
  71.  
  72.     /* Sample Tree 2-  Unbalanced Tree
  73.      *                1
  74.      *                 \
  75.      *                  2
  76.      *                   \
  77.      *                    3
  78.      *                     \
  79.      *                      4
  80.      *                       \
  81.      *                        5
  82.      */
  83.     cout<<"\n\nPost Order Traversal  of tree 2 is\n" ;
  84.     t.PostOrder(node);
  85.  
  86.     /* Creating Tree 3 having just a single node */
  87.     struct node *root = t.createnode(15);
  88.  
  89.     /* Sample Tree 3- Tree having just one root node.
  90.      *              15              
  91.      */
  92.     cout<<"\n\nPost Order traversal of tree 3 is\n";
  93.     t.PostOrder(root);
  94.     return 0;
  95. }
Program Explanation

1. Check if the current node is empty or null.
2. Traverse the left subtree by recursively calling the PostOrder function.
3. Traverse the right subtree by recursively calling the PostOrder function.
4. Print the info part of the current node.

Runtime Test Cases
Post Order Traversal of  tree 1 is
17 91 27 13 55 19 25
 
Post Order Traversal  of tree 2 is
13 5 4 3 2 1
 
Post Order traversal of tree 3 is
15

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.