Skip to content

Instantly share code, notes, and snippets.

@bitcpf
Created July 10, 2014 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitcpf/7c5d19d378c7a595ef57 to your computer and use it in GitHub Desktop.
Save bitcpf/7c5d19d378c7a595ef57 to your computer and use it in GitHub Desktop.
package cc150_4_3;
import java.util.Arrays;
public class BTMH <Key extends Comparable<Key>> {
private Node root;
private class Node{
private int key;
private Node left, right;
public Node(int data){
this.key = data;
}
}
public BTMH (int[] data){
this.root = BTMHBuild(data,0,data.length-1);
}
public Node BTMHBuild(int[] keys, int low, int high){
if(low > high) return null;
int mid = high - (high - low)/2;
// System.out.println(mid);
Node temp = new Node(keys[mid]);
temp.left = BTMHBuild(keys,low, mid-1);
temp.right = BTMHBuild(keys,mid+1,high);
return temp;
}
public void printTree(Node head){
if(head == null) return;
printTree(head.left);
System.out.print(head.key + ", ");
printTree(head.right);
}
public static void main(String[] args){
int[] data = {1,3,4,5,6,7,8,9,11,22,34,56,69};
System.out.println(Arrays.toString(data));
BTMH<Integer> test = new BTMH<Integer>(data);
System.out.println(test.root.key);
test.printTree(test.root);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment