Tree traversal is a process of visiting all the nodes of a tree in a systematic order.
‣ There are three standard methods to traverse the binary trees.
These are as follows:
- Preorder Traversal
- Inorder Traversal
- Postorder Traversal
Preorder Traversal:
Preorder traversal is a tree traversal algorithm that visits the root node of a tree first, followed by the nodes in the left subtree in preorder, and then the nodes in the right subtree in preorder.
Steps:
- If root =NULL, return
- Visit the root
- Traverse the left subtree in preorder
- Traverse the right subtree in preorder
Inorder Traversal:
Inorder traversal is a tree traversal algorithm that visits the nodes in the left subtree of a tree in inorder, followed by the root node, and then the nodes in the right subtree in inorder.
Steps:
- If root=NULL, retrurn
- Traverse the left subtree in inorder
- Visit the root
- Traverse the right subtree in inorder
Postorder Traversal:
Postorder traversal is a tree traversal algorithm that visits the nodes in the left subtree of a tree in postorder, followed by the nodes in the right subtree in postorder, and then the root node.
Steps:
- If root=NULL, return
- Traverse the left subtree in postorder
- Traverse the right subtree in postorder
- Visit the root
Q). Determine the preorder, postorder and inorder traversal of the binary tree as shown in fig: