【问题标题】:How to get the first element when going through a binary search tree with inorder?用中序遍历二叉搜索树时如何获取第一个元素?
【发布时间】:2016-04-14 16:58:53
【问题描述】:

我的尝试(给了我一个 NullPointerException):

public Karte giveFirst(BinarySearchTree<Karte> t){
    if(t.getLeftTree() != null) {
        return giveFirst(t.getLeftTree());
    }else{
        return t.getContent();
    }
}

完整的二叉搜索树代码:

package  Model;


    private class BSTNode<CT extends ComparableContent<CT>> {

        private CT content;
        private BinarySearchTree<CT> left, right;

        public BSTNode(CT pContent) {

            this.content = pContent;
            left = new BinarySearchTree<CT>();
            right = new BinarySearchTree<CT>();
        }

    }

    private BSTNode<ContentType> node;

    public BinarySearchTree() {
        this.node = null;
    }

    public void insert(ContentType pContent) {
        if (pContent != null) {
            if (isEmpty()) {
                this.node = new BSTNode<ContentType>(pContent);
            } else if (pContent.isLess(this.node.content)) {
                this.node.left.insert(pContent);
            } else if(pContent.isGreater(this.node.content)) {
                this.node.right.insert(pContent);
            }
        }
    }

    public BinarySearchTree<ContentType> getLeftTree() {
        if (this.isEmpty()) {
            return null;
        } else {
            return this.node.left;
        }
    }

    public ContentType getContent() {
        if (this.isEmpty()) {
            return null;
        } else {
            return this.node.content;
        }
    }

    public BinarySearchTree<ContentType> getRightTree() {
        if (this.isEmpty()) {
            return null;
        } else {
            return this.node.right;
        }
    }

    public void remove(ContentType pContent) {
        if (isEmpty()) {
            return;
        }

        if (pContent.isLess(node.content)) {
            node.left.remove(pContent);
        } else if (pContent.isGreater(node.content)) {
            node.right.remove(pContent);
        } else {
            if (node.left.isEmpty()) {
                if (node.right.isEmpty()) {
                    node = null;
                } else {
                    node = getNodeOfRightSuccessor();
                }
            } else if (node.right.isEmpty()) {
                node = getNodeOfLeftSuccessor();
            } else {
                if (getNodeOfRightSuccessor().left.isEmpty()) {
                    node.content = getNodeOfRightSuccessor().content;
                    node.right = getNodeOfRightSuccessor().right;
                } else {
                    BinarySearchTree<ContentType> previous = node.right
                            .ancestorOfSmallRight();
                    BinarySearchTree<ContentType> smallest = previous.node.left;
                    this.node.content = smallest.node.content;
                    previous.remove(smallest.node.content);
                }
            }
        }
    }

    public ContentType search(ContentType pContent) {
        if (this.isEmpty() || pContent == null) {
            return null;
        } else {
            ContentType content = this.getContent();
            if (pContent.isLess(content)) {
                return this.getLeftTree().search(pContent);
            } else if (pContent.isGreater(content)) {
                return this.getRightTree().search(pContent);
            } else if (pContent.isEqual(content)) {
                return content;
            } else {
                return null;
            }
        }
    }
    private BinarySearchTree<ContentType> ancestorOfSmallRight() {
        if (getNodeOfLeftSuccessor().left.isEmpty()) {
            return this;
        } else {
            return node.left.ancestorOfSmallRight();
        }
    }

    private BSTNode<ContentType> getNodeOfLeftSuccessor() {
        return node.left.node;
    }

    private BSTNode<ContentType> getNodeOfRightSuccessor() {
        return node.right.node;
    }
}

如何更改/重写我的代码以使其正常工作?

【问题讨论】:

    标签: java binary-search-tree inorder


    【解决方案1】:

    想想当你到达最左边的叶节点时会发生什么。

    它的左树将为空,这意味着您将跳出 while 循环。在此之后您将返回 null,因此任何访问返回值状态的尝试都会引发异常。

    当它没有更多的左子节点时,你必须返回这个节点,因为这将是树中最小的元素

    【讨论】:

    • 只需将 while 更改为 if 并添加 else 条件 else {return t;}
    • 将 t.getLeftTree().isEmpty() 更改为 t.getLeftTree() != null
    • 那么你对树的定义可能有问题。除非我看到完整的源代码,否则我帮不了你太多。如果您的 t.getLeftTree() 以可接受的方式运行,则此递归方法应该可以正常工作
    【解决方案2】:

    另外,我不知道你为什么有BinarySearchTreeBSTNodeBSTNode 应该足以构建一棵树。请阅读一些教程/课程以获得更好的理解。

    按顺序遍历二叉搜索树时如何获取第一个元素?

    继续向左走,直到你不能再向左走……如果一个节点有一个左孩子,那就去吧。继续这样做,直到你到达一个没有左孩子的节点。当你在那里时,你知道你在树的最左边。下面的代码 sn-p 展示了如何在给定 BST 的 root 的情况下实现这一点。

    public BSTNode getSmallest(BSTNode root) {
        if(root.left != null)
            return getSmallest(root.left);
        return root;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多