英文:
Why can I not use .compareTo() when comparing the key of a a node in a BST to 0?
问题
- 这并不是类中包含的全部代码,但如果这还不够的话,我会添加剩下的部分。
- add() 的作用是将值按照键添加到二叉搜索树的正确位置。如果键已经存在,则不执行任何操作。
- contains() 应该在树中包含指定的键时返回 True。
{
public Node left;
public Node right;
public int key;
public String value;
public void add ( int key, String value )
{
if ( key.compareTo ( this.key ) < 0)
{
if ( left != null )
left.add ( key, value )
else
left = new Node ( key, value );
}
else if ( key.compareTo ( this.key ) > 0 )
{
if ( right != null )
right.add ( key, value );
else
right = new Node ( key, value);
}
else
this.value = value;
}
public boolean contains ( int key )
{
if ( this.key == ( key ) )
return value;
if ( key.compareTo ( this.key ) < 0 )
return left == null ? null : left.contains ( key );
else
return right == null ? null : right.contains ( key );
}
}
英文:
-
This is not all the code contained within the class, but if this is not enough I will add the rest as well.
-
add() is meant to add the value to the correct position in the BST using the key. If the key already exists, do nothing.
-
contains() is supposed to return True if the specified key is in the tree
```public class Node { public Node left; public Node right; public int key; public String value; public void add ( int key, String value ) { if ( key.compareTo ( this.key ) < 0) { if ( left != null ) left.add ( key, value ) else left = new Node ( key, value ); } else if ( key.compareTo ( this.key ) > 0 ) { if ( right != null ) right.add ( key, value ); else right = new Node ( key, value); } else this.value = value; } public boolean contains ( int key ) { if ( this.key == ( key ) ) return value; if ( key.compareTo ( this.key ) < 0 ) return left == null ? null : left.contains ( key ); else return right == null ? null : right.contains ( key ); } }
答案1
得分: 2
问题在于 int
是原始类型,因此不实现 Comparable 接口,所以您不能使用 int.compareTo。然而,包装变量 Integer 则实现了 Comparable 接口。您可以简单地使用 Integer 替代 int,或者使用 Integer.compare(1, 2) 来保留原始类型的使用。
public static class Node {
public Node left;
public Node right;
public Integer key;
public String value;
public Node(Integer key, String value) {
this.key = key;
this.value = value;
}
public void add(Integer key, String value) {
if (key.compareTo(this.key) < 0) {
if (left != null)
left.add(key, value);
else
left = new Node(key, value);
} else if (key.compareTo(this.key) > 0) {
if (right != null)
right.add(key, value);
else
right = new Node(key, value);
} else
this.value = value;
}
public boolean contains(Integer key) {
if (this.key.intValue() == key) {
return true;
}
if (key.compareTo(this.key) < 0)
return left == null ? false : left.contains(key);
else
return right == null ? false : right.contains(key);
}
}
英文:
The issue is that int
is primitive and therefor does not implement Comparable so you cannot use int.compareTo, however the boxed variation Integer does. You can simply use Integer instead of int, or alternatively use Integer.compare(1, 2) and retain your usage of primitives.
public static class Node {
public Node left;
public Node right;
public Integer key;
public String value;
public Node(Integer key, String value) {
this.key = key;
this.value = value;
}
public void add(Integer key, String value) {
if (key.compareTo(this.key) < 0) {
if (left != null)
left.add(key, value);
else
left = new Node(key, value);
} else if (key.compareTo(this.key) > 0) {
if (right != null)
right.add(key, value);
else
right = new Node(key, value);
} else
this.value = value;
}
public boolean contains(Integer key) {
if (this.key.intValue() == (key)) {
return true;
}
if (key.compareTo(this.key) < 0)
return left == null ? null : left.contains(key);
else
return right == null ? null : right.contains(key);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论