【问题标题】:Traversing through a Array-Binary tree遍历数组二叉树
【发布时间】:2021-07-31 19:07:34
【问题描述】:

在没有递归的情况下遍历数组二叉树时遇到了麻烦。 我希望有人能告诉我我做错了什么。为简单起见,代码保持简单。

请注意:不允许添加Iterator等其他方法来检查是否有hasNext()等。

现在我只想打印所有的键(包括子键),希望我能完成剩下的学习!

非常感谢您的帮助。

public class BTree {

    private Node root;

    public BTree(Node root) {
        this.root = root;
    }

    public int getMaxKey() {
        Node copy = root;

        for (int i = 0; copy.child != null && i < copy.child.length; i++) {
            System.out.println("key: " + copy.key); // output: 15, 80
            if (copy.child != null) {
                copy = copy.child[i];
            }

        }

        return 0;
    }
}
public class Node {
    public int key;
    public Node[] child;

    public Node(int key, Node[] child) {
        this.key = key;
        this.child = child;
    }
}

public class NodeMain {

    public static void main(String[] args) {
        Node[] lv1Nodes = new Node[2];
        lv1Nodes[0] = new Node(25, null);
        lv1Nodes[1] = new Node(99, null);

        Node[] lv0Nodes = new Node[2];
        lv0Nodes[0] = new Node(80, lv1Nodes);
        lv0Nodes[1] = new Node(5, null);

        Node root = new Node(15, lv0Nodes);

        BTree bTree = new BTree(root);
        int maxKey = bTree.getMaxKey(); // output should be 99
        System.out.println(maxKey);
    }

}

【问题讨论】:

    标签: java


    【解决方案1】:

    因此,对于二叉树遍历,您在这里只跟随一个分支直到 null。但是,您需要按照子数组的遍历顺序跟踪所有节点,以便从中获取最大元素。

    请参考下面的代码,这里我使用堆栈来保存所有值数组节点,我们可以弹出并检查 maxValue。

    public class BTree {
        private final Node root;
    
        public BTree(Node root) {
            this.root = root;
        }
    
        public int getMaxKey() {
            int maxKey = root.key;
            Stack<Node> stack = new Stack<>();
            stack.addAll(Arrays.asList(root.child));
            while(!stack.isEmpty()){
                Node node = stack.pop();
                System.out.println(node.key);
                if(node.key > maxKey)
                    maxKey = node.key;
                if(node.child != null)
                    stack.addAll(Arrays.asList(node.child));
            }
            return maxKey;
        }
    }
    

    编辑:我创建了一个图像,可以帮助我们更清楚地理解树,我已将其用作此代码的参考。

    【讨论】:

    • 感谢您的回答。我完全忘记了,如果你选择迭代操作,你总是走一条路。那是我缺少的链接!如果您选择递归操作,您将遵循多条路径。我实际上对您的解决方案没有任何问题,但我尝试了一个没有堆栈的解决方案。随意看看。
    • 嗨@Jotaro,您的解决方案适用于具有小于或等于两个节点的节点子值。如果节点子值超过两个怎么办?
    • 我之前也在考虑这个问题。我会说你不能有两个以上的节点,因为二叉树不支持它。 a binary tree is a tree data structure in which each node has at most two children en.wikipedia.org/wiki/Binary_tree btw:我更喜欢你的解决方案(因为它更短更干净),但我正在寻找没有任何其他数据结构的解决方案。
    • 哦,是的,我忘记了那部分。对于那个很抱歉。在这种情况下,您的解决方案更适合空间。谢谢。
    【解决方案2】:

    我没有 Stack 对象的解决方案:

    public int getMaxKey() {
            Node copy = root;
            int max = -1;
    
            while (copy.child != null) {
                if (max < copy.key)
                    max = copy.key; //root
    
                if (copy.child.length == 1) { // max key between parent and child node, only one path available
                    if (max < copy.child[0].key) {
                        max = copy.child[0].key;
                        copy = copy.child[0];
                    }
    
                } else if (copy.child.length == 2) { // max between two nodes, decide between one path
                    int maxBetweenChild = Math.max(copy.child[0].key, copy.child[1].key);
                    Node node = Arrays.stream(copy.child).filter(n -> n.key == maxBetweenChild).findFirst().get();
                    copy = node;
    
                    if (max < maxBetweenChild)
                        max = maxBetweenChild;
                }
            }
    
            return max;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-01-01
      • 2022-11-11
      • 1970-01-01
      • 2021-11-20
      • 2021-03-08
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多