Loading, please wait...

A to Z Full Forms and Acronyms

Explain the Tree Traversing operations | Data Structure Tutorial

Nov 28, 2022 #DataStructureTutorial, 1946 Views
In this article, you will understand different types of tree traversal techniques. 

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

  1. Traverse all the nodes in the left subtree.
  2. Then traverse to the root node.
  3. Visit all the nodes one by one in the right subtree.

inorder(root -> left)

display(root -> data)

inorder(root -> right)

Preorder Traversal

  1. Traverse the root node.
  2. Traverse all the nodes one by one in the left subtree.
  3. Traverse all the nodes one by one in the right subtree.

display(root -> data)

preorder(root -> left)

preorder(root -> right)

Postorder Traversal

  1. Visit all the left subtree nodes one by one.
  2. Visit all the right subtree nodes one by one.
  3. Visit the root node

postorder(root -> left)

postorder(root -> right)

display(root -> data)

A to Z Full Forms and Acronyms