# Inorder Tree Traversal without Stack

In any data structure, traversal is an important operation. In the traversal operation, we walk through each element of the data structure at least once. The traversal operation plays a very important role in various other operations on the data structure like searching. We need to visit each element of the data structure atleast once to compare each incoming element to the key that we want to find in the data structure.

Like any other data structure, the tree data also needs to be traversed to access each element, known as a node of the tree data structure. There are different ways of traversing a tree depending upon the order in which the tree's nodes are visited and the types of data structure used for traversing the tree.

There are various data structures involved in traversing a tree, as traversing a tree involves iterating over all nodes in some manner. As from a given node, there could be more than one way to traverse or visit the next node of the tree, so it becomes important to store one of the nodes traverses further and store the rest of the nodes having a possible path for backtracking the tree if needed.

As we know, backtracking is not a linear approach, so we need different data structures for traversing through the whole tree.

## Types of Tree Traversal

The stack and queue are the major data structure that is used for traversing a tree. So depending upon the order in which the nodes of the tree are visited, the different types of tree traversal are:

1. Depth First Search Tree Traversal
• Pre-order Tree Traversal.
• Post-order Tree Traversal.
• In-order Tree Traversal.
• Reverse Pre-order Tree Traversal.
• Reverse Post-order Tree Traversal.
• Reverse In-order Tree Traversal.

Now, we will be focusing on the in-order tree traversal type. All three types; in-order tree traversal, pre-order tree traversal and post-order tree traversal, have the difference of visiting the tree's root node, such as:

• In pre-order tree traversal, the tree's root node is visited first, followed by the left node (or subtree) and right node (or subtree).
• In in-order tree traversal, the left node (or subtree) is visited first, followed by the root node and then the right node (or subtree).
• And in the post-order tree traversal, the left node (or subtree) is visited first, followed by the right node (or subtree) and the root node of the tree is visited in the last.

## In-order Tree Traversal

In-order traversal can be defined as the traversal that will process all tree nodes by recursively processing the left subtree, then processing the root, and finally the right subtree.

For example, let us write an in-order traversal for the tree given below. The above tree has seven nodes in it. Node A is the tree's root node, having three nodes in its left and right subtrees, respectively.

• According to the in-order tree traversal definition, the left subtree should be visited first, and in the left subtree, the deepest left node is node D.
• Then the root of this subtree is visited node B (the left subtree's root node).
• In the last, the rightmost node of the binary tree, node E, is visited, so now our left subtree is completed traversed, we will visit the root node A, and in the last, the right subtree is traversed.
• Similarly, in the right subtree, we first need to traverse the leftmost node, node F and then visit the root node of the right subtree, node C. In the end, the rightmost node of the right subtree is visited that is node G.
• So, the in-order tree traversal of the above tree is:

The stack data structure is mostly used for the in-order traversal of a tree, but in this article, we will see the recursive way to traverse the tree.

### C++ Code:

Let's write a C++ code for in-order tree traversal of the tree using recursion.

Output: The output of the above code is:

```The inorder traversal of the tree with recursion is:
40 20 10 70 50 80 30 60
```

In the above C++ code, we have used recursion instead of the stack data structure to traverse the tree. For the traversal of the tree, a recursive function named 'inorder' is created that will be called recursively to traverse through the tree. This recursive function will be called for each of the subtrees of the whole tree.

In each subtree, this function will first print the leftmost leaf child node of the tree. After printing the leftmost leaf child node, it will print the tree's root node, and then in the last, the rightmost child leaf node is printed and called again for another subtree. So, all tree nodes are traversed successfully by giving recursive calls to the inorder() function.

### Java Code:

Now let's write a code in Java to create a tree and print the elements of the tree in the in-order tree traversal manner.

Output: The output of the above code is:

```The inorder traversal of the tree with recursion is:
40 20 10 70 50 80 30 60
```

In the above Java code, we have used recursion instead of the stack data structure to traverse the tree. For the traversal of the tree, a recursive function named 'inorder' is created that will be called recursively to traverse through the tree. This recursive function will be called for each of the subtrees of the whole tree.

In each subtree, according to the definition of the in-order tree traversal, this function will first print the leftmost leaf child node of the tree. After printing the leftmost leaf child node, it will print the tree's root node, and then in the last, the rightmost child leaf node is printed and called again for another subtree. So, all tree nodes are traversed successfully by giving recursive calls to the inorder() function.

### Feedback   