Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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 - Fatal编程技术网

返回Java中多态二进制搜索树的大小?

返回Java中多态二进制搜索树的大小?,java,binary-search-tree,Java,Binary Search Tree,我正在尝试为多态二进制搜索树编写size()方法。 即,具有类EmptyTree和非EmptyTree的BST,这两个类都实现了“树”接口。EmptyTree用于表示空树或子树,而不是null(如在常规BST中) 但是,当我尝试在测试中运行此命令时: Tree<Integer, String> treeThree = EmptyTree.getInstance(); //EmptyTree is a singleton class treeThree = treeThree.a

我正在尝试为多态二进制搜索树编写size()方法。 即,具有类EmptyTree和非EmptyTree的BST,这两个类都实现了“树”接口。EmptyTree用于表示空树或子树,而不是null(如在常规BST中)

但是,当我尝试在测试中运行此命令时:

Tree<Integer, String> treeThree = EmptyTree.getInstance(); //EmptyTree is a singleton class
   treeThree = treeThree.add(3, "3"); 
   treeThree = treeThree.add(2, "2"); 
   treeThree = treeThree.add(1, "1"); 
   treeThree = treeThree.add(4, "4");
   assertEquals(4, treeThree.size());
非空树查找:

public V lookup(K key) {
    return null;
  }
public V lookup(K key) {
      if(key.compareTo(this.key) == 0)
          return this.value;
      else {
          if(key.compareTo(this.key) < 0)
              return this.left.lookup(key);
          if(key.compareTo(this.key) > 0) 
              return this.right.lookup(key);
      }

    return null;
  }
public V查找(K键){
if(key.compareTo(this.key)==0)
返回此.value;
否则{
if(key.compareTo(this.key)<0)
返回此.left.lookup(键);
如果(key.compareTo(this.key)>0)
返回此.right.lookup(键);
}
返回null;
}

正如j_random_hacker先生在评论中所说,您不需要在add方法中调用查找方法。这将替换包含EmptyTree作为其子树的整个子树。 将添加方法更改为:

public NonEmptyTree<K, V> add(K key, V value) {
      if(this.key.compareTo(key) == 0) {
          this.value = value;
      }
      else {
          if(key.compareTo(this.key) < 0) { 
                  this.left = this.left.add(key, value);
          }
          else { 
                  this.right = this.right.add(key, value);
          }
      }
      return this;
  }
公共非空树添加(K键,V值){
if(this.key.compareTo(key)==0){
这个值=值;
}
否则{
如果(key.compareTo)(this.key)<0{
this.left=this.left.add(键,值);
}
否则{
this.right=this.right.add(键,值);
}
}
归还这个;
}

这是它失败的最小例子吗?在任何情况下,第一个元素添加到
treetree
之后,
treetree
如何将其类从
EmptyTree
更改为
NonEmptyTree
?我不明白多态性在这里是如何工作的,除非允许类对象在Java中更改其类。我添加了一些其他有效的示例。哎呀,我错过了您将调用
add()
的结果分配回
treeThree
,所以这现在是有意义的。
lookup()
是否只返回该节点上的值?应该这样。(顺便说一句:您编写
this.left.lookup(key)=null
的事实表明,使用
EmptyTree
类型毕竟没有多大用处,不是吗。)添加了查找方法。问题是,您在尝试添加节点时调用
lookup()
。假设您正在添加一个元素,
if(key.compareTo(this.key)<0)
in
NonEmptyTree.add()
刚刚成功。进一步假设当前节点有一个包含k>0个节点的左子树,但是
key
没有出现在其中:那么
lookup()
将返回
false
,当调用
this.left=newnonemptytree(key,value)时,您将丢弃位于左侧子级的整个现有子树,并将其替换为包含单个新
键的节点。实例变量、键和值最初设置为null,因此我得到null指针异常。它们必须设置为null,因为我们不知道将要输入什么。
public NonEmptyTree<K, V> add(K key, V value) {  
    return new NonEmptyTree(key, value);
}
public NonEmptyTree<K, V> add(K key, V value) {
      if(this.key.compareTo(key) == 0) {
          this.value = value;
          return this;
      }
      else {
          if(key.compareTo(this.key) < 0) { 
              if(this.left.lookup(key) == null) {
                  this.left = new NonEmptyTree<K,V>(key, value);
                  return this;
              }
              else
                  return this.left.add(key, value);
          }
          else if(key.compareTo(this.key) > 0) { 
              if(this.right.lookup(key) == null) {
                  this.right = new NonEmptyTree<K,V>(key, value);
                  return this;
              }
              else
                  return this.right.add(key, value);
          }
      }
      return this;
  }
public V lookup(K key) {
    return null;
  }
public V lookup(K key) {
      if(key.compareTo(this.key) == 0)
          return this.value;
      else {
          if(key.compareTo(this.key) < 0)
              return this.left.lookup(key);
          if(key.compareTo(this.key) > 0) 
              return this.right.lookup(key);
      }

    return null;
  }
public NonEmptyTree<K, V> add(K key, V value) {
      if(this.key.compareTo(key) == 0) {
          this.value = value;
      }
      else {
          if(key.compareTo(this.key) < 0) { 
                  this.left = this.left.add(key, value);
          }
          else { 
                  this.right = this.right.add(key, value);
          }
      }
      return this;
  }