我正在尝试使用insert()填充二叉搜索树,但是每当我'打印'我的BST的内容时,我只会获得插入BST的最后一项。我需要修复哪些以确保我的所有价值都保留在BST中?
从debuggin,我认为我的问题是我的public void insert()将新值设置为root evertime它被调用。我不知道如何解决它?
这是我的BST CLass:
public class BinarySearchTree<T extends Comparable<T>> {
private class BinarySearchTreeNode<E>{
public BinarySearchTreeNode<E> left, right;
private E data;
private BinarySearchTreeNode (E data) {
this.data = data;
}
}
private BinarySearchTreeNode<T> root;
public boolean isEmpty() {
return root == null;
}
private BinarySearchTreeNode<T> insert(T value, BinarySearchTreeNode<T> ptr) {
if (ptr == null){
ptr = new BinarySearchTreeNode<>(value);
return ptr;
}
int compare = value.compareTo(ptr.data); //when ptr != null, this line and below should execute for each bstStrings.inster(x)
/* pass the value (s1...sN) when compared to (ptr.data) to compare
* when value and ptr.data == 0 re
*/
if (compare == 0) {
return ptr;
}
if (compare < 0) {
while (ptr.left != null){
ptr = ptr.left;
if (ptr.left == null) {//found insertion point
BinarySearchTreeNode<T> node = new BinarySearchTreeNode<>(value);
ptr = ptr.left;
ptr = node;
return ptr;
}
}
}
else {
return insert(value, ptr.left);
}
if (compare > 0) {
if (ptr.right == null) {
BinarySearchTreeNode<T> node = new BinarySearchTreeNode<>(value);
ptr = ptr.right;
ptr = node;
return ptr;
}
}
else {
return insert(value, ptr.right);
}
return ptr;
}
public void insert(T value) {
root = insert(value, root); //****Where I believe the problem is******
}
private void printTree(BinarySearchTreeNode<T>node){
if(node != null){
printTree(node.left);
System.out.println(" " + node.data);
printTree(node.right);
}
}
public void printTree(){
printTree(root);
System.out.println();
}
}
对于添加的上下文,这里是我的Main(),我调用insert()并尝试将字符串插入BST:
public class Main {
public static void main(String[] args) {
BinarySearchTree<String> bstStrings = new BinarySearchTree<String>();
String s = "Hello";
String s1 = "World";
String s2 = "This Morning";
String s3 = "It's";
bstStrings.insert(s);
bstStrings.insert(s1);
bstStrings.insert(s2);
bstStrings.insert(s3); //the only inserted value that is printed below
bstStrings.printTree();
System.out.println();
System.out.println("You should have values above this line!");
}
}
最后我的控制台输出:
It's
You should have values above this line!
答案 0 :(得分:4)
一些提示:
insert
内看不到任何递归调用。如果没有适当的递归调用(如果基于值进入当前节点的左子树或右子树),您将如何遍历BST?我确实看到一些注释掉的代码看起来会执行这些调用。他们为什么评论出来?root
。这会将root
设置为每次都指向新节点 。我认为这不是你想要的。root
是否为null
,然后将新节点设置为该值。 / LI>
ptr
。由于您的BST维护对root
的引用,因此您始终可以引用树的根。每次插入时,都从root
开始,然后以递归方式遍历树,直到找到合适的位置来插入新节点。如果你真的必须返回引用,那么你肯定不应该将root
设置为该新节点!这是一些帮助你的伪代码:
// Recursive function that inserts a value into a BST
function insert(node, value):
//Handles the case where you have no nodes in the tree, so root is null
if node is null:
node = new Node(value)
// If the value is lesser than the current node's value, we need to insert it
// somewhere in the right subtree
else if value < node.value:
if node.right is null:
// This node doesn't have a right child, so let's insert the new node here
node.right = new Node(value)
else:
// This node has a right child, so let's go further into this subtree to
// find the right place to insert the new node
insert(node.right, value)
// If the value is greater than the current node's value, we need to insert it
// somewhere in the left subtree
else if value > node.value:
if node.left is null:
// This node doesn't have a left child, so let's insert the new node here
node.left = new Node(value)
else:
// This node has a left child, so let's go further into this subtree to
// find the right place to insert the new node
insert(node.left, value)
else:
// A node with this value already exists so let's print out an erro
error("Node with that value already exists")
end function