自定义二进制搜索树，inOrder不起作用，需要输入，Custom Binary Search Tree, inOrder not working and need input,

``````private BTNode<T> root;
private int maxHeight;
private int numberOfNodes;

@Override
public int getMaxHeight() {
return maxHeight;
}

@Override
public int getNodeCount() {

return numberOfNodes;
}

@Override
public BTNode<T> getRoot() {

return root;
}
``````

//这是我认为问题开始的地方。

``````@Override
public BTNode<T> insert(T rec) {
BTNode<T> node = new BTNode<T>();
if (isEmpty()) {
root = node;
root.setRecord(rec);
root.setHeight(0);
} else
node = insertify(rec, root);
numberOfNodes++;

return node;
}

private BTNode<T> insertify(T rec, BTNode<T> node) {
int TVal = Integer.parseInt(rec.toString());
int Nodevalue = Integer.parseInt(node.getRecord().toString());
BTNode<T> newnode = null;

if (TVal < Nodevalue) {
if (node.hasLeft())
this.insertify(rec, node.getLeft());
else {
newnode = new BTNode<T>();
node.setLeft(newnode);

newnode.setRecord(rec);
newnode.setParent(node);
newnode.setHeight(newnode.getParent().getHeight() + 1);

if (newnode.getHeight() > maxHeight)
maxHeight = newnode.getHeight();

}

} else if (TVal > Nodevalue) {
if (node.hasRight())
this.insertify(rec, node.getRight());
} else {
newnode = new BTNode<T>();
node.setRight(newnode);

newnode.setRecord(rec);
newnode.setParent(node);
newnode.setHeight(newnode.getParent().getHeight() + 1);

if (newnode.getHeight() > maxHeight)
maxHeight = newnode.getHeight();
return newnode;
}

return newnode;

}

@Override
public boolean isEmpty() {

if (root == null)
return true;
else
return false;
}

ArrayList<T> inorder = new ArrayList<T>();

private void inordertra(BTNode<T> node) {
if (node.hasLeft()) {
inordertra(node.getLeft());
}

if (node.hasRight()) {
inordertra(node.getRight());
}
}

@Override
public String toStringInorder(BTNode<T> v) {
String s = "";
inorder.clear();
inordertra(root);

for (int i = 0; i < inorder.size(); i++) {
s =  s + inorder.get(i);
}
System.out.print(s);
return s;
}

}
``````

1 个回复1

===============>>#1 票数：0

``````@Override
public boolean isEmpty() {
if (root == null)
return true;
else
return false;
}
``````

``````@Override
public boolean isEmpty() {
return root == null;
}
``````