当前位置: 首页 > 知识库问答 >
问题:

创建二叉搜索树

郎志
2023-03-14

好的,所以我目前正在尝试创建一个二叉搜索树,每个节点都包含对某个对象的引用,以及对其左侧子项的引用和对右子项的引用(总共3个变量)。左子项必须始终小于其父项,而右子项必须始终大于其父项。我必须创建两个方法:1种方法( contains()) 来检查元素是否在树中,以及一个add()方法将元素添加到树中的适当位置。

以下是BinarySearchTree类:

public class BinarySearchTree extends BinaryTree {

public BinarySearchTree(TreeNode t){
    super(t);
}

public boolean contains (Comparable obj){
    if (this.myRoot != null){
        return containshelper(obj, this.myRoot);
    }
    return false;

}

public static boolean containshelper(Comparable comp, TreeNode t){
    boolean flag = true;
    int x = comp.compareTo(t.myItem);
    if (x < 0 && t.myLeft != null){
        containshelper(comp,t.myLeft);
    }
    if (x > 0 && t.myRight != null){
        containshelper(comp,t.myRight);
    }
    if (x == 0){
        return true;
    }
    return false;
}


public void add(Comparable key) {
    if (!this.contains(key)){
        if (this.myRoot != null){
            add(myRoot, key);
        }
        System.out.print("Getting Here ");
    }
    else {
        System.out.print("Tree already contains item");
    }
}

private static TreeNode add(TreeNode t, Comparable key) {

    if (((Comparable) t.myItem).compareTo(key) < 0 && t.myLeft != null){
        add(t.myLeft,key);

    }
    if(((Comparable) t.myItem).compareTo(key) > 0 && t.myRight != null ) {
        add(t.myRight,key);

    }
    if (((Comparable) t.myItem).compareTo(key) < 0 && t.myLeft == null){
        TreeNode q = new TreeNode(key);
        t.myLeft = q;

    }
    if (((Comparable) t.myItem).compareTo(key) > 0 && t.myRight == null ){
        TreeNode w = new TreeNode(key);
        t.myRight = w;

    }
    return t;
}
}

下面是TreeNode类(包含在BinaryTree中):

    public static class TreeNode {

    public Object myItem;
    public TreeNode myLeft;
    public TreeNode myRight;
    public int size;

    public TreeNode (Object obj) {
        size = size(this);
        myItem = obj;
        myLeft = myRight = null;
    }
    public int size(TreeNode t) { 
        return(sizehelper(t)); 
    }
    private int sizehelper(TreeNode node) { 
        if (node == null){ 
            return(0); 
        }
        else { 
            return(size(node.myLeft) + 1 + size(node.myRight)); 
        } 
    }


    public TreeNode (Object obj, TreeNode left, TreeNode right) {
        myItem = obj;
        myLeft = left;
        myRight = right;
    }
}
}

我很确定我的contains()方法是可行的,但是我怎么也想不出add()为什么行不通。任何帮助都将不胜感激。

共有1个答案

易成双
2023-03-14

您在包含帮助器中有一些错误

public static boolean containshelper(Comparable comp, TreeNode t){

    // TreeNode.myItem should be declared of type `Comparable` instead of `Object`
    int x = comp.compareTo(t.myItem);
    if (x < 0 && t.myLeft != null){
        return containshelper(comp,t.myLeft); // where does result of this call go? We must return the result !!
    }
    if (x > 0 && t.myRight != null){
        return containshelper(comp,t.myRight); // this too ?
    }
    if (x == 0){
        return true;
    }
    return false;
}

这里也有同样的错误:

private static TreeNode add(TreeNode t, Comparable key) {

if (t.myItem.compareTo(key) < 0 && t.myLeft != null){
    t = add(t.myLeft,key); // where does returned result go?
}
else if (t.myItem.compareTo(key) > 0 && t.myRight != null ) {
    t = add(t.myRight,key);
}
else if (t.myItem.compareTo(key) < 0 && t.myLeft == null){
    TreeNode q = new TreeNode(key);
    t.myLeft = q;
}
else if (t.myItem.compareTo(key) > 0 && t.myRight == null ){
    TreeNode w = new TreeNode(key);
    t.myRight = w;
}
return t;
}

您应该声明“可比较”类型的 TreeNode.myItem,以强制每个要适合二进制搜索的类提供 compareTo 方法。这比将对象转换为可比较对象要好,因为该对象可能没有实现可比较,然后会导致运行时错误

 类似资料:
  • 本文向大家介绍C#创建二叉搜索树的方法,包括了C#创建二叉搜索树的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#创建二叉搜索树的方法。分享给大家供大家参考。具体如下: 希望本文所述对大家的C#程序设计有所帮助。

  • 我不知道这些节点是否被插入,但输出结果是正确的。我只想插入节点到左边的孩子,我可以消除那代码吗?root.right=insertLevelOrder(arr,root.right,2*i+2); 还有为什么这个循环没有“i++”的符号,int i是如何自动增加的?

  • 我很难按我教授想要的格式打印出一个二叉搜索树。 他的格式是这样的: 我的代码:

  • 编写一个函数,如果给定的二叉搜索树包含给定的值,则返回1,否则返回0。 例如,对于以下树: N1(值:1,左:null,右:null) n2(值:2,左:n1,右:n3) N3(值:3,左:null,右:null) 对contains(&n2,3)的调用应返回1,因为根位于n2的树包含编号3。 函数应该返回1,然而,它返回0或者根本不返回。

  • 我用java编写了一个实用的二叉搜索树,除了一个关键的函数,搜索。我使用的逻辑是,我将检查根是否为空,然后我要搜索的术语是否等于根(所以返回根)或>根(所以搜索右子树)或 使用printlns查看正在发生的事情,我发现如果值在那里,它将通过正确的if语句(包括将BNode n设置为找到的值),但随后由于某种原因将再次通过方法(返回null)。 这个方法唯一起作用的时候是在搜索根节点的时候,这对我来

  • 在二元搜索树的情况下,为什么我们不能简单地在一个节点有两个子节点的情况下,将一个节点的前一个节点替换为后一个节点?