-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnthmax.go
82 lines (78 loc) · 1.55 KB
/
nthmax.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
)
type maxheap struct{
h [] int
}
func (m *maxheap)buildmaxheap(arr []int) {
if len(arr) == 0 {
fmt.Println("empty array cant be create a heap")
}
fmt.Println(arr)
for _ , v := range arr {
m.insert(v)
}
}
func (m *maxheap) insert(ele int) {
m.h = append(m.h, ele)
m.heapifyUP(len(m.h)-1)
}
func (m *maxheap) heapifyUP(index int ){
for m.h[index] > m.h[parent(index)]{
m.swap(index , parent(index))
index = parent(index)
}
}
func (m *maxheap) swap(i1 , i2 int){
m.h[i1] , m.h[i2] = m.h[i2] , m.h[i1]
}
func parent (index int)int{
return (index - 1) / 2
}
func left(index int )int{
return 2 * index + 1
}
func right(index int)int{
return 2 * index + 2
}
func (m *maxheap)extract() int {
if len(m.h) == 0 {
fmt.Printf("Empty Heap")
return -1
}
ele := m.h[0]
m.h[0] , m.h[len(m.h)-1] = m.h[len(m.h)-1] , m.h[0]
m.h = m.h[:len(m.h)-1]
m.heapifyDown(0)
return ele
}
func (m *maxheap)heapifyDown(index int) {
l , r := left(index) , right(index)
lastindex := len(m.h)-1
var childToCompare int
for l <= lastindex{
if l == lastindex{
childToCompare = l
}else if(m.h[l] > m.h[r]){
childToCompare = l
}else{
childToCompare =r
}
if m.h[childToCompare] > m.h[index]{
m.h[childToCompare] , m.h[index] = m.h[index] , m.h[childToCompare]
index = childToCompare
l , r = left(childToCompare),right(childToCompare)
}else{
return
}
}
}
func main(){
heap := &maxheap{}
arr := []int{32,45,35,39,85,95,20,23}
heap.buildmaxheap(arr)
for _,_ = range heap.h{
fmt.Printf("%d ",heap.extract())
}
}