Nikunj Banka
Nikunj Banka

Reputation: 11365

Binary Tree : Advantages of pre-order ,post-order traversals in Binary Tree?

Inorder traversal of a Binary Search Tree yields nodes in increasing order. But what advantages do pre order and post order traversals have on any binary tree?

EDIT:What I mean by advantages is : "any situation where applying pre-order or post-order traversal is specifically suited".

Upvotes: 4

Views: 2692

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Not all binary trees have numbers in them. You can use a binary tree to represent things that exhibit tree structure, such as expressions. For example, 2 * 3 + 4 can be represented as

              +
            /   \
           *     4
         /   \
        2     3

If you represent an expression like that, the in-order traversal would yield your "normal" infix notation of

2 * 3 + 4

but a post-order traversal would yield a Reverse Polish Notation of the expression:

2 3 * 4 +

Upvotes: 6

Related Questions