0
votes

There are preorder, inorder and postorder traversal for a binary tree, but no matter what order, it just traverses the tree to find a matched path. Is there any use case where I have to use any of the orders? Or are they just different ways but no difference regarding practical usage? Thanks.

1

1 Answers

2
votes

There is definite practical usage with these traversals.

There are few specific use cases as below : By using In-order traversal, you can get sorted node values if your requirement needs sorted information.. By using Pre-order traversal, you can create a copy of the tree and also can be used to get prefix expression of an expression tree. Postorder traversal is used to delete the tree and also can be useful to get postfix expression of an expression tree.

The appropriate traversal technique shall be used based on which nodes should be fetched first for the requirement / design in hand. In case, if your requirement requires roots to be processed /picked / analyzed before picking up leaf nodes then pre-order traversal shall be helpful. Else, if leaf nodes have to be processed / fetched / analyzed before root nodes, then post-order shall be helpful.