Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在使用顺序进行二叉搜索树时,如何获取第一个元素?_Java_Binary Search Tree_Inorder - Fatal编程技术网

Java 在使用顺序进行二叉搜索树时,如何获取第一个元素?

Java 在使用顺序进行二叉搜索树时,如何获取第一个元素?,java,binary-search-tree,inorder,Java,Binary Search Tree,Inorder,我的尝试(给了我一个NullPointerException): public Karte giveFirst(二进制搜索树t){ if(t.getLeftTree()!=null){ 返回giveFirst(t.getLeftTree()); }否则{ 返回t.getContent(); } } 完整的二进制搜索树代码: package Model; private class BSTNode<CT extends ComparableContent<CT>&

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

public Karte giveFirst(二进制搜索树t){
if(t.getLeftTree()!=null){
返回giveFirst(t.getLeftTree());
}否则{
返回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;
    }
}
包模型;
私有类节点{
私人CT内容;
私有二进制搜索树(左、右);
公共节点(CT pContent){
this.content=p内容;
左=新的二进制搜索树();
右=新的二进制搜索树();
}
}
专用节点;
公共二进制搜索树(){
this.node=null;
}
公共void插入(ContentType pContent){
if(pContent!=null){
if(isEmpty()){
this.node=新的BSTNode(pContent);
}else if(pContent.isLess(this.node.content)){
this.node.left.insert(pContent);
}else if(pContent.ismorer(this.node.content)){
this.node.right.insert(pContent);
}
}
}
公共二进制搜索树getLeftTree(){
if(this.isEmpty()){
返回null;
}否则{
返回this.node.left;
}
}
公共内容类型getContent(){
if(this.isEmpty()){
返回null;
}否则{
返回this.node.content;
}
}
公共二进制搜索树getRightTree(){
if(this.isEmpty()){
返回null;
}否则{
返回this.node.right;
}
}
删除公共void(ContentType pContent){
if(isEmpty()){
返回;
}
if(pContent.isLess(node.content)){
node.left.remove(pContent);
}else if(pContent.ismorer(node.content)){
node.right.remove(pContent);
}否则{
if(node.left.isEmpty()){
if(node.right.isEmpty()){
node=null;
}否则{
node=GetNodeOfrieghtSuccession();
}
}else if(node.right.isEmpty()){
node=getNodeOfLeftSuccessor();
}否则{
if(getNodeOfRightSuccession().left.isEmpty()){
node.content=getNodeOfrieghtSuccessiver().content;
node.right=getNodeOfrieghtSuccinator().right;
}否则{
BinarySearchTree previous=node.right
.antestorofsmallright();
BinarySearchTree=previous.node.left;
this.node.content=最小的.node.content;
previous.remove(最小的.node.content);
}
}
}
}
公共ContentType搜索(ContentType pContent){
if(this.isEmpty()| | pContent==null){
返回null;
}否则{
ContentType content=this.getContent();
if(无内容(内容)){
返回此.getLeftTree().search(pContent);
}else if(pContent.ismore(content)){
返回此.getRightTree().search(pContent);
}else if(pContent.isEqual(内容)){
返回内容;
}否则{
返回null;
}
}
}
私有二进制搜索树ancestorOfSmallRight(){
if(getNodeOfLeftSuccessor().left.isEmpty()){
归还这个;
}否则{
返回node.left.ancestorOfSmallRight();
}
}
私有BSTNode getNodeOfLeftSuccessor(){
返回node.left.node;
}
私有BSTNode GetNodeOfrieghtSuccessiver(){
返回node.right.node;
}
}

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

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

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


当该节点没有剩余子节点时,必须返回该节点,因为它将是树中最小的元素

此外,我不知道为什么要使用
BinarySearchTree
BSTNode
。一个
BSTNode
应该足以构建一棵树。为了更好地理解,请阅读一些教程/课程

在使用顺序进行二叉搜索树时,如何获取第一个元素

一直向左走直到你不能再向左走。。。如果一个节点有一个左子节点,则选择它。继续这样做,直到到达一个没有左子节点的节点。当你在那里时,你知道你在树的最左边。下面的代码片段展示了如何在给定BST的
root
的情况下实现这一点

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

只需将while更改为if并添加一个else条件else{return t;}将t.getLeftTree().isEmpty()更改为t.getLeftTree()!=那么这可能是树的定义有问题。除非我看到完整的源代码,否则我帮不了你多少忙。如果您的t.getLeftTree()以可接受的方式运行,则此递归方法应该可以正常工作
public BSTNode getSmallest(BSTNode root) {
    if(root.left != null)
        return getSmallest(root.left);
    return root;
}