Can I do inorder traversal of a binary tree without recursion and stack?

送分小仙女□ 提交于 2019-12-21 16:18:14

问题


Can anyone give me a solution for traversing a binary tree in inorder without recursion and without using a stack?


回答1:


Second edit: I think this is right. Requires node.isRoot, node.isLeftChild, and node.parent, in addition to the usual node.left_child and node.right_child.

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



回答2:


Since traversing a binary tree requires some kind of state (nodes to return after visiting successors) which could be provided by stack implied by recursion (or explicit by an array).

The answer is no, you can't. (according to the classic definition)

The closest thing to a binary tree traversal in an iterative way is probably using a heap

EDIT: Or as already shown a threaded binary tree ,




回答3:


Yes, you can. In order to do this, you would require a parent pointer in order to ascend the tree.




回答4:


Start with tree_first(), continue with tree_next() until get NULL. Full code: 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;
}



回答5:


As someone here already stated, it is possible, but not without the parent pointer. The parent pointer basically allows you to traverse "the path" if you want, and therefore print-out the nodes. But why does recursion works without parent pointer? Well if you understand recursion it goes something like this(imagine the recursion stack):

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

So when the recursion ends you then have printed the chosen side of the binary tree in reversed order.



来源:https://stackoverflow.com/questions/2595002/can-i-do-inorder-traversal-of-a-binary-tree-without-recursion-and-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!