I have an Array of Strings that are in order A-Z. I was wondering the best way to go about sorting them for a balanced binary search tree. My initial thought is to split the array up into the first half and the second half and then sort them individually.
Shouldn't I be able to use a recursive way to keep splitting it in half to get the next Node for the tree? I just can't wrap my head around it right now and thought I would ask if any one had any ideas. to lead me in the right direction or provide some examples. Thanks!
i am using my own BinaryTree Class and BinaryTreeNode Class. EDIT:
public class BinaryTree {
private BinaryTreeNode root;
public void insert(String text) {
root = insertNode(root, text);
}
private BinaryTreeNode insertNode(BinaryTreeNode curNode, String text) {
if (curNode == null) {
BinaryTreeNode newNode = new BinaryTreeNode(text);
//newNode.value = text;
return newNode;
} else {
if (text.compareTo(curNode.value) < 0 ) {
//left child
//use of recursion to properly place Node
curNode.left = insertNode(curNode.left, text);
return curNode;
}
else {
//right
//use of recursion to properly place Node
curNode.right = insertNode(curNode.right, text);
return curNode;
}
}
}
public BinaryTreeNode getRoot() {
return root;
}
public void setRoot(BinaryTreeNode root) {
this.root = root;
}
}
would this be considered a Self balancing binary search tree?