![]() |
VOOZH | about |
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.
Preorder traversal is defined as a type of tree traversal that follows the Root-Left-Right policy where:
Inorder traversal is defined as a type of tree traversal technique that follows the Left-Root-Right pattern, such that:
Postorder traversal is defined as a type of tree traversal that follows the Left-Right-Root policy such that for each node:
Below is the Implementation of the above idea:
Pre-order traversal: 1 2 4 5 3 6 In-order traversal: 4 2 5 1 3 6 Post-order traversal: 4 5 2 6 3 1
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 |