Skip to content

Priority Queue Min heap

Latest
Compare
Choose a tag to compare
@andela-sjames andela-sjames released this 25 Mar 20:01
· 1 commit to master since this release
  • Allow minheap to be created
package main

import (
    "fmt"
    pqueue "github.com/andela-sjames/priorityQueue"
)

func main() {

    // defaults to max heap if Min option is
    // not set to true
    maxheap := pqueue.NewHeap(pqueue.Options{})

    maxheap.InsertPriority("Visit China", 2)
    maxheap.InsertPriority("Visit Japan", 7)
    maxheap.InsertPriority("Eat Pizza", 2)
    maxheap.InsertPriority("Run marathon", 11)
    maxheap.InsertPriority("Buy Banana", 7)
    maxheap.InsertPriority("Go Fishing", 13)
    maxheap.InsertPriority("Skipping", 2)

    item, p := maxheap.ShowPriority()
    fmt.Println(item, p, "The priority") // Go Fishing 13 The priority

    s := maxheap.ShowHeap()
    fmt.Println(s)
    // [{"Item":"Go Fishing","Priority":13} {"Item":"Visit Japan","Priority":7} {"Item":"Run marathon","Priority":11} {"Item":"Visit China","Priority":2} {"Item":"Buy Banana","Priority":7} {"Item":"Eat Pizza","Priority":2} {"Item":"Skipping","Priority":2}]

    t := maxheap.ShowHashTable()
    fmt.Println(t) // &map[2:[3 5 6] 7:[1 4] 11:[2] 13:[0]]

    item, p = maxheap.Poll()
    fmt.Println(item, p, "The poll") // Go Fishing 13 The poll

    t = maxheap.ShowHashTable()
    fmt.Println(t) // &map[2:[3 5 2] 7:[1 4] 11:[0]]

    _ = maxheap.Remove(11)

    t = maxheap.ShowHashTable()
    fmt.Println(t) // &map[2:[3 2 4] 7:[0 1]]

    l := maxheap.Length()
    fmt.Println(t) // 5

    // Set Min option to true for minheap
    minheap := pqueue.NewHeap(pqueue.Options{
        Min: true,
    })

}