From 63c27a9ab3ceba9645632005d127cc2631185b18 Mon Sep 17 00:00:00 2001 From: lee <554050334@qq.com> Date: Sat, 1 Dec 2018 12:31:20 +0800 Subject: [PATCH 1/2] add heap insert and delete in golang --- go/28_heap/heap.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 go/28_heap/heap.go diff --git a/go/28_heap/heap.go b/go/28_heap/heap.go new file mode 100644 index 00000000..f978c131 --- /dev/null +++ b/go/28_heap/heap.go @@ -0,0 +1,79 @@ +package heap + +import "fmt" + +type Heap struct { + a []int + n int + count int +} + +//init heap +func NewHeap(capacity int) *Heap { + heap := &Heap{} + heap.n = capacity + heap.a = make([]int, capacity+1) + heap.count = 0 + return heap +} + +//top-max heap -> heapify from down to up +func (heap *Heap) insert(data int) { + //defensive + if heap.count == heap.n { + return + } + + heap.count++ + heap.a[heap.count] = data + + //compare with parent node + i := heap.count + parent := i / 2 + for parent > 0 && heap.a[parent] < heap.a[i] { + swap(heap.a, parent, i) + i = parent + parent = i / 2 + } +} + +//heapfify from up to down +func (heap *Heap) removeMax() { + + //defensive + if heap.count == 0 { + return + } + + //swap max and last + swap(heap.a, 1, heap.count) + heap.count-- + + //comapre with left and right + for i := 1; i <= heap.count/2; { + + maxIndex := i + if heap.a[i] < heap.a[i*2] { + maxIndex = i * 2 + } + + if i*2+1 <= heap.count && heap.a[maxIndex] < heap.a[i*2+1] { + maxIndex = i*2 + 1 + } + + if maxIndex == i { + break + } + + swap(heap.a, i, maxIndex) + i = maxIndex + } + +} + +//swap two elements +func swap(a []int, i int, j int) { + tmp := a[i] + a[i] = a[j] + a[j] = tmp +} From 086276d4d01967cd8d48cd56d236105760bd638f Mon Sep 17 00:00:00 2001 From: lee <554050334@qq.com> Date: Sat, 1 Dec 2018 13:43:00 +0800 Subject: [PATCH 2/2] add heap algo --- go/28_heap/heap.go | 18 ++++++++------ go/28_heap/heap_sort.go | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 go/28_heap/heap_sort.go diff --git a/go/28_heap/heap.go b/go/28_heap/heap.go index f978c131..0d9327ee 100644 --- a/go/28_heap/heap.go +++ b/go/28_heap/heap.go @@ -1,7 +1,5 @@ package heap -import "fmt" - type Heap struct { a []int n int @@ -49,15 +47,21 @@ func (heap *Heap) removeMax() { swap(heap.a, 1, heap.count) heap.count-- - //comapre with left and right - for i := 1; i <= heap.count/2; { + //heapify from up to down + heapifyUpToDown(heap.a, heap.count) +} + +//heapify +func heapifyUpToDown(a []int, count int) { + + for i := 1; i <= count/2; { maxIndex := i - if heap.a[i] < heap.a[i*2] { + if a[i] < a[i*2] { maxIndex = i * 2 } - if i*2+1 <= heap.count && heap.a[maxIndex] < heap.a[i*2+1] { + if i*2+1 <= count && a[maxIndex] < a[i*2+1] { maxIndex = i*2 + 1 } @@ -65,7 +69,7 @@ func (heap *Heap) removeMax() { break } - swap(heap.a, i, maxIndex) + swap(a, i, maxIndex) i = maxIndex } diff --git a/go/28_heap/heap_sort.go b/go/28_heap/heap_sort.go new file mode 100644 index 00000000..76171335 --- /dev/null +++ b/go/28_heap/heap_sort.go @@ -0,0 +1,54 @@ +package heap + +//build a heap +func buidHeap(a []int, n int) { + + //heapify from the last parent node + for i := n / 2; i >= 1; i-- { + heapifyUpToDown(a, i, n) + } + +} + +//sort by ascend, a index begin from 1, has n elements +func sort(a []int, n int) { + buidHeap(a, n) + + k := n + for k >= 1 { + swap(a, 1, k) + heapifyUpToDown(a, 1, k-1) + k-- + } +} + +//heapify from up to down , node index = top +func heapifyUpToDown(a []int, top int, count int) { + + for i := top; i <= count/2; { + + maxIndex := i + if a[i] < a[i*2] { + maxIndex = i * 2 + } + + if i*2+1 <= count && a[maxIndex] < a[i*2+1] { + maxIndex = i*2 + 1 + } + + if maxIndex == i { + break + } + + swap(a, i, maxIndex) + i = maxIndex + } + +} + +//swap two elements +func swap(a []int, i int, j int) { + tmp := a[i] + a[i] = a[j] + a[j] = tmp +}