Morris Traversal for Inorder in JavaIn this section, we will learn about the Morris traversal for inorder in Java. In Morris traversal, we do the traversal of a tree without the help of recursion or stack. The Morris traversal is based on the threaded binary tree. In this traversal, we do the internal modification in order to create the internal links for the inorder successor. Eventually, we revert the modification so that the original tree is restored. Morris Traversal Inorder AlgorithmThe following are the steps involved in the Morris Traversal (Inorder).
ImplementationLet's see the implementation of the Morris traversal for the inorder using the algorithms described above. FileName: BTree.java Output: The inorder traversal of the binary tree is: 1 8 4 6 9 7 Time Complexity: We observe that every edge of the tree is traversed at most 3 times. The same number of extra edges, as in the input tree, is removed and created. Thus, the total time complexity of the above program is O(n). Note: At many places, it is said that in recursive approach for doing the inorder traversal does not consume any space. However, this is not true. In recursion, a stack is used, even though we do not provide the stack explicitly.The Morris traversal can work with the help of the internal modification done in the binary tree. Without internal modification, the Morris traversal cannot work. Next TopicMorris Traversal for Preorder in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India