Java二进制搜索树插入方法不工作

Java二进制搜索树插入方法不工作,java,binary-search-tree,Java,Binary Search Tree,我的BST insert方法有问题。它只是在左边插入我的名字列表,而不是在右边递归和适当地平衡。这就是我的输出。这个名字列表有125个名字,所以我把它缩短以适应这个窗口。在输出下面,我输入了BST java程序的insert方法的java代码,以及主方法的一部分。我希望有人能看到我的问题,因为我和我的导师找不到问题 前序遍历: 沃尔特:零 |-劳伦斯:空 |-肯:空 |-珍妮花:空 |-大卫:空 |-沃尔特:空 |-菲尔:空 |-斯科蒂:空 |-托德:空 |-莱纳德:无效 |-卡拉:空 |-米歇

我的BST insert方法有问题。它只是在左边插入我的名字列表,而不是在右边递归和适当地平衡。这就是我的输出。这个名字列表有125个名字,所以我把它缩短以适应这个窗口。在输出下面,我输入了BST java程序的insert方法的java代码,以及主方法的一部分。我希望有人能看到我的问题,因为我和我的导师找不到问题

前序遍历: 沃尔特:零 |-劳伦斯:空 |-肯:空 |-珍妮花:空 |-大卫:空 |-沃尔特:空 |-菲尔:空 |-斯科蒂:空 |-托德:空 |-莱纳德:无效 |-卡拉:空 |-米歇尔:空 |-吉尔:空 |-史蒂文:空 |-永利:零 |-劳埃德:零

public void insertLeft(E key, T value) {
    Node<E, T> node = new Node<E, T>(key, value);
    if (root == null) {
        root = node;
    } else {
        Node current = root;
        while (current.left != null) {
            current = current.left;
        }
        current.left = node;
    }
}

//User friendly THE ONLY THING THAT DOES NOT WORK!!!!!!!
public boolean insert(E key, T value) {
    return insert(root, key, value);
}

public boolean insert(Node position, E key, T value) {
    if (position == null) {
        root = new Node<E, T>(key, value);
        return true;
    }
    int comparison = position.key.compareTo(key);
    if (comparison > 0) {
        if (position.left == null) {;
            position.left = new Node<E, T>(key, value);
            return true;
        }
        return insert(position.left, key, value);
    } else if (comparison < 0) {
        if (position.right == null) {
            position.right = new Node<E, T>(key, value);
            return true;
        }
        return insert(position.right, key, value);
    } else {
        return false;
    }
}

树向左边生长,因为这是您在代码中告诉它的

for(String dataAdd : nameData)
    bst.insertLeft(dataAdd, null);
顾名思义,insertLeft只在左侧插入,将树更像是一个链表

public void insertLeft(E key, T value)
{
    Node<E, T> node = new Node<E, T>(key, value);
    if(root == null)
        root = node;
    else
    {
        Node current = root;
        while(current.left != null)
            current = current.left;
        current.left = node;
    }
}
请注意,对于调用insertSorted后要排序的树,该树必须已经排序


还请注意,空树是排序的,只有一个节点的树也是排序的,这可能是因为您只使用insertLeft,它只在左侧插入新节点,实际上根本没有排序。您正在尝试使用insertLeft生成已排序的树吗?如果soIt应该按照字符串中列出的方式插入,那么它是一个名称很差的函数。在那之后我必须平衡它。insertLeft只是一个尝试和错误的东西,看看我们是否能让它进入。但我们也尝试过其他方法,这一个最终进入了名单,但我们不知道除了这一点,该怎么办。我是单独做一个insertRight呢,还是可以单独使用insert方法?更常见的做法是在插入时排序,而不是在插入后尝试平衡树。你想做什么?还要注意,插入时平衡是一件痛苦的事情,而排序则不是。这是我的项目要求的一部分。能够将每个名称添加到二叉树节点中–一次一个。按照二叉树插入的通用算法将每个节点放入树中。下一页列出了最少要使用的名称。您可以让程序通过文件读取或硬代码加载名称。在任何一种情况下,都必须使用您设计的“添加”方法一次输入1个名称。姓名将按照其列出的确切顺序读取/添加。
public void insertLeft(E key, T value)
{
    Node<E, T> node = new Node<E, T>(key, value);
    if(root == null)
        root = node;
    else
    {
        Node current = root;
        while(current.left != null)
            current = current.left;
        current.left = node;
    }
}
public void insertSorted(E key, T value)
{
    Node<E, T> node = new Node<E, T>(key, value);
    if(root == null) //keep the same base case
        root = node;
    else
    {
        Node current = root;
        while(true)
            if(node < current) //check left
                if(current.left == null) //set left
                {
                    current.left = node;
                    break;
                }
                else //iterate over left
                    current = current.left;
            else //check right
                if(current.right == null) //set right
                {
                    current.right = node;
                    break;
                }
                else //iterate over right
                    current = current.right;
    }
}