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;
insertLeft, which only inserts new nodes to the left, and is not actually sorted at all. Are you trying to make a sorted tree usinginsertLeft? It's a poorly named function if so - phflack