Open In App

Tree Traversal Techniques

Last Updated : 11 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. In this article, we will discuss all the tree traversal techniques along with their uses.

Tree-Traversal-Techniques-(1)

Tree Traversal

Tree Traversal refers to the process of visiting or accessing each node of the tree exactly once in a certain order. Tree traversal algorithms help us visit and process all the nodes of the tree. Since a tree is not a linear data structure, there can be multiple choices for the next node to be visited. Hence we have many ways to traverse a tree.

Tree-Traversal-Techniques

There are multiple tree traversal techniques that decide the order in which the nodes of the tree are to be visited. These are defined below:

  • Depth First Search or DFS
    • Inorder Traversal
    • Preorder Traversal
    • Postorder Traversal
  • Level Order Traversal or Breadth First Search or BFS

Inorder Traversal

Inorder traversal visits the node in the order: Left -> Root -> Right

Algorithm for Inorder Traversal

Inorder(tree )

● Traverse the left subtree, i.e., call Inorder(left->subtree)
● Visit the root.
● Traverse the right subtree, i.e., call Inorder(right->subtree)


Uses of Inorder Traversal

  • In the case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order.
  • To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder traversal is reversed can be used.
  • Inorder traversal can be used to evaluate arithmetic expressions stored in expression trees.

Refer Inorder Traversal of Binary Tree for more

Preorder Traversal

Preorder traversal visits the node in the order: Root -> Left -> Right

Algorithm for Preorder Traversal

Preorder(tree)

● Visit the root.
● Traverse the left subtree, i.e., call Preorder(left->subtree)
● Traverse the right subtree, i.e., call Preorder(right->subtree)

Uses of Preorder Traversal

  • Preorder traversal is used to create a copy of the tree.
  • Preorder traversal is also used to get prefix expressions on an expression tree.

Refer Preorder Traversal of Binary Tree for more

Postorder Traversal

Postorder traversal visits the node in the order: Left -> Right -> Root

Algorithm for Postorder Traversal:

Postorder(tree)

●Traverse the left subtree, i.e., call Postorder(left->subtree)
● Traverse the right subtree, i.e., call Postorder(right->subtree)
● Visit the root

Uses of Postorder Traversal

  • Postorder traversal is used to delete the tree.
  • Postorder traversal is also useful to get the postfix expression of an expression tree.
  • Postorder traversal can help in garbage collection algorithms, particularly in systems where manual memory management is used.

Refer Postorder Traversal of Binary Tree for more

Level Order Traversal

Level Order Traversal visits all nodes present in the same level completely before visiting the next level.

Algorithm for Level Order Traversal

LevelOrder(tree)

● Create an empty queue Q
● Enqueue the root node of the tree to Q
● Loop while Q is not empty
○Dequeue a node from Q and visit it
○ Enqueue the left child of the dequeued node if it exists
○ Enqueue the right child of the dequeued node if it exists .

Uses of Level Traversal

  1. Level-wise node processing, like finding maximum/minimum at each level.
  2. Tree serialization/deserialization for efficient storage and reconstruction.
  3. Solving problems like calculating the "maximum width of a tree" by processing nodes level by level.

Refer Level Order Traversal (Breadth First Search or BFS) of Binary Tree for more

Other Tree Traversals

  1. Boundary Traversal
  2. Diagonal Traversal

1. Boundary Traversal

Boundary Traversal of a Tree includes

  • left boundary (nodes on left excluding leaf nodes)
  • leaves (consist of only the leaf nodes)
  • right boundary (nodes on right excluding leaf nodes)

Algorithm for Boundary Traversal:

BoundaryTraversal(tree)

● If root is not null:
○ Print root's data
○ PrintLeftBoundary(root->left) // Print the left boundary nodes
○ PrintLeafNodes(root->left) // Print the leaf nodes of left subtree
○ PrintLeafNodes(root->right) // Print the leaf nodes of right subtree
○ PrintRightBoundary(root->right) // Print the right boundary nodes

Uses of Boundary Traversal

  • Boundary traversal helps visualize the outer structure of a binary tree, providing insights into its shape and boundaries.
  • Boundary traversal provides a way to access and modify these nodes, enabling operations such as pruning or repositioning of boundary nodes.

Refer Boundary Traversal of binary tree for more

2. Diagonal Traversal

In the Diagonal Traversal of a Tree, all the nodes in a single diagonal will be printed one by one.

Algorithm for Diagonal Traversal

DiagonalTraversal(tree):

● If root is not null:
○ Create an empty map
○ DiagonalTraversalUtil(root, 0, M) // Call helper function with initial diagonal level 0
○ For each key-value pair (diagonalLevel, nodes) in M:
■ For each node in nodes:
■ Print node's data

DiagonalTraversalUtil(node, diagonalLevel, M):

● If node is null
● Return
● If diagonalLevel is not present in M:
○ Create a new list in M for diagonalLevel
● Append node's data to the list at M[diagonalLevel]
● DiagonalTraversalUtil(node->left, diagonalLevel + 1, M) // Traverse left child with increased diagonal level
● DiagonalTraversalUtil(node->right, diagonalLevel, M) // Traverse right child with same diagonal level

Uses of Diagonal Traversal

  • Diagonal traversal helps in visualizing the hierarchical structure of binary trees, particularly in tree-based data structures like binary search trees (BSTs) and heap trees.
  • Diagonal traversal can be utilized to calculate path sums along diagonals in a binary tree.

Refer Diagonal Traversal of Binary Tree for more

Some other important Tutorials:


Next Article

Similar Reads