0

我正在尝试使用 insert() 填充二叉搜索树,但是当我每次“打印”我的 BST 的内容时,我只会得到插入到 BST 中的最后一项。我需要解决什么问题才能确保我的所有价值都保留在 BST 中?

从调试中,我认为我的问题是我的 public void insert() 每次调用它时都会将新值设置为 root。我不知道如何解决它?

这是我的 BST 课程:

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!
4

1 回答 1

4

一些提示:

  • 我在里面看不到任何递归调用insert。如果没有适当的递归调用(根据值进入当前节点的左子树或右子树),您将如何遍历 BST?我确实看到了一些看起来像是会执行这些调用的注释掉的代码。为什么他们被注释掉了?
  • 您将返回新插入的节点,然后将其设置为root. 这将每次root设置指向新节点。我不认为那是你想要的。
  • 如果您尝试处理树为空的特殊情况,您需要做的就是检查 if rootis null,然后将新节点设置为该节点。
  • 实在是没必要回去了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
于 2013-03-13T16:34:56.447 回答