heap is a lightweight package that provides binary min and max binary heap in Go. for seeing usages check this.
go get github.com/erfanmomeniii/heap
Next, include it in your application:
import "github.com/erfanmomeniii/heap"
The following examples illustrates how to use this package for creating max and min binary heap tree:
package main
import (
"fmt"
"github.com/erfanmomeniii/heap"
)
func main() {
h := heap.NewMax()
h.Insert(4)
h.Insert(12)
h.Insert(10)
max,_:= h.GetMax()
fmt.Println(max)
// 12
h.Delete()
max,_=h.GetMax()
fmt.Println(max)
// 10
}
package main
import (
"fmt"
"github.com/erfanmomeniii/heap"
)
func main() {
h := heap.NewMin()
h.Insert(4)
h.Insert(12)
h.Insert(10)
min,_:= h.GetMin()
fmt.Println(min)
// 4
h.Delete()
min,_=h.GetMin()
fmt.Println(min)
// 10
}
We can also update the value of a heap node by using an alias. Here is an example:
package main
import (
"fmt"
"github.com/erfanmomeniii/heap"
)
func main() {
h := heap.NewMin()
h.Insert(4, "number 1")
h.Insert(12, "number 2")
h.Insert(10, "number 3")
m, a := h.GetMin()
fmt.Println(m,a)
// 4
// number 1
h.Update("number 1", 13)
m, a = h.GetMin()
fmt.Println(m,a)
// 10
// number 3
}
Pull requests are welcome. For changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.