由于某些原因,add(value)函数不希望工作。我应该可以使用Node和TreeNode来创建一个孩子。这不是一棵平衡的树。我尝试了Node和NodeTree,并在节点上创建一个变量并将其添加进去,但没有成功试图向java中的树添加节点
public abstract class TreeNode implements Comparable<TreeNode>{
protected int value;
protected TreeNode left;
protected TreeNode right;
public abstract int getValue();
public abstract int getSize();
public abstract TreeNode getLeft();
public abstract TreeNode getRight();
public void add(int value){
if (value >= this.value){
if (this.right == null){
this.right = new Node(value); //trying to put a node in the "right"
}else{
right.add(value);
}
}else if(value < this.value){
if (this.left == null){
this.left = new Node(value); //trying to do the same thing here
}else{
left.add(value);
}
}
}
public String toString() {
return (left.toString() + ", " +Integer.toString(this.value) + ", " + right.toString());
}
public int CompareTo(TreeNode obj){
if(this.value > obj.value){
return 1;
}else if(this.value < value){
return -1;
}else{
return 0;
}
}
//public void remove(int value) throws NotFoundException{
//}
}
你输入了什么值?你期望会发生什么,实际发生了什么?价值分配的价值在哪里? – Steve 2013-05-01 22:26:37
你说你已经试过'Node'和'NodeTree'。你尝试过'TreeNode'吗? – 2013-05-01 22:28:23