Thursday, 23 November 2017

C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree


Code:

# include    iostream
# include    cstdlib
using namespace std;
/*
 * Node Declaration
 */
struct node
{
    int info;
    struct node *left;
    struct node *right;
}*root;

/*
 * Class Declaration
 */
class BST
{
    public:
        void insert(node *, node *);
        void postorder(node *);
        void display(node *, int);
        BST()
        {
            root = NULL;
        }
};
/*
 * Main Contains Menu
 */
int main()
{
    int choice, num;
    BST bst;
    node *temp;
    while (1)
    {
        cout<<"-----------------"<
        cout<<"Operations on BST"<
        cout<<"-----------------"<
        cout<<"1.Insert Element "<
        cout<<"2.Postorder Traversal"<
        cout<<"3.Display"<
        cout<<"4.Quit"<
        cout<<"Enter your choice : ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            temp = new node;
            cout<<"Enter the number to be inserted : ";
            cin>>temp->info;
            bst.insert(root, temp);
            break;
        case 2:
            cout<<"Postorder Traversal of BST:"<
            bst.postorder(root);
            cout<
            break;
        case 3:
            cout<<"Display BST:"<
            bst.display(root,1);
            cout<
            break;
        case 4:
            exit(1);
        default:
            cout<<"Wrong choice"<
        }
    }
}


/*
 * Inserting Element into the Tree
 */
void BST::insert(node *tree, node *newnode)
{
    if (root == NULL)
    {
        root = new node;
        root->info = newnode->info;
        root->left = NULL;
        root->right = NULL;
        cout<<"Root Node is Added"<
        return;
    }
    if (tree->info == newnode->info)
    {
        cout<<"Element already in the tree"<
        return;
    }
    if (tree->info > newnode->info)
    {
        if (tree->left != NULL)
        {
            insert(tree->left, newnode);
        }
        else
        {
            tree->left = newnode;
            (tree->left)->left = NULL;
            (tree->left)->right = NULL;
            cout<<"Node Added To Left"<
            return;
        }
    }
    else
    {
        if (tree->right != NULL)
        {
            insert(tree->right, newnode);
        }
        else
        {
            tree->right = newnode;
            (tree->right)->left = NULL;
            (tree->right)->right = NULL;
            cout<<"Node Added To Right"<
            return;
        }
    }
}

/*
 * Postorder Traversal
 */
void BST::postorder(node *ptr)
{
    if (root == NULL)
    {
        cout<<"Tree is empty"<
        return;
    }
    if (ptr != NULL)
    {
        postorder(ptr->left);
        postorder(ptr->right);
        cout<info<<"  ";
    }
}

/*
 * Display Tree Structure
 */
void BST::display(node *ptr, int level)
{
    int i;
    if (ptr != NULL)
    {
        display(ptr->right, level+1);
        cout<
        if (ptr == root)
            cout<<"Root->:  ";
        else
        {
            for (i = 0;i < level;i++)
                cout<<"       ";
}
        cout<info;
        display(ptr->left, level+1);
    }
}


Output:

-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 6
Root Node is Added
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:

Root->:  6
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 3
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:

Root->:  6
              3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 2
Postorder Traversal of BST:
3  6
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 9
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:

              9
Root->:  6
              3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 2
Postorder Traversal of BST:
3  9  6
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 1
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:

              9
Root->:  6
              3
                     1
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 2
Postorder Traversal of BST:
1  3  9  6
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 7
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:

              9
                     7
Root->:  6
              3
                     1
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 2
Postorder Traversal of BST:
1  3  7  9  6
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 11
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:

                     11
              9
                     7
Root->:  6
              3
                     1
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 2
Postorder Traversal of BST:
1  3  7  11  9  6
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 2
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:

                     11
              9
                     7
Root->:  6
              3
                            2
                     1
-----------------
Operations on BST
-----------------
1.Insert Element
2.Postorder Traversal
3.Display
4.Quit
Enter your choice : 2
Postorder Traversal of BST:
2  1  3  7  11  9  6
-----------------
Operations on BST
-----------------
1.Insert Element
2.Preorder Traversal
3.Display
4.Quit
Enter your choice : 4



More C++ Programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...