diff --git a/Heap.class b/Heap.class new file mode 100644 index 0000000000000000000000000000000000000000..f3a0f7267ab207e77b8cd42965b681a19ebccf24 Binary files /dev/null and b/Heap.class differ diff --git a/Heap.java b/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..6a4355293e4786f40dd512f9d3d98fc50109b3f8 --- /dev/null +++ b/Heap.java @@ -0,0 +1,105 @@ +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(count>0){ + int j = (count - 1) / 2; + int arr=heap[count].compareTo(heap[j]); + if(arr<0){ + break; + } + swap(count, j); + count = j; + } + } + + public void swap(int i, int j){ + T temp = heap[i]; + heap[i] = heap[j]; + heap[j] = temp; + } + + + public void print(){ + System.out.print("\n"); + for (T i:heap){ + System.out.print(i+"\t"); + } + } + + +public T delete(){ + + T tempmax = heap[0]; + + heap[0] = heap[--count]; + + percolateDown(count, 0); + + heap[count] = null; + return tempmax; + +} + +private int percolateDown(int count, int 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; +} + +public boolean RChildValid(int n, int i){ + if(i * 2 + 2 < n){ + return true; + } + return false; +} + +public boolean LChildValid(int n, int i){ + if(i * 2 + 1 < n){ + return true; + } + return false; +} + +public int Bigger(int i, int j){ + + int arr=heap[i].compareTo(heap[j]); + if(j < count){ + return arr>0 ? i : j; + } + return i; +} + + +} \ No newline at end of file diff --git a/Main.class b/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..8b42cffed5c4e8f1b0987633a52aecc2f0d696c1 Binary files /dev/null and b/Main.class differ diff --git a/Main.java b/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..0fd25fe43b353831d4ca94d370575bdaabc25bdd --- /dev/null +++ b/Main.java @@ -0,0 +1,18 @@ +class Main { + public static void main(String[] args){ + Heap heap = new Heap(10); + heap.add(0); + + heap.add(3); + heap.add(9); + heap.add(1); + heap.add(5); + heap.add(4); + heap.add(18); + heap.add(21); + heap.print(); + + heap.delete(); + heap.print(); + } +} \ No newline at end of file diff --git a/README.md b/README.md index e931f2f0393b17470887781a3e4325a8e502941d..9dd8882c8161eb4dbe74e57bd0daf54e64983cae 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 ## 工作流程