0
5.2kviews
Construct the binary tree for the inorder and post order traversal sequence given below: -

Inorder “INFORMATION”

Postorder “INOFMAINOTR”

Write a function to traverse a tree in postOrder

1 Answer
0
55views

Binary tree construction:

Since, R is the last element in the post order sequence, R will be the root.

Elements to the left of R will be a part of left sub tree and the elements to the right of R will be a part of right sub tree.

Further, F is the last element of post Order sequence in the left sub tree.

Hence, F will be the root element in this case.

Elements before F i.e. I and N will be a part of left sub tree and O will be a part of right sub tree.

This process can be continued for the remaining elements to obtain the resultant binary tree.

The steps mentioned above can be represented graphically as follows:

enter image description here

enter image description here

C++ function to traverse a tree in postOrder

voidBinarySearchTree::postOrder(node *ptr)
{
if (root == NULL)
        {
    cout<<"Tree is empty."<<endl;
    return;
        }
if (ptr != NULL)
        {
    postOrder(ptr->left);
    postOrder(ptr->right);
    cout<<ptr->info<<"  ";
        }
}
Please log in to add an answer.