繁体   English   中英

如何使用此类创建实例?

[英]how do I create an Instance with this class?

如何创建此类的实例来测试我的主程序中的函数? 我试图通过插入、删除和检查值是否存在来构建二叉搜索树。 我正在尝试使用我的构造函数创建我的对象。 每次我使用构造函数创建对象并将值传递给主程序中的构造函数时,我总是得到构造函数的值,在这种情况下为 9 作为我的结果。 我对此并不感到惊讶。 但我面临的挑战是使用对象 bst 调用函数 insert (bst.insert()) 以便将适当的结果打印到屏幕上。 任何帮助表示赞赏。 下面是代码:

class BST {
    static class Algo_BST {
        public int value;
        public Algo_BST left;
        public Algo_BST right;
        
        public Algo_BST(int value) {
          this.value = value;
        }

        public Algo_BST insert(int value) {
          // Write your code here.
                if(value < this.value){
                    if(left == null){
                         Algo_BST bst = new Algo_BST(value);
                         left = bst;
                        //this.left = new Algo_BST(value);
                    }else{
                        left.insert(value);
                    }
                }else{
                        if(right == null){
                            Algo_BST bst = new Algo_BST(value);
                             right = bst;
                            //this.right = new Algo_BST(value);
                        }else{
                            right.insert(value);
                        }
                    }
          // Do not edit the return statement of this method.
          return this;
        }

        public boolean contains(int value) {
                if (this.value < value){
                    if(left == null){
                     return false;
                    }else{
                    return left.contains(value);
                    }
                }else if(this.value > value){
                     if(right == null){
                         return false;
                     }else{
                         return right.contains(value);
                     }
                }else{
                    return true;
                }           
        }
  
       public String toString() {
            return "Data: " + this.value;
        }
       
    public static void main(String[] args)
    {   
        BST.Algo_BST bst = new BST.Algo_BST();
        System.out.print("Insert: "+bst.insert(90));
        System.out.print("Insert: "+bst.insert(50));
        System.out.print("Insert: "+bst.insert(70));
        System.out.print("Insert: "+bst.insert(80));
        System.out.print("Contains: "+bst.contains(80));
        System.out.print("Contains: "+bst.contains(1000));
    }
}  

我测试了你的课程并看到了一些细节:

第一的。 本地属性在方法insert ,您不需要额外的行:

//Algo_BST bst = new Algo_BST(value);
//left = bst;
this.left = new Algo_BST(value);
...
//Algo_BST bst = new Algo_BST(value);
//right = bst;
this.right = new Algo_BST(value);

第二。 大于和小于验证在方法contains是错误的:

// if (this.value < value){ // Wrong validation
if (this.value > value){
...
// }else if(this.value > value){ // Wrong validation
} else if(this.value < value){

我已经使用这些修复程序测试了该应用程序,并且运行良好。

insert这些更改使其返回新插入的节点。 我没有测试过它们。

    public Algo_BST insert(int value) {
      // Write your code here.
            if (value < this.value) {
                if (left == null) {
                     left = new Algo_BST(value);
                } else {
                     left.insert(value)   
                }
                return left;
             } else {
                if (right == null) {
                    this.right = new Algo_BST(value);
                } else {
                    right.insert(value);
                }
                return right;
             }
      // Do not edit the return statement of this method.
      return this;
    }

通过此更改,结果的“打印”应打印添加的内容。 我认为这不是特别有用,因为它没有显示树结构(即,您可能会弄错所有内容并且打印输出仍然是正确的)。

正如最初编写的那样,这棵树允许重复值。 这是故意的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM