×

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

Understanding Data Processing

Introduction Data In our everyday lives, any task that we perform online is related to data. Millions of pieces of data are produced every second across the globe. Data production is largely...

4 minutes read.

Delete nodes from the linked list which have a greater value on the right side

Delete nodes from the linked list which have a greater value on the right side In this problem, we have given a singly linked list, and we need to remove all...

3 minutes read.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 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.

Interval Tree

Interval Tree Interval Tree: The concept is to increase a Binary Search Tree self-balancing such as Red Black Tree, and AVL Tree, so that every feature can be completed in time O(Logn). Each Interval...

4 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

Types of Linked list

Single linked list  A single linked list is a linked list in which all nodes are connected with each other in sequence. Each node of a singly linked list has two...

7 minutes read.

What Is Dfs Algorithm in Data Structures

DFS stands for Depth First Search. Generally, it is a repetitive or decidable type of algorithm which is basically used in identifying all the vertices or nodes of a graph...

5 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

Deletion in Binary Search Tree

Implementation #include <iostream> using namespace std; struct _nod {   int ky;   struct _nod *Lft, *Rt; }; // Creating a node in the binary tree. struct _nod *nw_nod(int Itm) {   struct _nod *temp = (struct _nod *)malloc(sizeof(struct...

4 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

4 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

3 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.