我可以在没有递归和堆栈的情况下遍历二叉树吗?

时间:2010-04-07 18:41:37

标签: tree tree-traversal

任何人都可以给我一个解决方案,以便在没有递归和不使用堆栈的情况下遍历二进制树吗?

6 个答案:

答案 0 :(得分:4)

第二次编辑:我认为这是对的。除了通常的node.left_child和node.right_child之外,还需要node.isRoot,node.isLeftChild和node.parent。

state = "from_parent"
current_node = root
while (!done)
  switch (state)
    case "from_parent":
      if current_node.left_child.exists
        current_node = current_node.left_child
        state = "from_parent"
      else
        state = "return_from_left_child"
    case "return_from_left_child"
      if current_node.right_child.exists
        current_node = current_node.right_child
        state = "from_parent"
      else
        state = "return_from_right_child"
    case "return_from_right_child"
      if current_node.isRoot
        done = true
      else
        if current_node.isLeftChild
         state = "return_from_left_child"
        else
         state = "return_from_right_child"
        current_node = current_node.parent

答案 1 :(得分:0)

由于遍历binary tree需要某种状态(访问后继节点后返回的节点),这可能是由递归暗示的堆栈提供的(或由数组显式)。

答案是否定的,你不能。 (根据经典定义)

以迭代方式最接近二叉树遍历的事情可能是使用heap

编辑: 或者已经显示了threaded binary tree

答案 2 :(得分:0)

是的,你可以。为了做到这一点,你需要一个父指针才能提升树。

答案 3 :(得分:0)

从tree_first()开始,继续使用tree_next()直到获得NULL。 完整代码:https://github.com/virtan/tree_closest

struct node {
    int value;
    node *left;
    node *right;
    node *parent;
};

node *tree_first(node *root) {
    while(root && root->left)
        root = root->left;
    return root;
}

node *tree_next(node *p) {
    if(p->right)
        return tree_first(p->right);
    while(p->parent) {
        if(!p->parent->right || p->parent->right != p)
            return p->parent;
        else p = p->parent;
    }
    return 0;
}

答案 4 :(得分:0)

1.双线程树

如果你的树节点有父引用/指针,那么在遍历过程中跟踪你来自哪个节点,这样你就可以决定下一步去哪里。

在 Python 中:

class Node:
    def __init__(self, value, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right
        self.parent = None
        if self.left:
            self.left.parent = self
        if self.right:
            self.right.parent = self

    def inorder(self):
        cur = self
        pre = None
        nex = None
        while cur:
            if cur.right and pre == cur.right:
                nex = cur.parent
            elif not cur.left or pre == cur.left:
                yield cur.value  # visit!
                nex = cur.right or cur.parent
            else:
                nex = cur.left
            pre = cur
            cur = nex

root = Node(1,
            Node(2, Node(4), Node(5)),
            Node(3)
        )

print([value for value in root.inorder()])  # [4, 2, 5, 1, 3]

2.单线程树

如果你的树节点没有父引用/指针,那么你可以做一个所谓的莫里斯遍历,它暂时改变树,使 right 属性 - 一个没有右孩子的节点-- 临时指向它的中序后继节点:

在 Python 中:

class Node:
    def __init__(self, value, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

    def inorder(self):
        cur = self
        while cur:
            if cur.left:
                pre = cur.left
                while pre.right:
                    if pre.right is cur:
                        # We detect our mutation. So we finished
                        # the left subtree traversal.
                        pre.right = None
                        break
                    pre = pre.right
                else:  # prev.right is None
                    # Mutate this node, so it links to curr
                    pre.right = cur
                    cur = cur.left
                    continue
            yield cur.value
            cur = cur.right

root = Node(1,
            Node(2, Node(4), Node(5)),
            Node(3)
        )

print([value for value in root.inorder()])

答案 5 :(得分:-1)

正如这里已经说过的那样,有可能,但不是没有父指针。父指针基本上允许您根据需要遍历“路径”,因此打印出节点。 但是为什么递归在没有父指针的情况下工作?好吧,如果你理解递归,它会像这样(想象一下递归堆栈):

  recursion //going into
   recursion
    recursion
     recursion 
     recursion //going back up
    recursion
   recursion
  recursion

因此,当递归结束时,您将以相反的顺序打印二叉树的选定面。

相关问题