diff --git a/Heap.java b/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..8beb48527ca074e93146fcd6c0105377f7b097af --- /dev/null +++ b/Heap.java @@ -0,0 +1,156 @@ +package 数据结构; + +public class Heap { + public int[] heap; + public int SIZE; + public int count; + + Heap(int size) { + SIZE = size; + heap = new int[SIZE]; + count = 0; + } + + public void add(int item) { + if (count >= SIZE) { + System.out.println("Heap Full"); + return; + } + if (count == 0) { + heap[count] = item; + count++; + return; + } + if (count >= 1) { + int index = count; + heap[index] = item; + int ind; + if (index % 2 == 0) { + ind = (index / 2) - 1; + } else { + ind = index / 2; + } + while (heap[ind] < heap[index]) { + if (index % 2 == 0) { + int parent = (index / 2) - 1; + + if (heap[index] > heap[parent]) { + int temp; + temp = heap[parent]; + heap[parent] = heap[index]; + heap[index] = temp; + } + } else { + int parent = index / 2; + + if (heap[index] > heap[parent]) { + int temp; + temp = heap[parent]; + heap[parent] = heap[index]; + heap[index] = temp; + } + } + index = ind; + if (ind % 2 == 0) { + ind = ind / 2 - 1; + } else { + ind = ind / 2; + } + if (ind == -1) { + break; + } + } + count++; + } + } + + public void delete() { + if (count <= 0) { + System.out.println("Heap Empty"); + return; + } + if (count == 1) { + heap[0] = 0; + count--; + return; + } + + heap[0] = heap[count - 1]; + heap[count - 1] = 0; + count--; + int index = 0; + int left = 2 * index + 1; + int right = 2 * index + 2; + while (heap[index] < heap[left] || heap[index] < heap[right]) { + if (heap[left] > heap[right]) { + int temp = heap[index]; + heap[index] = heap[left]; + heap[left] = temp; + index = left;//3 + left = 2 * index + 1; + right = 2 * index + 2; + if (left + 1 > SIZE && right + 1 > SIZE) { + return; + } + if (left + 1 <= SIZE && right + 1 > SIZE) { + if (heap[index] > heap[left]) { + return; + } else { + temp = heap[index]; + heap[index] = heap[left]; + heap[left] = temp; + return; + } + } + } else { + int temp = heap[index]; + heap[index] = heap[right]; + heap[right] = temp; + index = right; + left = 2 * index + 1; + right = 2 * index + 2; + if (left + 1 > SIZE && right + 1 > SIZE) { + return; + } + if (left + 1 <= SIZE && right + 1 > SIZE) { + if (heap[index] > heap[left]) { + return; + } else { + temp = heap[index]; + heap[index] = heap[left]; + heap[left] = temp; + return; + } + } + + + } + } + } + + public void print() { + for (int j = 1; j <= 4; j++) { + int tail = (int) (Math.pow(2, j) - 1); + int head = tail - (int) Math.pow(2, j - 1); + if (tail == (int) (Math.pow(2, 4) - 1)) { + int sheng = tail - SIZE; + tail = tail - sheng; + } + for (int i = head; i < tail; i++) { + if (SIZE == tail - 1) { + return; + } + if (j == 1) { + System.out.print(" " + heap[i]); + } else if (j == 2) { + System.out.print(" " + heap[i] + " "); + } else if (j == 3) { + System.out.print(" " + heap[i] + " "); + } else if (j == 4) { + System.out.print(" " + heap[i] + " "); + } + } + System.out.println(); + } + } +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..913ecfb7ef82223ac3a69d08a5dcd7de0dd8b21b --- /dev/null +++ b/Main.java @@ -0,0 +1,26 @@ +package 数据结构; + +public class Main { + public static void main(String args[]) { + Heap hp = new Heap(10); + hp.add(10); + hp.add(9); + hp.add(8); + hp.add(7); + hp.add(6); + hp.add(5); + hp.add(4); + hp.add(3); + hp.add(2); + hp.add(1); + + hp.print(); + System.out.println("数组有"+ hp.count+"个数"); + + hp.delete(); + hp.print(); + System.out.println("数组有"+ hp.count+"个数"); + + } +} +