原文地址:Inorder Tree Traversal without recursion and without stack!
用Morris Traversal,我们可以不用递归和栈遍历树。Morris Traversal的思想是基于线索二叉树的。在这个遍历中,我们首先建立到中序先驱的连接,并用这些连接打印数据,最后恢复变化,还原出树的原貌。
1. 初始化current为root
2. While current不为NULL
If current没有左孩子
a) 打印current的值
b) 跳到right,比如:current = current->right
Else
a) 使current作为在current的左子树中最右节点的右孩子
b) 跳到left孩子,比如:current = current->left
尽管遍历的过程中改变了树,但是活干完以后原形状的树还是会被还原出来的。不像基于栈的遍历,这种遍历不需要额外的空间。
/* A binary tree tNode has data, pointer to left child
and a pointer to right child */
class tNode
{
int data;
tNode left, right;
tNode(int item)
{
data = item;
left = right = null;
}
}
class BinaryTree
{
tNode root;
/* Function to traverse binary tree without recursion and
without stack */
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
{
/* Find the inorder predecessor of current */
pre = current.left;
while (pre.right != null && pre.right != current)
pre = pre.right;
/* Make current as right child of its inorder predecessor */
if (pre.right == null)
{
pre.right = current;
current = current.left;
}
/* Revert the changes made in if part to restore the
original tree i.e.,fix the right child of predecssor*/
else
{
pre.right = null;
System.out.print(current.data + " ");
current = current.right;
} /* End of if condition pre->right == NULL */
} /* End of if condition current->left == NULL*/
} /* End of while */
}
public static void main(String args[])
{
/* Constructed binary tree is
1
/ \
2 3
/ \
4 5
*/
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);
}
}
// This code has been contributed by Mayank Jaiswal(mayank_24)
输出:
4 2 5 1 3
参考文献:
www.liacs.nl/~deutz/DS/september28.pdf
http://comsci.liu.edu/~murali/algo/Morris.htm
www.scss.tcd.ie/disciplines/software_systems/…/HughGibbonsSlides.pdf