diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..73f69e0958611ac6e00bde95641f6699030ad235 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..d4b854b8d4fa7be1ca526617ed1590623b835f8c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..b16effcb15582734970db92fdd35ef4b55314bc0 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Heap.java b/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..2ac4560e1795ab6a4ff303d5f10ab390e65fcc4e --- /dev/null +++ b/Heap.java @@ -0,0 +1,173 @@ + +public class Heap>{ + public T[] heap; + public int SIZE; + public int count; + + Heap(int size){ + SIZE = size; + // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable + heap = (T[])new Comparable[SIZE]; + count = 0; + } + + + public void add (T item){ + //插入不能大于数组 + if(count >= SIZE){ + // throw new Exception("Heap Overflow"); + System.out.println("Heap Full"); + return; + } + heap[count]=item; + //向上调整 + percolateUp(count); + count++; + + } + //向上过滤调整 + private void percolateUp(int count) { + // 到达堆顶之前,反复 + while(0 < count){ + int j = (count - 1) / 2; + // 适合位置跳出 + //比较大于1 小于-1 等于0 + int arr=heap[count].compareTo(heap[j]); + if(arr<0){ + break; + } + swap(count, j);// 换值 + count = j;// i继续往上 + } + } + + //2.交换方法 + public void swap(int i, int j){ + T temp = heap[i]; + heap[i] = heap[j]; + heap[j] = temp; + } + + //删除元素 + public T delete(){ + // 根节点摘除,更换为末尾节点,再置末尾为0 + T tempmax = heap[0]; + // 最后一个位置的节点 + heap[0] = heap[--count]; + // 下降 + percolateDown(count, 0); + // 置为0 + heap[count] = null; + return tempmax; + + } + //向下过滤调整 + private int percolateDown(int count, int i) { + // 下滤,n是大小,i是下标 + int j;// 子节点 + while(i != (j = ProperParent(count, i))){ + swap(i, j); + // 换位,将父节点继续往下 + i = j; + } + return i;// 返回最后下滤的位置 + + } + + private int ProperParent(int count, int i) { + return RChildValid(count, i) ? Bigger(Bigger(i, i * 2 + 1), i * 2 + 2) : + LChildValid(count, i) ? Bigger(i, i * 2 + 1) : i; + } + + // 1.1检测是否存在右节点 + public boolean RChildValid(int n, int i){ + if(i * 2 + 2 < n){ + return true; + } + return false; + } + // 1.2检测是否存在左节点 + public boolean LChildValid(int n, int i){ + if(i * 2 + 1 < n){ + return true; + } + return false; + } + // 1.3 对比方法 + public int Bigger(int i, int j){ + // j存在 + int arr=heap[i].compareTo(heap[j]); + if(j < count){ + return arr>0 ? i : j; + } + return i; + } + + + //堆排序 + /** + * 创建堆, + * @param arr 待排序列 + */ + public void heapSort(int[] arr) { + //创建堆 + for (int i = (arr.length - 1) / 2; i >= 0; i--) { + //从第一个非叶子结点从下至上,从右至左调整结构 + adjustHeap(arr, i, arr.length); + } + + //调整堆结构+交换堆顶元素与末尾元素 + for (int i = arr.length - 1; i > 0; i--) { + //将堆顶元素与末尾元素进行交换 + int temp = arr[i]; + arr[i] = arr[0]; + arr[0] = temp; + + //重新对堆进行调整 + adjustHeap(arr, 0, i); + } + } + + /** + * 调整堆 + * @param arr 待排序列 + * @param parent 父节点 + * @param length 待排序列尾元素索引 + */ + private static void adjustHeap(int[] arr, int parent, int length) { + //将temp作为父节点 + int temp = arr[parent]; + //左孩子 + int lChild = 2 * parent + 1; + + while (lChild < length) { + //右孩子 + int rChild = lChild + 1; + // 如果有右孩子结点,并且右孩子结点的值大于左孩子结点,则选取右孩子结点 + if (rChild < length && arr[lChild] < arr[rChild]) { + lChild++; + } + + // 如果父结点的值已经大于孩子结点的值,则直接结束 + if (temp >= arr[lChild]) { + break; + } + + // 把孩子结点的值赋给父结点 + arr[parent] = arr[lChild]; + + //选取孩子结点的左孩子结点,继续向下筛选 + parent = lChild; + lChild = 2 * lChild + 1; + } + arr[parent] = temp; + } + + // 3.遍历 + public void print(){ + for (T i:heap){ + System.out.print("i:"+i+" "); + } + System.out.println(); + } +} \ No newline at end of file diff --git a/Main.java b/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..f0cc27d51ed146935abbfd96a28bc89c5397a3e5 --- /dev/null +++ b/Main.java @@ -0,0 +1,48 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Heap heap = new Heap(20); + + int[] arr = {16, 7, 3, 20, 17, 8}; + for (int i : arr) { + System.out.print(i + " "); + } + System.out.println(); + heap.heapSort(arr); + + for (int i : arr) { + System.out.print(i + " "); + } + +// 菜单选项 +// String key = ""; +// Scanner scanner = new Scanner(System.in); +// while (true){ +// System.out.println("add:插入元素"); +// System.out.println("list:查询元素"); +// System.out.println("delete:删除元素"); +// System.out.println("exit:退出系统"); +// key = scanner.next(); +// switch (key){ +// case "add": +// System.out.print("请输入要插入的元素:"); +// int number = scanner.nextInt(); +// heap.add(number); +// break; +// case "list": +// heap.print(); +// break; +// case "delete": +// System.out.println("删除元素:"+heap.delete()); +// break; +// case "exit": +// scanner.close(); +// System.exit(0); +// default: +// break; +// +// } +// } + } +} diff --git a/README.md b/README.md index e931f2f0393b17470887781a3e4325a8e502941d..41aca87a2caf0d420ec69ba324b7a612a5db5f15 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,12 @@ ### 截止日期 -下次实验课上课前(12.16) +- 基础-1 下次实验课上课前(12.16) +- 基础-2 12.23 ### 基础 -1. 实现⼀个小顶堆或者⼤顶堆 +1. 实现小顶堆或者大顶堆 - (1) 定义堆的数据结构 - (2) 实现入堆函数 - (3) 实现出堆函数 @@ -20,7 +21,7 @@ ### 进阶 1. 利用优先级队列合并K个有序数组 -2. 求⼀组动态数据集合的最⼤Top K +2. 求一组动态数据集合的Top K ## 工作流程 diff --git a/lec07-heap.iml b/lec07-heap.iml new file mode 100644 index 0000000000000000000000000000000000000000..b107a2dd81165eaaf682ad3da030668b937fbb6c --- /dev/null +++ b/lec07-heap.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/lec07-heap/.gitignore b/out/production/lec07-heap/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..be4e21604f0029f5107fa5b6efaf8f36f0518239 --- /dev/null +++ b/out/production/lec07-heap/.gitignore @@ -0,0 +1,7 @@ +gen-branch.js +.DS_Store +package.json +package-lock.json +node_modules +id.txt +run.sh diff --git a/out/production/lec07-heap/.idea/.gitignore b/out/production/lec07-heap/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..73f69e0958611ac6e00bde95641f6699030ad235 --- /dev/null +++ b/out/production/lec07-heap/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/out/production/lec07-heap/.idea/misc.xml b/out/production/lec07-heap/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..d4b854b8d4fa7be1ca526617ed1590623b835f8c --- /dev/null +++ b/out/production/lec07-heap/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/lec07-heap/.idea/modules.xml b/out/production/lec07-heap/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..b16effcb15582734970db92fdd35ef4b55314bc0 --- /dev/null +++ b/out/production/lec07-heap/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/lec07-heap/.idea/vcs.xml b/out/production/lec07-heap/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/out/production/lec07-heap/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lec07-heap/Heap.class b/out/production/lec07-heap/Heap.class new file mode 100644 index 0000000000000000000000000000000000000000..c652c7a2f1d67e63965a6b1312ce3d78cef6a5c9 Binary files /dev/null and b/out/production/lec07-heap/Heap.class differ diff --git a/out/production/lec07-heap/Main.class b/out/production/lec07-heap/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..399108f53fdd73371a7e3b41c2552925b004526d Binary files /dev/null and b/out/production/lec07-heap/Main.class differ diff --git a/out/production/lec07-heap/README.md b/out/production/lec07-heap/README.md new file mode 100644 index 0000000000000000000000000000000000000000..41aca87a2caf0d420ec69ba324b7a612a5db5f15 --- /dev/null +++ b/out/production/lec07-heap/README.md @@ -0,0 +1,37 @@ +# 散列表实验 + +### 作业要求 + +1. 必做任务:基础部分 +2. 选做任务:进阶部分,做多少是多少 + +### 截止日期 + +- 基础-1 下次实验课上课前(12.16) +- 基础-2 12.23 + +### 基础 + +1. 实现小顶堆或者大顶堆 + - (1) 定义堆的数据结构 + - (2) 实现入堆函数 + - (3) 实现出堆函数 +2. 实现堆排序 + +### 进阶 + +1. 利用优先级队列合并K个有序数组 +2. 求一组动态数据集合的Top K + +## 工作流程 + +1. Fork 本仓库 +2. Clone 代码到本地 +3. 切换到自己学号对应的分支 +4. 完成作业 +5. 提交代码 +6. 新建 Pull Request + +## Questions + +1. 如何提交?lec02-linkedlist 项目中的参考作业提交流程.pdf 文件 diff --git a/out/production/lec07-heap/lec07-heap.iml b/out/production/lec07-heap/lec07-heap.iml new file mode 100644 index 0000000000000000000000000000000000000000..b107a2dd81165eaaf682ad3da030668b937fbb6c --- /dev/null +++ b/out/production/lec07-heap/lec07-heap.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git "a/out/production/lec07-heap/\350\257\276\344\273\266/lecture07-pq.pdf" "b/out/production/lec07-heap/\350\257\276\344\273\266/lecture07-pq.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..bf751be1e6720ccb13d80c752dc2587677aabb6e Binary files /dev/null and "b/out/production/lec07-heap/\350\257\276\344\273\266/lecture07-pq.pdf" differ