在不使用堆栈或递归的情况下解释Morris有序树遍历


125

有人可以在不使用堆栈或递归的情况下帮助我了解以下Morris有序树遍历算法吗?我试图理解它是如何工作的,但是它只是在逃避我。

 1. Initialize current as root
 2. While current is not NULL
  If current does not have left child     
   a. Print currents data
   b. Go to the right, i.e., current = current->right
  Else
   a. In current's left subtree, make current the right child of the rightmost node
   b. Go to this left child, i.e., current = current->left

我知道树的修改方式current noderight child,将max nodein 做成in,right subtree并使用此属性进行有序遍历。但是除此之外,我迷路了。

编辑:找到了此随附的c ++代码。我很难理解修改后的树是如何还原的。魔术在于else子句,一旦修改了正确的叶子,该子句就会被击中。有关详细信息,请参见代码:

/* Function to traverse binary tree without recursion and
   without stack */
void MorrisTraversal(struct tNode *root)
{
  struct tNode *current,*pre;

  if(root == NULL)
     return; 

  current = root;
  while(current != NULL)
  {
    if(current->left == NULL)
    {
      printf(" %d ", 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;
      }

     // MAGIC OF RESTORING the Tree happens here: 
      /* Revert the changes made in if part to restore the original
        tree i.e., fix the right child of predecssor */
      else
      {
        pre->right = NULL;
        printf(" %d ",current->data);
        current = current->right;
      } /* End of if condition pre->right == NULL */
    } /* End of if condition current->left == NULL*/
  } /* End of while */
}

12
我以前从未听说过这种算法。相当优雅!
弗雷德·富

5
我认为指出伪代码+代码的来源(大概)可能有用。
伯恩哈德·巴克


在上面的代码中,不需要以下行: pre->right = NULL;
prashant.kr.mod,

Answers:


155

如果我没看错算法,这应该是其工作方式的一个示例:

     X
   /   \
  Y     Z
 / \   / \
A   B C   D

首先,X是根,因此将其初始化为currentX有一个左孩子,所以X它成为X左子树的最右孩子- X有序遍历的直接前任。这样X就做成了合适的孩子B,然后current设置为Y。树现在看起来像这样:

    Y
   / \
  A   B
       \
        X
       / \
     (Y)  Z
         / \
        C   D

(Y)上面提到了Y及其所有子项,由于递归问题而将其省略。无论如何,重要的部分都会列出。现在树已链接回X,遍历继续...

 A
  \
   Y
  / \
(A)  B
      \
       X
      / \
    (Y)  Z
        / \
       C   D

然后A输出,因为它没有左子,然后current返回YA在上一次迭代中将其作为右子。在下一次迭代中,Y有两个孩子。但是,循环的双重条件使其在到达自身时停止,这表明它的左子树已被遍历。因此,它会自行打印,并继续其右侧的子树B

B进行打印,然后current变为X,与经过相同的检查过程一样Y,还意识到其左子树已被遍历,并继续Z。树的其余部分遵循相同的模式。

不需要递归,因为返回到(子)树根的链接不再依赖于通过堆栈的回溯,而是移动到了在递归有序树遍历算法中无论如何都将对其进行访问的点-左子树已完成。


3
感谢您的解释。左子节点不会被切断,而是稍后通过切断新的右子节点(为了遍历的目的)而添加到最右边的叶子,来还原树。请参阅我的更新后的代码。
brainydexter 2011年

1
漂亮的草图,但我仍然不了解while循环条件。为什么需要检查pre-> right!=当前?
No_name

6
我不明白为什么这样。打印A之后,Y成为根,并且您仍然将A作为左孩子。因此,我们处于与以前相同的情况。我们重复A。实际上,它看起来像一个无限循环。
user678392

这不是切断Y和B之间的连接吗?当X设置为current且Y设置为pre时,它将向下看pre的右子树,直到找到当前(X),然后将pre => right设置为NULL,这将是B正确吗?按照上面发布的代码
Achint 2014年

17

递归有序遍历为: (in-order(left)->key->in-order(right))。(类似于DFS)

在执行DFS时,我们需要知道回溯到的位置(这就是我们通常保留堆栈的原因)。

当我们经过一个父节点时,我们需要回溯到->我们找到需要从中回溯的节点并更新其到父节点的链接。

我们何时回溯?当我们无法走得更远时。什么时候我们不能走得更远?没有孩子的礼物时。

我们回溯到哪里?通知:致成功!

因此,当我们沿着左子路径跟踪节点时,请在每个步骤中将前任节点设置为指向当前节点。这样,前任者将具有到后任者的链接(用于回溯的链接)。

