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

时间:2017-12-21 19:58:23

标签: java binary-search-tree

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

预订遍历: 沃尔特:空     | -Lawrence:空         | -Ken:空             | -Jennifer:空                 | -David:空                     | -Walter:空                         | -Phil:空                             | -Scotty:空                                 | -Todd:空                                     | -Leonard:空                                         | -Kara:空                                             | -Michelle:空                                                 | -Jill:空                                                     | -Steven:空                                                         | -Wynn:空                                                             | -Lloyd:空

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;
    }
}

public static void main(String [] args){         Main main = new Main();

    BinaryTree bst = new BinaryTree();
    //ADD DATA Oshiro stuff
    String namesList = "Walt Lawrence Ken Jennifer David Walter Phil Scotty "
            + "Todd Leonard Kara Michelle Jill Steven Wynn Lloyd Brandon Gary"
            + " Jim Dale Joyce Don Tom Christine Rachel Jeff Raymond Kelli"
            + " Charles Kevin Brant Joseph Michael Kelly Jessie Suzie Sally"
            + " Christian Terry John Art Francis Riki Evelyn Tony Ikaika Joe"
            + " Ann Neil Daniel Willie James Jeremy Aislynn Larry Celeste"
            + " Paige Dennis Fred Rosa Ryan George Gabe Lance Carolyn Mariah"
            + " Hal Christina Christopher Mark Stephen Stanley Sharon Hannah"
            + " Gregory Barry Kawika Greg Derek Philip Alfredo Jillian Joedie"
            + " Anthony Kyle Bradley Masa Clyde Robert Zachary Jaron Fernando"
            + " Kosuke Becky Dora Rheada Ashley Dustin Joshuah Ricardo Pete"
            + " Katrina Arwin Mica Arlene Venus Jenny Nicole Jeylyn Trisha"
            + " Theresa Eric Terry Trenton Marcus Tristan Rueben Melvin"
            + " Kurtis Mary";
    //Use name for names and nameLength for number of names
    String[] nameData = namesList.split("\\s");
    boolean[] nameInsert = new boolean[nameData.length];
    for (String dataAdd : nameData) {
        bst.insertLeft(dataAdd, null);

    }
    System.out.println("\nThe Oshiro NamesList has been added to the tree, printing tree:");
    bst.traverse(1);
    System.out.println(bst.toString());

    Scanner userInput = new Scanner(System.in);
    System.out.println("Options available:"
            + "\n[0]Add the names to the tree"
            + "\n[1]Delete a name from the tree"
            + "\n[2]Print out contents of tree in alphabetical order"
            + "\n[3]Print out contents of tree in reverse alphabetical order"
            + "\n[4]Search the tree for a specific name and return number of probes"
            + "\n[5]Destroy the tree"
            + "\n[6]Balance the tree"
            + "\n[7]Draw the tree as loaded"
            + "\n[8]Draw the tree completely balanced"
            + "\n[9]Draw the tree"
            + "\n[10]Exit");
    int selection;

1 个答案:

答案 0 :(得分:1)

树正在向左增长,因为这就是你在代码中告诉它的内容

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;
    }
}

insertLeft方法的结构运行良好,如果树为空(root == null),则检查基本情况,然后遍历树以添加到左侧

如果您希望它对于排序树( B inary S 每个 T ree)的insert有用,那么你可能希望编辑一下以考虑排序顺序

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;
    }
}

请注意,对于要在调用insertSorted后进行排序的树,必须已对树进行排序

另请注意,对空树进行排序,只有一个节点

的树