Created
March 15, 2023 18:48
-
-
Save skatiyar/7023a2658eee00f1a3b05d4e7787de89 to your computer and use it in GitHub Desktop.
MinHeap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func parent(i int) int { | |
return (i - 1) / 2 | |
} | |
func left(i int) int { | |
return (2 * i) + 1 | |
} | |
func right(i int) int { | |
return (2 * i) + 2 | |
} | |
type minheap struct { | |
heap []int | |
} | |
func (h *minheap) swap(i, j int) { | |
h.heap[i], h.heap[j] = h.heap[j], h.heap[i] | |
} | |
func (h *minheap) insert(val int) { | |
h.heap = append(h.heap, val) | |
h.heapifyUp(len(h.heap) - 1) | |
} | |
func (h *minheap) delete(val int) { | |
lidx := len(h.heap) - 1 | |
for i := 0; i <= lidx; i += 1 { | |
if h.heap[i] == val { | |
h.heap[i] = h.heap[lidx] | |
h.heap = h.heap[:lidx] | |
h.heapifyDown(i) | |
break | |
} | |
} | |
} | |
func (h *minheap) getmin() int { | |
return h.heap[0] | |
} | |
func (h *minheap) deletemin() int { | |
lidx := len(h.heap) - 1 | |
temp := 0 | |
temp, h.heap[0] = h.heap[0], h.heap[lidx] | |
h.heap = h.heap[:lidx] | |
h.heapifyDown(0) | |
return temp | |
} | |
func (h *minheap) decreasekey(key, sub int) { | |
lidx := len(h.heap) - 1 | |
for i := 0; i <= lidx; i += 1 { | |
if h.heap[i] == key { | |
h.heap[i] = h.heap[i] - sub | |
if i > 0 && h.heap[i] < h.heap[parent(i)] { | |
h.heapifyUp(i) | |
} else { | |
h.heapifyDown(i) | |
} | |
break | |
} | |
} | |
} | |
func (h *minheap) print() { | |
fmt.Println(h.heap) | |
} | |
func (h *minheap) heapifyUp(idx int) { | |
for h.heap[parent(idx)] > h.heap[idx] { | |
h.swap(parent(idx), idx) | |
idx = parent(idx) | |
} | |
} | |
func (h *minheap) heapifyDown(idx int) { | |
lheap := len(h.heap) | |
for idx < len(h.heap) { | |
lidx, ridx := left(idx), right(idx) | |
if lidx < lheap && h.heap[lidx] < h.heap[idx] { | |
idx = lidx | |
} | |
if ridx < lheap && h.heap[ridx] < h.heap[idx] { | |
idx = ridx | |
} | |
if h.heap[parent(idx)] > h.heap[idx] { | |
h.swap(parent(idx), idx) | |
} else { | |
break | |
} | |
} | |
} | |
func (h *minheap) height() int { | |
hlen := len(h.heap) | |
if hlen != 0 { | |
return int(math.Floor(math.Log2(float64(hlen)))) + 1 | |
} | |
return 0 | |
} | |
func main() { | |
tree := &minheap{ | |
heap: make([]int, 0), | |
} | |
// insert | |
tree.insert(2) | |
tree.insert(12) | |
tree.insert(1) | |
tree.insert(-2) | |
tree.insert(23) | |
tree.insert(-7) | |
tree.insert(5) | |
tree.insert(22) | |
tree.print() | |
// delete | |
tree.delete(23) | |
tree.print() | |
// sub | |
tree.decreasekey(22, 22) | |
tree.print() | |
// sub | |
tree.decreasekey(-2, -5) | |
tree.print() | |
// get min | |
fmt.Println(tree.getmin()) | |
fmt.Println(tree.deletemin()) | |
fmt.Println(tree.getmin()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment