written 8.6 years ago by | • modified 5.1 years ago |
Inorder “INFORMATION”
Postorder “INOFMAINOTR”
Write a function to traverse a tree in postOrder
written 8.6 years ago by | • modified 5.1 years ago |
Inorder “INFORMATION”
Postorder “INOFMAINOTR”
Write a function to traverse a tree in postOrder
written 8.6 years ago by |
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:
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<<" ";
}
}