×

Binary Tree Inorder Traversal

The binary tree is a type of tree in which each and every node has atleast two children except the leaf nodes. We have various operations in the binary tree, and all of them have their own functions as well as implementations. These operations are insertion, deletion, traversal and many more. In this article, we are briefly going to discuss the various different methods of traversal. The traversal operation has many of its own approaches that are inorder, postorder and preorder traversal.

Inorder traversal

When we have a binary tree, and we want to explore and traverse that tree in ascending order, then we usually use this kind of approach, which is the inorder traversal. When we talk about linear data structures, which are linked lists, arrays, stacks, and queues, in those data structures we can only explore or traverse in one direction. Still, in non-linear data structures, such as the tree and graphs, there are several different ways in which we can traverse or explore the data. Here, we will discuss another way of traversing the data structure, which is the inorder traversal.

The inorder traversal method generally hovers over the basic principle of left root right policy. In this policy, the left root right implies that the left subtree of the current root node is visited first; when we start traversing, we visit the root node, then we commute to the right subtree, and its root node is visited. Here, the name itself suggests that the traversal or exploration occurs between the left and right subtrees.

Mainly there are two given approaches or ways in which we can simply use for the traversal in the tree. They are: -

  1. Inorder traversal using recursion
  2. Inorder traversal using an iterative method

Now we will describe each of them in detail: -

Inorder traversal using recursion

In this type of inorder traversal, we first have to process and configure all the given nodes in the left subtree, and then we have to store and keep them intact in the root node. After that, when we have processed and configured all the nodes present in the right subtree.

Inorder traversal using an iterative method

The iterative traversal of the tree is done by using the stack data structure and is one of the best methods to traverse a tree. We first have to initialize the stack and then push the current node onto the stack.

Algorithm for ignored traversal

  1. In this, firstly, we have to visit all the nodes and vertices that are situated in the left subtree.
  2. Then we have to commute to the root node.
  3. After finishing that, we will visit all the nodes that are present in the right subtree.
inorder(root->left)
display(root->data)
inorder(root->right)

Implementation of inorder traversal using recursive method

#include <iostream>
using namespace std;
struct Node
{
    int data;
    Node *lft, *rt;
 
    Node(int info)
    {
        this->info = info;
        this->lft = this->rt = nullpointr;
    }
};
 
// We will use a recursive function to perform the mechanism of inorder traversal on the tree. 
void inorder(Nod* root)
{
    // return if found out the current node is clear and 
    if (root == nullpointr) {
        return;
    }
 
    // We have to travel to the left subtree 
    inorder(root->lft);
 
    //We will next showcase the information part of the root(or current node). 
    cout << root->info << " ";
 
    // We have to travel to the right subtree 
    inorder(root->rt);
}
 
int main()
{
    /* Build the following tree
               1
             /   \
            /     \
           2       3
          /      /   \
         /      /     \
        4      5       6
              / \
             /   \
            7     8
    */
 
    Nod* root = new Nod(1);
    root->lft = new Nod(2);
    root->rt = new Nod(3);
    root->lft->lft = new Nod(4);
    root->rt->lft = new Nod(5);
    root->rt->rt = new Nod(6);
    root->rt->lft->lft = new Nod(7);
    root->rt->lft->rt = new Nod(8);
 
    inorder(root);
 
    return 0;
}

Output:

BINARY TREE INORDER TRAVERSAL

Implementation of inorder traversal using the iterative method

#include <iostream>
#include <stack>
using namespace std;
 struct Node
{
    int info;
    Nod *lft, *rt;
 
    Nod(int info)
    {
        this->info = info;
        this->lft = this->rt = nullpointr;
    }
};
 
// We will use an Iterative function to perform the mechanism of inorder traversal on the tree. 
 void inorderIterative(Nod* root)
{
    // We will first have to create a vacant stack which is completely empty
    stack<Nod*> stack;
 
    // We have to begin from the root node and then initialize the current node to the root node 
    Nod* current = root;
 
    // By any chance, if the current node which is provided to us is zero and the stack is also vacant, then we are done
    while (!stack.empty() || current!= nullpointr)
    {
        // when we find the current node, occupy it and press it back into the given stack and then shift it to the left node.
        if (current!= nullpointr)
        {
            stack.push(current);
            current = current->lft;
        }
        else {
            // In case the current node that we have is empty; then we have to pop an element from the allotted stack. 
            // Set it to print the result
            current = stack.top();
            stack.pop();
            cout << current->info << " ";
 
            curr = current->rt;
        }
    }
}
 
int main()
{
    /* Build the following tree
               1
             /   \
            /     \
           2       3
          /      /   \
         /      /     \
        4      5       6
              / \
             /   \
            7     8
    */
 
    Nod* root = new Nod(1);
    root->lft = new Nod(2);
    root->rt = new Nod(3);
    root->lft->lft = new Nod(4);
    root->rt->lft = new Nod(5);
    root->rt->rt = new Nod(6);
    root->rt->lt->lt = new Nod(7);
    root->rt->lft->rt = new Nod(8);
 
    inorderIterative(root);
 
    return 0;
}

Output:

BINARY TREE INORDER TRAVERSAL

Related Topics

Object-Oriented Analysis and Design

While designing a system, one should know all the requirements or needs of the plan beforehand, and to do so, we should use a systematic approach to analyze the goal...

3 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.

Preorder Traversal of Binary Trees

In general, Stack, Array, Queue, and other linear data structures only have one way to traverse the data. However, there are numerous ways to traverse through the data in a hierarchical...

3 minutes read.

Finding the Minimum and Maximum Value of a Binary Tree

Implementation // Writing a C++ program that will help us find out the maximum and the minimum in a binary tree.  #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new class tree node. class...

5 minutes read.

Bottom view of the binary tree

The bottom of the binary tree is generally defined as the number of nods present in the bottom-most part of the tree. In this article, we will see the implementation...

3 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

Merge Sort

Merge Sort is one of the most widely used sorting algorithms, and it is based on the Divide and Conquer principle. A problem is subdivided into multiple sub-problems in this method....

8 minutes read.

What is B tree?

What do you mean by B Tree in Data Structures? In the technological world, a B tree is simply a well-managed and coordinated tree and an integral part of the data...

6 minutes read.

Application of Stack in Data Structures

In this article, we will discuss all the different applications of stack. What is meant by stack? The stack is a non-primitive linear data structure in which the insertion of the new...

11 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Spanning Tree

Spanning Tree: The spanning tree is a subset of the graph. It is a non-cyclic graph. If any node in the spanning tree is truncated, the entire graph fails. There are...

10 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 minutes read.

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

4 minutes read.