Preorder vs Inorder vs Postorder
Attribute | Pre-order | In-order | Post-order |
---|---|---|---|
Visit Order | Root, Left Subtree, Right Subtree | Left Subtree, Root, Right Subtree | Left Subtree, Right Subtree, Root |
Sorted Order (BST) | No | Yes | Yes |
Expression Tree | Prefix (Polish) Notation | Infix (Standard) Notation | Postfix (Reverse Polish) Notation |
Evaluation Order | Operators come before operands | Operators come between operands | Operators come after operands |
Memory Usage | No additional memory for tracking traversal, stack space for recursion/iteration | No additional memory for tracking traversal, stack space for recursion/iteration | No additional memory for tracking traversal, stack space for recursion/iteration |
Left-Right Symmetry | Not symmetric | Symmetric | Not symmetric |
Application | Constructing expression trees, cloning/copying trees, prefix notation | In-order traversal of BST for sorted order, expression evaluation | Deleting nodes in binary trees, post-fix notation, expression evaluation |
Performance | Better for copying/cloning trees, prefix notation | Suitable for sorting BST elements, expression evaluation | Suitable for deleting nodes, post-fix notation, expression evaluation |
Tree Modification | Easy to copy/cloning, preserving original structure | Not ideal for modification | Difficult for modification, requires reversing |
Preorder vs Inorder vs Postorder
In Preorder Traversal, the root node is visited first, followed by the left and right subtrees. Inorder Traversal starts with the left subtree, visits the root, and then the right subtree, often used in binary search trees. Postorder Traversal begins with the left subtree, moves to the right subtree, and visits the root last, useful for deleting nodes. In this article, we will discuss the Tree Traversal Techniques used in data structure and algorithms with their differences.