Inorder Tree Traversal without recursion and without stack!
Last Updated :
16 Jun, 2023
Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree.
1. Initialize current as root
2. While current is not NULL
If the current does not have left child
a) Print current’s data
b) Go to the right, i.e., current = current->right
Else
a) Find rightmost node in current left subtree OR
node whose right child == current.
If we found right child == current
a) Update the right child as NULL of that node whose right child is current
b) Print current’s data
c) Go to the right, i.e. current = current->right
Else
a) Make current as the right child of that rightmost
node we found; and
b) Go to this left child, i.e., current = current->left
Although the tree is modified through the traversal, it is reverted back to its original shape after the completion. Unlike Stack based traversal, no extra space is required for this traversal.
C++
#include <bits/stdc++.h>
using namespace std;
struct tNode {
int data;
struct tNode* left;
struct tNode* right;
};
void MorrisTraversal( struct tNode* root)
{
struct tNode *current, *pre;
if (root == NULL)
return ;
current = root;
while (current != NULL) {
if (current->left == NULL) {
cout << current->data << " " ;
current = current->right;
}
else {
pre = current->left;
while (pre->right != NULL
&& pre->right != current)
pre = pre->right;
if (pre->right == NULL) {
pre->right = current;
current = current->left;
}
else {
pre->right = NULL;
cout << current->data << " " ;
current = current->right;
}
}
}
}
struct tNode* newtNode( int data)
{
struct tNode* node = new tNode;
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int main()
{
struct tNode* root = newtNode(1);
root->left = newtNode(2);
root->right = newtNode(3);
root->left->left = newtNode(4);
root->left->right = newtNode(5);
MorrisTraversal(root);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct tNode {
int data;
struct tNode* left;
struct tNode* right;
}tNode;
void MorrisTraversal(tNode* root)
{
tNode *current, *pre;
if (root == NULL)
return ;
current = root;
while (current != NULL) {
if (current->left == NULL) {
printf ( "%d " , current->data);
current = current->right;
}
else {
pre = current->left;
while (pre->right != NULL
&& pre->right != current)
pre = pre->right;
if (pre->right == NULL) {
pre->right = current;
current = current->left;
}
else {
pre->right = NULL;
printf ( "%d " , current->data);
current = current->right;
}
}
}
}
tNode* newtNode( int data)
{
tNode* node = (tNode *) malloc ( sizeof (tNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int main()
{
tNode* root = newtNode(1);
root->left = newtNode(2);
root->right = newtNode(3);
root->left->left = newtNode(4);
root->left->right = newtNode(5);
MorrisTraversal(root);
return 0;
}
|
Java
class tNode {
int data;
tNode left, right;
tNode( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree {
tNode root;
void MorrisTraversal(tNode root)
{
tNode current, pre;
if (root == null )
return ;
current = root;
while (current != null )
{
if (current.left == null )
{
System.out.print(current.data + " " );
current = current.right;
}
else {
pre = current.left;
while (pre.right != null
&& pre.right != current)
pre = pre.right;
if (pre.right == null ) {
pre.right = current;
current = current.left;
}
else
{
pre.right = null ;
System.out.print(current.data + " " );
current = current.right;
}
}
}
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new tNode( 1 );
tree.root.left = new tNode( 2 );
tree.root.right = new tNode( 3 );
tree.root.left.left = new tNode( 4 );
tree.root.left.right = new tNode( 5 );
tree.MorrisTraversal(tree.root);
}
}
|
Python3
class TreeNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def morris_traversal(root):
current = root
while current:
if current.left is None :
print (current.data, end = " " )
current = current.right
else :
pre = current.left
while pre.right and pre.right ! = current:
pre = pre.right
if pre.right is None :
pre.right = current
current = current.left
else :
pre.right = None
print (current.data, end = " " )
current = current.right
if __name__ = = '__main__' :
root = TreeNode( 1 )
root.left = TreeNode( 2 )
root.right = TreeNode( 3 )
root.left.left = TreeNode( 4 )
root.left.right = TreeNode( 5 )
morris_traversal(root)
|
C#
using System;
class BinaryTree {
tNode root;
public class tNode {
public int data;
public tNode left, right;
public tNode( int item)
{
data = item;
left = right = null ;
}
}
void MorrisTraversal(tNode root)
{
tNode current, pre;
if (root == null )
return ;
current = root;
while (current != null )
{
if (current.left == null )
{
Console.Write(current.data + " " );
current = current.right;
}
else {
pre = current.left;
while (pre.right != null
&& pre.right != current)
pre = pre.right;
if (pre.right == null )
{
pre.right = current;
current = current.left;
}
else
{
pre.right = null ;
Console.Write(current.data + " " );
current = current.right;
}
}
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new tNode(1);
tree.root.left = new tNode(2);
tree.root.right = new tNode(3);
tree.root.left.left = new tNode(4);
tree.root.left.right = new tNode(5);
tree.MorrisTraversal(tree.root);
}
}
|
Javascript
<script>
class tNode
{
constructor(item)
{
this .data = item;
this .left = this .right = null ;
}
}
let root;
function MorrisTraversal(root)
{
let current, pre;
if (root == null )
return ;
current = root;
while (current != null )
{
if (current.left == null )
{
document.write(current.data + " " );
current = current.right;
}
else {
pre = current.left;
while (pre.right != null
&& pre.right != current)
pre = pre.right;
if (pre.right == null ) {
pre.right = current;
current = current.left;
}
else
{
pre.right = null ;
document.write(current.data + " " );
current = current.right;
}
}
}
}
root = new tNode(1);
root.left = new tNode(2);
root.right = new tNode(3);
root.left.left = new tNode(4);
root.left.right = new tNode(5);
MorrisTraversal(root);
</script>
|
Time Complexity: O(n) If we take a closer look, we can notice that every edge of the tree is traversed at most three times. And in the worst case, the same number of extra edges (as input tree) are created and removed.
Auxiliary Space: O(1) since using only constant variables
References:
www.liacs.nl/~deutz/DS/september28.pdf
www.scss.tcd.ie/disciplines/software_systems/…/HughGibbonsSlides.pdf
Please write comments if you find any bug in above code/algorithm, or want to share more information about stack Morris Inorder Tree Traversal.
Please Login to comment...