【问题标题】:Traverse binary tree - no recursion, no stack, no tree modification遍历二叉树——无递归、无栈、无树修改
【发布时间】:2014-11-04 22:19:41
【问题描述】:

因此,对于我正在构建的应用程序,我需要能够在不使用递归、堆栈或在创建树后以任何方式修改树的情况下遍历二叉树。我的节点结构如下:

typedef struct 
{
    ValueType value;  //Data stored in node
    int left_index;   //Left child
    int right_index;  //Right child
    int parent_index; //Parent node
}

我将树存储为一维数组,其中每个节点的左子节点位于索引 2*i + 1,右子节点位于 2*i + 2,父节点位于 [i-1]/2 .如果一个节点没有父节点或子节点,它的关联索引为-1。

我发现的唯一一种基于迭代的非堆栈算法是一种叫做“Morris Traversal”的算法,如下所示:http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/

但是,Morris Traversal 在遍历期间修改了树,这是我无法做到的。

我愿意为每个节点添加任何需要的信息,只要我能在上述约束条件下编写算法即可。

我所要求的是否可能?如果是这样,我将如何去做?甚至不确定如何开始。

【问题讨论】:

    标签: data-structures graph tree binary-search-tree traversal


    【解决方案1】:

    “while(!done)”循环不够吗?

    【讨论】:

      【解决方案2】:

      你想要的是threaded binary tree。所有叶子节点的右指针指向该节点的有序后继。

      创建这样的东西很容易,在插入或删除节点时更新它一点也不难。如果您可以控制节点结构,那么这几乎肯定是您想要的。

      【讨论】:

        【解决方案3】:
        public class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
            TreeNode parent;
        
            public void traverse() {
                TreeNode current = this.leftMost();
                while (current != null) {
                    System.out.println("Current at " + current.val);
                    current = current.inOrderNext();
                }
            }
        
            public TreeNode inOrderNext() {
                if (right != null) {
                    return right.leftMost();
                } else {
                    TreeNode current = this;
                    TreeNode above = this.parent;
                    while (true) {
                        if (above == null) {
                            return null;
                        } else {
                            if (above.left == current) {
                                return above;
                            } else {
                                current = above;
                                above = above.parent;
                            }
                        }
                    }
                }
            }
        
            public TreeNode leftMost() {
                TreeNode result = this;
                while (result.left != null) {
                    result = result.left;
                }
                return result;
            }
        
            public static void main(String args[]) {
                TreeNode first = new TreeNode();
                first.val = 4;
        
                TreeNode second = new TreeNode();
                second.val = 2;
                second.parent = first;
                first.left = second;
        
                TreeNode third = new TreeNode();
                third.val = 1;
                third.parent = second;
                second.left = third;
        
                third = new TreeNode();
                third.val = 3;
                third.parent = second;
                second.right = third;
        
                second = new TreeNode();
                second.val = 6;
                second.parent = first;
                first.right = second;
        
                third = new TreeNode();
                third.val = 5;
                third.parent = second;
                second.left = third;
        
                third = new TreeNode();
                third.val = 7;
                third.parent = second;
                second.right = third;
        
                first.traverse();
            }
        }
        

        【讨论】:

        • 有很多关于在没有“额外”的情况下遍历树的研究。但是第一个额外的人删除的是“父”指针。有了它,一个人可以随心所欲地在树上漫游,让一切变得简单。
        猜你喜欢
        • 2010-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        相关资源
        最近更新 更多