Inorder TraversalIn this article, we will discuss the inorder traversal in data structure. If we want to traverse the nodes in ascending order, then we use the inorder traversal. Following are the steps required for the inorder traversal:
Linear data structures such as stack, array, queue, etc., only have one way to traverse the data. But in hierarchical data structures such as tree, there are multiple ways to traverse the data. Here we will discuss another way to traverse the tree data structure, i.e., inorder traversal. There are two approaches used for the inorder traversal:
An inorder traversal technique follows the Left Root Right policy. Here, Left Root Right means that the left subtree of the root node is traversed first, then the root node, and then the right subtree of the root node is traversed. Here, inorder name itself suggests that the root node comes in between the left and the right subtrees. We will discuss the inorder traversal using both recursive and iterative techniques. Let's first start with inorder traversal using recursion. Inorder traversal using recursionExample of inorder traversalNow, let's see an example of inorder traversal. It will be easier to understand the procedure of inorder traversal using an example. ![]() The nodes with yellow color are not visited yet. Now, we will traverse the nodes of the above tree using inorder traversal.
After the completion of inorder traversal, the final output is - {15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70} Complexity of Inorder traversalThe time complexity of Inorder traversal is O(n), where 'n' is the size of binary tree. Whereas, the space complexity of inorder traversal is O(1), if we do not consider the stack size for function calls. Otherwise, the space complexity of inorder traversal is O(h), where 'h' is the height of tree. Implementation of Inorder traversalNow, let's see the implementation of inorder traversal in different programming languages. Program: Write a program to implement inorder traversal in C language. Output ![]() Program: Write a program to implement inorder traversal in C++. Output ![]() Program: Write a program to implement inorder traversal in C#. Output ![]() Program: Write a program to implement inorder traversal in Java. Output ![]() So, that's all about the article. Hope the article will be helpful and informative to you.
Next TopicConvert Infix to Postfix notation
|