我们会尽一切可能向左走,直到需要回溯为止。当我们需要回溯时,我们将打印当前节点,并通过正确的链接指向后继节点。

如果我们刚刚回溯->我们需要跟随合适的孩子(我们与左边的孩子在一起)。

如何判断我们是否刚刚回溯?获取当前节点的前任节点,并检查它是否具有指向该节点的正确链接。如果有-那么我们会遵循它。删除链接以还原树。

如果没有左链接=>,我们没有回溯,应该跟随左孩子。

这是我的Java代码(很抱歉,它不是C ++)

public static <T> List<T> traverse(Node<T> bstRoot) {
    Node<T> current = bstRoot;
    List<T> result = new ArrayList<>();
    Node<T> prev = null;
    while (current != null) {
        // 1. we backtracked here. follow the right link as we are done with left sub-tree (we do left, then right)
        if (weBacktrackedTo(current)) {
            assert prev != null;
            // 1.1 clean the backtracking link we created before
            prev.right = null;
            // 1.2 output this node's key (we backtrack from left -> we are finished with left sub-tree. we need to print this node and go to right sub-tree: inOrder(left)->key->inOrder(right)
            result.add(current.key);
            // 1.15 move to the right sub-tree (as we are done with left sub-tree).
            prev = current;
            current = current.right;
        }
        // 2. we are still tracking -> going deep in the left
        else {
            // 15. reached sink (the leftmost element in current subtree) and need to backtrack
            if (needToBacktrack(current)) {
                // 15.1 return the leftmost element as it's the current min
                result.add(current.key);
                // 15.2 backtrack:
                prev = current;
                current = current.right;
            }
            // 4. can go deeper -> go as deep as we can (this is like dfs!)
            else {
                // 4.1 set backtracking link for future use (this is one of parents)
                setBacktrackLinkTo(current);
                // 4.2 go deeper
                prev = current;
                current = current.left;
            }
        }
    }
    return result;
}

private static <T> void setBacktrackLinkTo(Node<T> current) {
    Node<T> predecessor = getPredecessor(current);
    if (predecessor == null) return;
    predecessor.right = current;
}

private static boolean needToBacktrack(Node current) {
    return current.left == null;
}

private static <T> boolean weBacktrackedTo(Node<T> current) {
    Node<T> predecessor = getPredecessor(current);
    if (predecessor == null) return false;
    return predecessor.right == current;
}

private static <T> Node<T> getPredecessor(Node<T> current) {
    // predecessor of current is the rightmost element in left sub-tree
    Node<T> result = current.left;
    if (result == null) return null;
    while(result.right != null
            // this check is for the case when we have already found the predecessor and set the successor of it to point to current (through right link)
            && result.right != current) {
        result = result.right;
    }
    return result;
}

4
我非常喜欢您的回答,因为它提供了提出此解决方案的高级理由!
KFL

6

我在这里为算法制作了动画: https //docs.google.com/presentation/d/11GWAeUN0ckP7yjHrQkIB0WT9ZUhDBSa-WR0VsPU38fg/edit?usp=sharing

希望这有助于理解。蓝色圆圈是光标,每张幻灯片是外部while循环的迭代。

这是morris遍历的代码(我从极客那里复制并修改了极客):

def MorrisTraversal(root):
    # Set cursor to root of binary tree
    cursor = root
    while cursor is not None:
        if cursor.left is None:
            print(cursor.value)
            cursor = cursor.right
        else:
            # Find the inorder predecessor of cursor
            pre = cursor.left
            while True:
                if pre.right is None:
                    pre.right = cursor
                    cursor = cursor.left
                    break
                if pre.right is cursor:
                    pre.right = None
                    cursor = cursor.right
                    break
                pre = pre.right
#And now for some tests. Try "pip3 install binarytree" to get the needed package which will visually display random binary trees
import binarytree as b
for _ in range(10):
    print()
    print("Example #",_)
    tree=b.tree()
    print(tree)
    MorrisTraversal(tree)

您的动画非常有趣。由于外部链接通常会在一段时间后消失,因此请考虑将其图像化并包含在您的帖子中。
laancelot

1
动画很有帮助!
yyFred

强大的电子表格和binarytree库的用法。但是代码不正确,无法打印根节点。您需要在行print(cursor.value)后添加pre.right = None
萨特南

4
public static void morrisInOrder(Node root) {
        Node cur = root;
        Node pre;
        while (cur!=null){
            if (cur.left==null){
                System.out.println(cur.value);      
                cur = cur.right; // move to next right node
            }
            else {  // has a left subtree
                pre = cur.left;
                while (pre.right!=null){  // find rightmost
                    pre = pre.right;
                }
                pre.right = cur;  // put cur after the pre node
                Node temp = cur;  // store cur node
                cur = cur.left;  // move cur to the top of the new tree
                temp.left = null;   // original cur left be null, avoid infinite loops
            }        
        }
    }

