Explain the Tree Traversing operations | Data Structure Tutorial
Explain the Tree Traversing operations | Data Structure Tutorial
In this article, you will understand different types of tree traversal techniques.
Tree Traversal means visiting every node of the tree one by one. For example, you want to find the smallest elements after checking every value one by one. In linear data structures such as arrays, stacks, queues, and linked lists, there is only one way to read the data. The tree can be traversed in various ways because it is a hierarchical data structure.
Let’s take an example, of how to read the elements of the tree:
- Starting from the top, left to right
- Starting from the button, left to right
This process is not giving respect to the hierarchy of the tree, only finding out the depth of the nodes.
From this, we can know, each tree is a combination of:
- A node carrying data
- Two Subtrees
We have to visit each node of the tree, so we need to visit every node in the subtree, visit the root node and go through all the nodes in the right subtree as well.
Inorder Traversal
- Traverse all the nodes in the left subtree.
- Then traverse to the root node.
- Visit all the nodes one by one in the right subtree.
inorder(root -> left)
display(root -> data)
inorder(root -> right)
Preorder Traversal
- Traverse the root node.
- Traverse all the nodes one by one in the left subtree.
- Traverse all the nodes one by one in the right subtree.
display(root -> data)
preorder(root -> left)
preorder(root -> right)
Postorder Traversal
- Visit all the left subtree nodes one by one.
- Visit all the right subtree nodes one by one.
- Visit the root node
postorder(root -> left)
postorder(root -> right)
display(root -> data)


