10.2 Make Binary Tree

http://www.davidlin.org/2011/06/how-to-build-balanced-binary-search.html

public class MakeBalanceTree {

    public Node makeTree(int[] array) {
        int low = 0;
        int high = array.length - 1;
        return makeTree(low, high, array);
      }

    private Node makeTree(int low, int high, int[] array) {
        if (low > high) {
          return null;
        }
        else {
          // Same as (low + high) / 2
          int mid = (low + high) >>> 1;
          Node node = new Node(array[mid]);
          node.left = makeTree(low, (mid - 1), array);
          node.right = makeTree((mid + 1), high, array);
          return node;
        }
      }
}
//    int[] myArray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//    bt.makeTree(myArray);

results matching ""

    No results matching ""