0
votes

I have an issue with my BST insert method. It is only inserting my names list to the left and not recursing to the right and balancing appropriately. This is what my output looks like. This names list has 125 names so I cut it short to fit in this window. Below the output, I have put in my java code for the insert method of my BST java program, and part of the main method. I hope someone can see my problem because my tutor and I cannot find the issue.

Preorder traversal: Walt:null |-Lawrence:null |-Ken:null |-Jennifer:null |-David:null |-Walter:null |-Phil:null |-Scotty:null |-Todd:null |-Leonard:null |-Kara:null |-Michelle:null |-Jill:null |-Steven:null |-Wynn:null |-Lloyd:null

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
Perhaps it's because you're only using insertLeft, which only inserts new nodes to the left, and is not actually sorted at all. Are you trying to make a sorted tree using insertLeft? It's a poorly named function if so - phflack
It is supposed to insert as listed in the string. I have to balance it after that. The insertLeft was just a trial and error thing to see if we could get it to enter. But we have tried others and this one finally enters the names list but we don't know what to do beyond this. Do I make a separate insertRight or can I just use the insert method on its own? - Rusty
It's more common to sort on inserting, rather than trying to balance the tree after. What are you trying to do? - phflack
Also note that balancing is a pain to do on insertion, while sorting is not. Change binary search tree to balance - phflack
This is part of my project requirement. Ability to add each name into a binary tree node – one at a time. Place each node into the tree following the general algorithm for binary tree insertion. (The minimum list of names to be used is on the next page). You may have the program load the names via a file read OR by hard code. In either case, you must enter the names 1 at a time using the “add” method you designed. Names will be read in/added in the EXACT order that they are listed. - Rusty

1 Answers

1
votes

The tree is growing to the left because that's what you're telling it in the code

for(String dataAdd : nameData)
    bst.insertLeft(dataAdd, null);

insertLeft, as the name suggests, only inserts on the left, treating the tree more like a linked list

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

The structure of the insertLeft method works well, checking the base case if the tree is empty (root == null), before traversing the tree to add to the left

If you expect it to work as insert for a sorted tree (Binary Seach Tree), you may wish to edit it a bit to take into account sorting order

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

Note that for the tree to be sorted after calling insertSorted, the tree must already be sorted

Also note that an empty tree is sorted, as is a tree with only 1 node