我认为这段代码会更好理解,只需使用null即可避免无限循环,而不必使用魔术。它可以很容易地修改为预购。


1
解决方案非常简洁,但是存在一个问题。根据Knuth的说法,最后不要修改树。做temp.left = null树会迷路。
Ankur 2015年

此方法可用于将二进制树转换为链接列表之类的地方。
cyber_raj '16

就像@Shan所说的那样,该算法不应更改原始树。当您的算法可以遍历时,它会破坏原始树。因此,这实际上不同于原始算法,因此具有误导性。
ChaoSXDemon


1

我希望下面的伪代码能更好地揭示:

node = root
while node != null
    if node.left == null
        visit the node
        node = node.right
    else
        let pred_node be the inorder predecessor of node
        if pred_node.right == null /* create threading in the binary tree */
            pred_node.right = node
            node = node.left
        else         /* remove threading from the binary tree */
            pred_node.right = null 
            visit the node
            node = node.right

参考问题中的C ++代码,内部while循环查找当前节点的有序前任。在标准二叉树中,前任的右子代必须为null,而在线程版本中,右子代必须指向当前节点。如果右子元素为null,则将其设置为当前节点,从而有效地创建threading,将其用作返回点,否则必须将其存储在通常是堆栈上。如果右子节点不为 null,则算法将确保原始树已还原,然后继续在右子树中遍历(在这种情况下,已知访问了左子树)。


0

Python解决方案时间复杂度:O(n)空间复杂度:O(1)

出色的莫里斯有序遍历说明

class Solution(object):
def inorderTraversal(self, current):
    soln = []
    while(current is not None):    #This Means we have reached Right Most Node i.e end of LDR traversal

        if(current.left is not None):  #If Left Exists traverse Left First
            pre = current.left   #Goal is to find the node which will be just before the current node i.e predecessor of current node, let's say current is D in LDR goal is to find L here
            while(pre.right is not None and pre.right != current ): #Find predecesor here
                pre = pre.right
            if(pre.right is None):  #In this case predecessor is found , now link this predecessor to current so that there is a path and current is not lost
                pre.right = current
                current = current.left
            else:                   #This means we have traverse all nodes left to current so in LDR traversal of L is done
                soln.append(current.val) 
                pre.right = None       #Remove the link tree restored to original here 
                current = current.right
        else:               #In LDR  LD traversal is done move to R  
            soln.append(current.val)
            current = current.right

    return soln

抱歉,但这不是对这个问题的直接答案。OP要求解释它是如何工作的,而不是实现,这可能是因为他们想自己实现算法。您的评论对已经了解该算法的人很有用,但OP尚不了解。同样,作为一项策略,答案应该是独立的,而不是仅链接到某些外部资源,因为链接可能会随着时间而改变或中断。可以包含链接,但是如果您这样做,还应该至少包括链接提供的内容的要点。
Anonymous1847

0

PFB莫里斯有序遍历的说明。

  public class TreeNode
    {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
        {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class MorrisTraversal
    {
        public static IList<int> InOrderTraversal(TreeNode root)
        {
            IList<int> list = new List<int>();
            var current = root;
            while (current != null)
            {
                //When there exist no left subtree
                if (current.left == null)
                {
                    list.Add(current.val);
                    current = current.right;
                }
                else
                {
                    //Get Inorder Predecessor
                    //In Order Predecessor is the node which will be printed before
                    //the current node when the tree is printed in inorder.
                    //Example:- {1,2,3,4} is inorder of the tree so inorder predecessor of 2 is node having value 1
                    var inOrderPredecessorNode = GetInorderPredecessor(current);
                    //If the current Predeccessor right is the current node it means is already printed.
                    //So we need to break the thread.
                    if (inOrderPredecessorNode.right != current)
                    {
                        inOrderPredecessorNode.right = null;
                        list.Add(current.val);
                        current = current.right;
                    }//Creating thread of the current node with in order predecessor.
                    else
                    {
                        inOrderPredecessorNode.right = current;
                        current = current.left;
                    }
                }
            }

            return list;
        }

        private static TreeNode GetInorderPredecessor(TreeNode current)
        {
            var inOrderPredecessorNode = current.left;
            //Finding Extreme right node of the left subtree
            //inOrderPredecessorNode.right != current check is added to detect loop
            while (inOrderPredecessorNode.right != null && inOrderPredecessorNode.right != current)
            {
                inOrderPredecessorNode = inOrderPredecessorNode.right;
            }

            return inOrderPredecessorNode;
        }
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.