I am working on a program that creates several lists of random numbers, then runs sort algorithms on them and tracks the time it takes them to run. I've had no trouble building the lists, and running the standard sorts (bubble, merge, insert etc).
My problem is that now I am trying to build a binary search tree using my lists, and am really confused. Using my book as a guide I have classes for node and tree basics, but I don't understand how to actually build the tree.
I believe the algorithm needs to be recursive, something like: middle item from list becomes root node, item to left of it is left child, item to right of it is right child.
I'm not sure where to go from there. I don't even know if that will work. Please guide me if you can, I have been stuck on this a few days now.
I can attach the code I already have, but I don't think most of it is relavant as it works fine. I'm pretty sure my brain is turned to jello now, and this part of my code is probably useless.
public static BinaryTree<Integer> treeBuilder(int arrayStart, int arrayEnd, int arrayMid){ if(!(arrayMid == arrayStart) && !(arrayMid == arrayEnd)){ BT.attachLeft(array[arrayMid - 1]); treeBuilder(0, arrayMid, arrayMid/2); BT.attachRight(array[arrayMid + 1]); treeBuilder(arrayMid+1, array.length, (array.length - arrayMid) / 2); } return BT; }