[英]Why wont my Binary Search Tree display more then 3 nodes?
My Binary Search Tree cant seem to manage more then 3 nodes or be able to display them. 我的二进制搜索树似乎无法管理超过3个节点或能够显示它们。 I needed to add them iterably and display them using a stack in the assignment but am having trouble displaying the BST if it has more then 3 elements.
我需要迭代地添加它们并在赋值中使用堆栈显示它们但是如果它有超过3个元素则无法显示BST。
public boolean add(E b) {
//if tree is empty
if (root == null) {
root = new Node();
root.setValue(b);
count++;
return true;
} //if val is already in tree
else if (b.compareTo(root.getValue()) == 0) {
return false;
//if left or right spot is free
} else {
while (root != null) {
compareResult = root.value.compareTo(b);
spot = root;
if (compareResult < 0) {
if (root.left != null) {
spot = spot.left;
count++;
return true;
} else {
Node n = new Node<>();
n.setValue(b);
spot.left = n;
count++;
return true;
}
} else if (compareResult > 0) {
if (root.right != null) {
spot = spot.right;
count++;
return true;
} else {
Node n = new Node<>();
n.setValue(b);
spot.right = n;
count++;
return true;
}
} else {
return false;
}
}
}
return false;
}
public void display() {
//needs to use an iterator
if (root == null) {
System.out.println("Error");
} else {
Node CurrentNode;
CurrentNode = root;
while (!stack.isEmpty() || CurrentNode != null) {
if (CurrentNode != null) {
stack.push(CurrentNode);
CurrentNode = CurrentNode.right;
}
else {
Node m= stack.pop();
System.out.println(m.value);
CurrentNode = m.left;
}
}
}
}
public int size() {
return count;
}
@Override
public int compareTo(E t) {
return ordering.compare(root,t);
}
@Override
public int compare(E t, E t1) {
return t.compareTo(t1);
}
The Node class looks like this Node类看起来像这样
public class Node<E extends Comparable> {
E value;
Node<E> left;
Node<E> right;
//constructor
public Node() {
value = null;
left = null;
right = null;
}
public void setValue(E t) {
value = t;
}
public void setLeft(Node<E> t) {
left = t;
}
public void setRight(Node<E> t) {
right = t;
}
public Node<E> getLeft() {
return left;
}
public Node<E> getRight() {
return right;
}
public E getValue() {
return value;
}
I can see a couple of changes that you need to make. 我可以看到你需要做出的一些改变。
After the } else {
line in your add
method, you should swap the next three lines around, to have 在你的
add
方法中的} else {
行之后,你应该交换接下来的三行,以便
spot = root;
while (spot != null) {
compareResult = spot.value.compareTo(b);
because spot
is going to track its way down the tree while you iterate the loop. 因为当你迭代循环时,
spot
会沿树向下移动。 But because you're currently using root
instead of spot
, you're always comparing values with the root
node, instead of lower down nodes. 但是因为你当前正在使用
root
而不是spot
,所以你总是将值与root
进行比较,而不是使用较低的节点。
Also, remove count++;
另外,删除
count++;
and return true;
并
return true;
from after spot = spot.left;
来自after
spot = spot.left;
and spot = spot.right;
和
spot = spot.right;
, because in these cases, you haven't actually added anything to the tree, so you need to continue on through the loop. ,因为在这些情况下,您实际上没有向树中添加任何内容,因此您需要继续循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.