Inorder Tree Traversal without StackIn 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 TraversalThe 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:
Now, we will be focusing on the inorder tree traversal type. All three types; inorder tree traversal, preorder tree traversal and postorder tree traversal, have the difference of visiting the tree's root node, such as:
Inorder Tree TraversalInorder 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 inorder 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.
The stack data structure is mostly used for the inorder 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 inorder 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 inorder 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 inorder 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.
Next TopicEnumeration of Binary Trees
