From 8b8c66e45faf6b0471847663015b5c466a30a2ae Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:02:09 +0800 Subject: [PATCH 1/4] [add] template code --- Heap.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Heap.java diff --git a/Heap.java b/Heap.java new file mode 100644 index 0000000..dd6535a --- /dev/null +++ b/Heap.java @@ -0,0 +1,30 @@ + +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; + } + + } + + public void delete(T item){ + + } + + public void print(){ + } +} \ No newline at end of file -- Gitee From c204d9d38d1a1887c5e56f027814d045f97d96dc Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:06:55 +0800 Subject: [PATCH 2/4] update README.md. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e931f2f..9dd8882 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ### 基础 -1. 实现⼀个小顶堆或者⼤顶堆 +1. 实现小顶堆或者大顶堆 - (1) 定义堆的数据结构 - (2) 实现入堆函数 - (3) 实现出堆函数 @@ -20,7 +20,7 @@ ### 进阶 1. 利用优先级队列合并K个有序数组 -2. 求⼀组动态数据集合的最⼤Top K +2. 求一组动态数据集合的Top K ## 工作流程 -- Gitee From c3305e480f1e72f57694a54a4d78215ca818c675 Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:07:50 +0800 Subject: [PATCH 3/4] update README.md. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9dd8882..41aca87 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ ### 截止日期 -下次实验课上课前(12.16) +- 基础-1 下次实验课上课前(12.16) +- 基础-2 12.23 ### 基础 -- Gitee From 45cd1f2f2ea38c09438f29d3dd57025914dc339c Mon Sep 17 00:00:00 2001 From: 13306922088 <1643531577@qq.com> Date: Tue, 15 Dec 2020 21:18:23 +0800 Subject: [PATCH 4/4] 25 --- Heap.java | 85 +++++++++++++++++++++++++++++++++++++++++++++++++------ Test.java | 11 +++++++ 2 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 Test.java diff --git a/Heap.java b/Heap.java index dd6535a..71b18b5 100644 --- a/Heap.java +++ b/Heap.java @@ -1,30 +1,97 @@ - -public class Heap>{ +public class Heap> {//堆 public T[] heap; public int SIZE; public int count; - Heap(int size){ + Heap(int size) { SIZE = size; // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable - heap = (T[])new Comparable[SIZE]; + heap = (T[]) new Comparable[SIZE]; count = 0; + } - public void add (T item){ - if(count >= SIZE){ + public void add(T item) { + + if (count >= SIZE) { // throw new Exception("Heap Overflow"); System.out.println("Heap Full"); return; } + heap[count] = item; + int index = count; + count++; + if (count > 1) { + while (index != 0) { + if (heap[index].compareTo(heap[(index - 1) / 2]) > 0) { + T temp = heap[index]; + heap[index] = heap[(index - 1) / 2]; + heap[(index - 1) / 2] = temp; + } + index = (index - 1) / 2; + } + } } - public void delete(T item){ - + public void delete() { + heap[0] = heap[count - 1]; + heap[count - 1] = null; + for (int i = 0; i < heap.length - 1; i++) { + if (heap[i].compareTo(heap[(2 * i) + 1]) < 0) { + T temp = heap[i]; + heap[i] = heap[(2 * i + 1)]; + heap[(2 * i + 1)] = temp; + } + if (heap[i].compareTo(heap[(2 * i) + 2]) < 0) { + T index = heap[i]; + heap[i] = heap[(2 * i + 2)]; + heap[(2 * i + 2)] = index; + } + return; + } } - public void print(){ + public void print() {//遍历 + int a = 2; + int number = 2; + int sum = 1; + int spaceCount = 40; + int spaceCount1 = 40; + for(int j = 0; j < spaceCount;j ++){ + System.out.print("\u00A0"); + } + System.out.print(" \t\u00A0"); + spaceCount--; + for (int i = 0; i < heap.length; i++) {//for (T t : heap) + System.out.print(heap[i] + "\u00A0"); + //int pow = (int)Math.pow(2, a); + if(i == 0){ + System.out.println(" "); + for(int j = 0; j < spaceCount;j ++){ + System.out.print("\u00A0"); + } + spaceCount-=2; + } + else if(number == i){//每一层都换行 + System.out.println(""); + for(int j = 0; j < spaceCount;j ++){ + System.out.print("\u00A0"); + } + spaceCount-=2; + number = number + (int)Math.pow(2, a); + a++; + } + if (i == sum){ + for(int j = 0; j < spaceCount1;j ++){ + System.out.print("\u00A0"); + } + spaceCount1-=8; + sum = 2 * sum + 2; + } + + } + System.out.println(); } } \ No newline at end of file diff --git a/Test.java b/Test.java new file mode 100644 index 0000000..27ea278 --- /dev/null +++ b/Test.java @@ -0,0 +1,11 @@ +public class Test { + public static void main(String[] args) { + Heap heap = new Heap(50); + for (int i = 0; i <= 50; i++) { + heap.add(i); + } + heap.print(); + //heap.delete(); + //heap.print(); + } +} \ No newline at end of file -- Gitee