- public class Heap {
-
- private static final int DEFAULT_SIZE = 10;
- private int[] array;
- private int tail;
-
- public Heap() {
- array = new int[DEFAULT_SIZE];
- tail = -1;
- }
-
- public Heap(int initialSize) {
- array = new int[initialSize];
- tail = -1;
- }
-
-
- @param
- @param
- public Heap(int[] data, int method) {
- this(data.length);
- switch (method) {
- case 1:
- constructHeapWilliams(data);
- break;
- case 2:
- constructHeapFloyd(data);
- break;
- default:
- throw new RuntimeException("Bad param method: method thould be 1 or 2");
- }
- }
-
- private void constructHeapWilliams(int[] data) {
- for (int i : data) {
- this.heapEnqueue(i);
- }
- }
-
- private void constructHeapFloyd(int[] data) {
- System.arraycopy(data, 0, this.array, 0, data.length);
- int lastBranch = this.array.length/2 - 1;
- for (int i = lastBranch; i >= 0; i--) {
- moveDown(this.array, i, this.array.length - 1);
- }
- tail = data.length - 1;
- }
-
- private void moveDown(int[] data, int first, int last) {
- int larger = 2 * first + 1;
- while (larger <= last) {
- if (larger < last && data[larger] < data[larger + 1]) {
- larger++;
- }
- if (data[first] < data[larger]) {
-
- int temp = array[first];
- array[first] = array[larger];
- array[larger] = temp;
-
- first = larger;
- larger = 2 * first + 1;
- } else {
- larger = last + 1;
- }
- }
- }
- private void reAllocateHeap() {
- int[] tmp = new int[2 * array.length];
- System.arraycopy(array, 0, tmp, 0, tail + 1);
- array = tmp;
- }
-
- public boolean isEmpty() {
- return tail == -1;
- }
-
- public void heapEnqueue(int el) {
- if (tail + 1 == array.length) {
- this.reAllocateHeap();
- }
- array[++tail] = el;
- int i = tail;
- int j = (i - 1)/2;
- while (i != 0 && el > array[j]) {
-
- int temp = array[j];
- array[j] = array[i];
- array[i] = temp;
-
- i = j;
- j = (i - 1)/2;
- }
- }
-
- public int heapDequeue() {
- if (tail == -1) {
- throw new RuntimeException("there is no element in heap");
- }
- int el = array[0];
- array[0] = array[tail--];
- if (tail < 1) {
- return el;
- }
- int lastBranch = (tail - 1)/2;
- int i = 0;
- int j = 2 * i + 1;
- if (j + 1 <= tail) {
- if (array[j] < array[j + 1]) {
- j++;
- }
- }
- while (i <= lastBranch && array[i] < array[j]) {
- int temp = array[j];
- array[j] = array[i];
- array[i] = temp;
-
- i = j;
- j = 2 * i + 1;
- if (j + 1 <= tail) {
- if (array[j] < array[j + 1]) {
- j++;
- }
- }
- }
- return el;
- }
-
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("[");
- for (int i = 0; i < this.tail; i++) {
- sb.append(this.array[i] + ",");
- }
- sb.append(this.array[this.tail] + "]");
- return sb.toString();
- }
-
- public boolean equals(Object o) {
- if (o instanceof Heap) {
- Heap h = (Heap)o;
- if (this.tail != h.tail) {
- return false;
- }
- for (int i = 0; i < this.tail + 1; i++) {
- if (this.array[i] != h.array[i]) {
- return false;
- }
- }
- return true;
- } else {
- return false;
- }
- }
-
- private static void test() {
- Heap heap = new Heap();
- for (int i = 0; i < 10; i++) {
- heap.heapEnqueue(i);
- }
- System.out.println(heap.toString());
- while (!heap.isEmpty()) {
- System.out.println(heap.heapDequeue());
- }
- int[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, };
- Heap heap2 = new Heap(data, 1);
- System.out.println(heap2.toString());
- Heap heap3 = new Heap(data, 2);
- System.out.println(heap3.toString());
- }
-
- public static void main(String[] args) {
- test();
- }
- }
|