Created
July 10, 2014 16:00
-
-
Save bitcpf/7c5d19d378c7a595ef57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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