- In-order Traversal
- Pre-order Traversal
- Post-order Traversal
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself.If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order.
![In Order Traversal](https://www.tutorialspoint.com/data_structures_algorithms/images/inorder_traversal.jpg)
D → B → E → A → F → C → G
Algorithm
Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Visit root node. Step 3 − Recursively traverse right subtree.
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.![Pre Order Traversal](https://www.tutorialspoint.com/data_structures_algorithms/images/preorder_traversal.jpg)
A → B → D → E → C → F → G
Algorithm
Until all nodes are traversed − Step 1 − Visit root node. Step 2 − Recursively traverse left subtree. Step 3 − Recursively traverse right subtree.
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node.![Post Order Traversal](https://www.tutorialspoint.com/data_structures_algorithms/images/postorder_traversal.jpg)
D → E → B → F → G → C → A
Algorithm
Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Recursively traverse right subtree. Step 3 − Visit root node.
No comments:
Post a Comment