/* 4.2 Given a sorted (increasing order) array, write an algorithm to create
 * a binary search tree with minimal height.
 * */

public class P0403 {
   private class Node {
      int value;
      Node left, right;

      public Node(int value) {
         this.value = value;
      }
   }

   public Node toBST(int[] data) {
      return toBST(data, 0, data.length - 1);
   }

   private Node toBST(int[] data, int start, int end) {
      if (start == end)
         return new Node(data[start]);

      if (end - start == 1) {
         Node root = new Node(data[start]);
         root.right = new Node(data[end]);
         return root;
      }

      int mid = (start + end) / 2;
      Node root = new Node(data[mid]);
      root.left = toBST(data, start, mid - 1);
      root.right = toBST(data, mid + 1, end);

      return root;
   }

   public void inorder(Node root) {
      if (root == null)
         return;

      inorder(root.left);
      System.out.print(root.value + " ");
      inorder(root.right);
   }

   public static void main(String[] args) {
      int[] data = {1, 2, 3, 4, 5, 6, 7, 8};

      P0403 p0403 = new P0403();
      Node root = p0403.toBST(data);
      p0403.inorder(root);
      System.out.println();
   }
}

@@ -437,17 +437,24 @@
    i
-f
+nt mid =
  (start 
 == e
@@ -453,55 +453,19 @@
 art 
-==
++
  end)
-%0A         return new Node(data%5Bstart%5D);%0A
+ / 2;
 %0A   
@@ -475,24 +475,20 @@
 if (
-end -
+mid ==
  start
- == 1
 ) %7B%0A
@@ -527,24 +527,55 @@
 ta%5Bstart%5D);%0A
+%0A         if (end != start)%0A   
          roo
@@ -601,24 +601,25 @@
 data%5Bend%5D);%0A
+%0A
          ret
@@ -641,43 +641,8 @@
  %7D%0A%0A
-      int mid = (start + end) / 2;%0A
     
@@ -1062,16 +1062,59 @@
 , 7, 8%7D;
+%0A      int%5B%5D data1 = %7B1, 2, 3, 4, 5, 6, 7%7D;
 %0A%0A      
@@ -1231,16 +1231,105 @@
 intln();
+%0A%0A      root = p0403.toBST(data1);%0A      p0403.inorder(root);%0A      System.out.println();
 %0A   %7D%0A%7D%0A

make code more elegant