Java没有操作符重载。你不能把可比较的类型和
>
. 你需要使用
root.val.compareTo(newNode.val)
相反。
作为旁白:
-
-
您不需要指定
<P extends Comparable<P>>
-
也许移动这个按钮更有意义
addValHelper
-
Node
实施
Comparable
这样,您的代码感觉更加习惯化,并且您不会将节点的字段公开给BST。
public class BST<T implements Comparable<T>> {
private final Node<T> root;
/** Presumably this is run when a value is added.. */
private void addValueHelper(Node rootNode, Node newNode) {
rootNode.attachChild(newNode);
}
public static class Node implements Comparable<T> {
private final T val;
private Node left;
private Node right;
public Node(T val) {
this.val = val;
}
public int compareTo(Node other) {
return this.val.compareTo(other.val);
}
/**
* Takes the given node and compares it with the current node.
* If the current node is greater than the given node, the given node is placed to the left.
* Otherwise it is placed to the right.
*/
protected void attachChild(Node newNode) {
if (this.compareTo(newNode) == 1) {
if (this.left == null) {
this.left = newNode;
return;
}
this.left.attachChild(newNode);
return;
}
if (this.right == null) {
this.right = newNode;
return;
}
this.right.attachChild(newNode);
}
}
}