为什么在将 BST 中的节点键值与 0 进行比较时不能使用 .compareTo()?
Why can I not use .compareTo() when comparing the key of a a node in a BST to 0?
- 这不是 class 中包含的所有代码,但如果这还不够,我会添加其余部分。
- add() 用于使用键将值添加到 BST 中的正确位置。如果密钥已经存在,则什么都不做。
contains() 应该 return 如果指定的键在树中
则为真
```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 );
}
}
问题是 int
是原始的,因此没有实现 Comparable,因此您不能使用 int.compareTo,但是盒装变体 Integer 可以。您可以简单地使用 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 ? null : left.contains(key);
else
return right == null ? null : right.contains(key);
}
}
- 这不是 class 中包含的所有代码,但如果这还不够,我会添加其余部分。
- add() 用于使用键将值添加到 BST 中的正确位置。如果密钥已经存在,则什么都不做。
contains() 应该 return 如果指定的键在树中
则为真```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 ); } }
问题是 int
是原始的,因此没有实现 Comparable,因此您不能使用 int.compareTo,但是盒装变体 Integer 可以。您可以简单地使用 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 ? null : left.contains(key);
else
return right == null ? null : right.contains(key);
}
}