Data Structure(Tree Traversal)
Tree Traversal:
Traversal of the tree in data structures is a process of visiting each node and prints their value. There are three ways to traverse tree data structure.
- Pre-order Traversal
- In-Order Traversal
- Post-order Traversal
In-Order Traversal
In the in-order traversal, the left subtree is visited first, then the root, and later the right subtree.
Algorithm:
Step 1- Recursively traverse the left subtree
Step 2- Visit root node
Step 3- Recursively traverse right subtree
Pre-Order Traversal
In pre-order traversal, it visits the root node first, then the left subtree, and lastly right subtree.
Algorithm:
Step 1- Visit root node
Step 2- Recursively traverse the left subtree
Step 3- Recursively traverse right subtree
Post-Order Traversal
It visits the left subtree first in post-order traversal, then the right subtree, and finally the root node.
Algorithm:
Step 1- Recursively traverse the left subtree
Step 2- Visit root node
Step 3- Recursively traverse right subtree
Comments
Post a Comment