Ok this looks better, but gives a null pointer exception ?
public static BST idealTop(Comparable [] C){
BST X = new BST();
helperIdeal(C, X.root,(C.length/2));
return X;
}
public static void helperIdeal(Comparable [] C, node R,int mid){
if(mid<0||mid>C.length-1){
R.left = null;
R.right = null;
}
else{
R.data = C[mid];
helperIdeal(C,R.left,mid/2);
helperIdeal(C,R.right,(mid*2)-1);
}
}
I am going to consider this a homework question…
The trick to the best algorithm here is that the list is sorted and a BST when read along the bottom is always sorted too.
Think about your data structure and how you can reference each node in the tree. If you do it well, you should get a linear algorithm.