Tree Traversal
Tree Traversal
Handwritten Notes- Click Here
A tree is a non-linear data structure in which elements are arranged in a hierarchical structure.
Tree Traversal is the process of visiting each node of a tree exactly once in a particular order.
The main types of tree traversal are:
- Preorder Traversal (Root → Left → Right)
- Inorder Traversal (Left → Root → Right)
- Postorder Traversal (Left → Right → Root)
Preorder Traversal :
In Preorder Traversal, the root node is visited first, followed by the left subtree and then the right subtree.
Example
A
/ \
B C
/ \
D EThe Preorder Traversal is: A → B → D → E → C
Inorder Traversal:
In Inorder Traversal, the left subtree is visited first, followed by the root node and then the right subtree.
Example
A
/ \
B C
/ \
D EThe Inorder Traversal is: D → B → E → A → C
Postorder Traversal:
In Postorder Traversal, the left subtree is visited first, followed by the right subtree, and the root node is visited last.
Example
A
/ \
B C
/ \
D EThe Postorder Traversal is: D → E → B → C → A
Comments
Post a Comment