Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postorder Traversal

In-order tree traversal obviously has application; getting the contents in order.

Preorder traversal seems really useful for creating a copy of the tree.

Is there a common use for postorder traversal of a binary tree?

like image 680
Dean J Avatar asked Jul 09 '10 20:07

Dean J


People also ask

What is Postorder traversal?

(algorithm) Definition: Process all nodes of a tree by recursively processing all subtrees, then finally processing the root. Also known as postfix traversal.

What is Postorder traversal of above tree?

Explanation: In postorder traversal the left subtree is traversed first and then the right subtree and then the current node. So, the posturer traversal of the tree is, S W T Q X U V R P.

What is preorder and postorder traversal?

For Preorder, you traverse from the root to the left subtree then to the right subtree. For Post order, you traverse from the left subtree to the right subtree then to the root.


1 Answers

Let me add another one:

Postorder traversal is also useful in deleting a tree. In order to free up allocated memory of all nodes in a tree, the nodes must be deleted in the order where the current node can only be deleted when both of its left and right subtrees are deleted.

Postorder does exactly just that. It processes both of the left and right subtrees before processing the current node.

like image 99
1337c0d3r Avatar answered Sep 27 '22 17:09

1337c0d3r