diff --git a/HomeWork/Heap.java b/HomeWork/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..e111796fc2f139c0c85d352ea768775bc55f2130 --- /dev/null +++ b/HomeWork/Heap.java @@ -0,0 +1,111 @@ + +public class Heap> { + public T[] heap; + public int SIZE; + public int count; + + Heap(int size) { + SIZE = size; + heap = (T[]) new Comparable[SIZE]; + count = 0; + } + + public void add(T item) { + if (count >= SIZE) { + System.out.println("Heap Full"); + return; + } + heap[count] = item; + count++; + int tempValue = count - 1; + while (true) { + if (heap[tempValue].compareTo(heap[(tempValue - 1) / 2]) > 0) { + T tempNode = heap[(tempValue - 1) / 2]; + heap[(tempValue - 1) / 2] = heap[tempValue]; + heap[tempValue] = tempNode; + tempValue = (tempValue - 1) / 2; + } else { + break; + } + } + } + + public void delete() { + heap[0] = heap[count - 1]; + heap[count - 1] = null; + count--; + for (int i = 0; i < count / 2; i++) { + if (heap[i * 2 + 1] != null) { + if (heap[i].compareTo(heap[i * 2 + 1]) < 0) { + T temp = heap[i]; + heap[i] = heap[i * 2 + 1]; + heap[i * 2 + 1] = temp; + } + } + if (heap[i * 2 + 2] != null) { + if (heap[i].compareTo(heap[i * 2 + 2]) < 0) { + T temp = heap[i]; + heap[i] = heap[i * 2 + 2]; + heap[i * 2 + 2] = temp; + } + } + } + } + + public void sortHeap(int[] arr) { + int arrCount = arr.length; + //先将数组先排序成大顶堆 + arr = sort(arrCount, arr); + //循环将堆顶和堆最后一个元素互换,然后除去最后一个元素,将数组继续排序成大顶堆 + while (arrCount/2>0){ + int temp=arr[0]; + arr[0]=arr[arrCount-1]; + arr[arrCount-1]=temp; + arrCount--; + arr= sort(arrCount,arr); + } + System.out.println(); + System.out.print("堆排序开始"); + for(int a:arr){ + System.out.print(" "+a); + } + } + + private int[] sort(int arrCount, int[] arr) { + for (int i = arrCount / 2 - 1; i >= 0; i--) { + //定义一个变量存放当前的位置 + int current = i; + //如果当前位置的左节点不为空,往下循环判断是不是大顶堆 + while ((current * 2 + 1) <= arrCount - 1) { + //定义一个变量存放左子树或者右子树中的最大值 + int maxNum = current; + //如果右子树不为空的话,两个系欸但值比较,否则左节点为最大值 + if ((current * 2 + 2) < arrCount) { + if (arr[current * 2 + 1] > arr[current * 2 + 2]) { + maxNum = current * 2 + 1; + } else { + maxNum = current * 2 + 2; + } + } else { + maxNum = current * 2 + 1; + } + //如果当前位置的节点小于左右中的最大节点,则把最大节点和当前位置互换,然后最大节点位置赋值给当前位置,当前位置继续进循环看是否满足顶堆 + if (arr[current] < arr[maxNum]) { + int temp = arr[current]; + arr[current] = arr[maxNum]; + arr[maxNum] = temp; + current = maxNum; + } else { + break; + } + } + } + return arr; + } + + public void print() { + for (int i = 0; i < count; i++) { + System.out.print(" " + heap[i]); + } + } +} \ No newline at end of file diff --git a/HomeWork/Test.java b/HomeWork/Test.java new file mode 100644 index 0000000000000000000000000000000000000000..e450f17a41e1a2bd0df0332a6de6b3be25999488 --- /dev/null +++ b/HomeWork/Test.java @@ -0,0 +1,22 @@ +public class Test { + public static void main(String[] args) { + Heap heap = new Heap(10); + int nodeNum[] = new int[]{8, 7, 6, 20, 5, 4, 22}; + initHeap(heap, nodeNum); + heap.delete(); + heap.delete(); + System.out.print("\n删除了2次最大值后:"); + heap.print(); + int[] arr = new int[]{5, 3, 9, 6, 15, 4, 11, 16, 20, 22, 25, 88, 66, 99, 77, 100, 8}; + heap.sortHeap(arr); + } + + private static void initHeap(Heap heap, int[] nodeNum) { + for (int i = 0; i < nodeNum.length; i++) { + heap.add(nodeNum[i]); + } + System.out.print("第一次构建堆:"); + heap.print(); + } + +}