C++ Program to Traverse binary tree in Postorder

By | 09.01.2017

Traverse binary tree in Postorder


Write a C++ Program to Traverse binary tree in Postorder. Here’s simple C++ Program to Traverse binary tree in Postorder in C Programming Language.


What is Tree ?


In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.

A tree data structure can be defined as follows…

  • Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.

A tree data structure can also be defined as follows…

  • Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.

Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.


Below is the source code for C++ Program to Traverse binary tree in Postorder which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to Traverse binary tree in Postorder  */

#include<iostream>
#include<stdlib.h>
 
using namespace std;
 
class node
{
public:
class node *left;
class node *right;
int data;
};
 
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
        node *temp,*temp1;
        if(root== NULL)
        {
                root=new node;
                root->data=ch;
                root->left=NULL;
                root->right=NULL;
                return;
        }
        temp1=new node;
        temp1->data=ch;
        temp1->right=temp1->left=NULL;
        temp=search(root,ch);
        if(temp->data>ch)
                temp->left=temp1;
        else
                temp->right=temp1;
 
}
node *search(node *temp,int ch)
{
        if(root== NULL)
        {
                cout <<"no node present";
                return NULL;
        }
        if(temp->left==NULL && temp->right== NULL)
                return temp;
 
        if(temp->data>ch)
             {  if(temp->left==NULL) return temp;
                search(temp->left,ch);}
        else
              { if(temp->right==NULL) return temp;
              search(temp->right,ch);
 
}              }
 
void display(node *temp)
{
        if(temp==NULL)
         return ;
        display(temp->left);
       cout<<temp->data << " ";
        display(temp->right);
}
void postorder( node *root)
{
        node *p;
        p=root;
        top=0;
 
        while(1)
        {
        while(p!=NULL)
        {
                stk[top]=p->data;
                top++;
                if(p->right!=NULL)
                        stk[top++]=-p->right->data;
                p=p->left; 
        }
        while(stk[top-1] > 0 || top==0)
        {
           if(top==0) return;
           cout << stk[top-1] <<" ";
           p=pop(root);
        }
        if(stk[top-1]<0)
        {
          stk[top-1]=-stk[top-1];
          p=pop(root);
      } }
 
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
int main()
{
        tree t1;
        int ch,n,i;

        while(1)
        {
                cout <<"\n1.INSERT\n2.DISPLAY 3.POSTORDER TRAVERSE\n4.EXIT\nEnter your choice:";
                cin >> ch;
                switch(ch)
                {
                case 1:   cout <<"enter no of elements to insert:";
                          cout<<"\n enter the elements";
                          cin >> n;
                          for(i=1;i<=n;i++)
                          {  cin >> ch;
                             t1.insert(ch);
                          }
                           break;
                case 2:   t1.display(t1.root);break;
                case 3:   t1.postorder(t1.root); break;
                case 4:   exit(1);
                }
        }
        
        return 0;
}

OUTPUT : :


/*  C++ Program to Traverse binary tree in Postorder  */

1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to insert:
 enter the elements4
1
2
3
4

1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:2
1 2 3 4 
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:3
4 3 2 1 
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:4
 
Exit code: 1

Above is the source code and output for C++ Program to Traverse binary tree in Postorder which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Category: C++ Programming Data Structures Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments