This is a creation in Article, where the information may have evolved or changed.
Heap operations are provided for any type that implements the heap interface. The (Kenggen) heap is a tree with a "minimum value for each node that is the root of the subtree" property. The smallest element of the tree is at the root, which is index 0.
Heap is a common method of implementing a priority queue. To create a priority queue, implement a heap interface that has a less method that uses a (negative) priority as the basis for comparison, so that the item with the highest priority of the queue can be removed with a pop by using the push Add item.
Type Interface
type Interface interface { sort.Interface Push(x interface{}) // add x as element Len() Pop() interface{} // remove and return element Len() - 1.}
As you can see, this heap structure inherits from sort. Interface, and sort. Interface, there are three methods that need to be implemented: Len () int/less (i, J int) Bool/swap (i, J int) plus two methods defined by the heap interface: Push (x interface{})/Pop () Inter face{}. So as long as these five methods are implemented, a heap is defined.
Any type that implements this interface can be used to build the minimum heap. The minimum heap can be passed through the heap. Init is established, the data is increment order or empty is also the smallest heap. The minimum heap constraints are:
!h.less (J, I) for 0 <= i < H.len () and 2*i+1 <= J <= 2*i+2 and J < H.len ()
Note the push and pop methods for the interface are called for the heap package, use the heap. Push and Heap.pop to add or remove elements to a heap.
Func fix (H Interface, I int)//After modifying the element I, it is more efficient to call this function to fix the heap than to insert a new element after removing the element I. The complexity of O (log (n)), where n equals H. Len ().
Func Init (H Interface)//Initializes a heap. A heap should be initialized before any heap operations are used. The init function is idempotent to the heap (execution is meaningless multiple times) and may be called at any time when the constraint of the heap is broken. The complexity of this function is O (n), where n equals H. Len ().
Func POPs (H Interface) interface{}//Deletes and returns the smallest element in heap H (without affecting constraints). The complexity of O (log (n)), where n equals H. Len (). The function is equivalent to remove (h, 0).
The Func Push (H Interface, x interface{})//Shang H inserts the element x and keeps the heap constrained. The complexity of O (log (n)), where n equals H. Len ().
Func Remove (h Interface, I int) interface{}//Deletes the element I in the heap and preserves the constraints of the heap. The complexity of O (log (n)), where n equals H. Len ().
Examples of how to use it:
Package Mainimport ("Container/heap" "FMT")//An intheap is a min-heap of Ints.type intheap []intfunc (H intheap) Len () int {return len (h)}func (H intheap) Less (i, J int) bool {return h[i] < H[j]}func (H intheap) Swap (i, J int) {H[i], h[j] = H[j], H[i]}func (H *intheap) push (x interface{}) {//Push and POP use pointer receivers because they MODIF Y the slice ' s length,//not just it contents.*h = append (*h, x. (int))}func (H *intheap) Pop () interface{} {old: = *hn: = Len (old) x: = Old[n-1]*h = Old[0:n-1]return x}//This example inserts several ints to an intheap, checks the minimum,// and removes them in order of Priority.func main () {h: = &intheap{2, 1, 5, 3, 6, 4, 5}heap. Init (h) heap. Push (H, 3) heap. Fix (H, 3) fmt. Printf ("Minimum:%d\n", (*H) [0]) for H.len () > 0 {fmt. Printf ("%d", heap. Pop (h))}}
Create a priority queue using the heap
This example demonstrates a priority queue built using the heap interface.package mainimport ("Container/heap" "FMT")// An item was something we manage in a priority Queue.type Item struct {value string//The value of the item; ARBITRARY.P riority int//The priority of the item in the queue.//the-index is needed by update and are maintained by the heap. Interface Methods.index Int//Index of the item in the heap.} A Priorityqueue implements Heap. Interface and holds Items.type Priorityqueue []*itemfunc (PQ Priorityqueue) len () int {return Len (PQ)}func (PQ Priorityq Ueue) Less (i, J int) bool {//We want POPs to give us the highest, not lowest, priority so We use greater than Here.return Pq[i].priority > Pq[j].priority}func (PQ priorityqueue) Swap (i, J int) {Pq[i], pq[j] = pq[j], Pq[i]pq[i].index = Ipq[j] . Index = J}func (PQ *priorityqueue) Push (x interface{}) {n: = Len (*PQ) Item: = x. (*item) Item.index = N*PQ = Append (*PQ, ite m)}func (PQ *priorityqueue) Pop () interface{} {oLD: = *pqn: = Len (old) Item: = Old[n-1]item.index =-1//For SAFETY*PQ = Old[0:n-1]return item}//update modifies the PR Iority and value of an item in the Queue.func (PQ *priorityqueue) Update (Item *item, value string, priority int) {Item.val UE = Valueitem.priority = Priorityheap. Fix (PQ, Item.index)}//This example creates a priorityqueue with some items, adds and manipulates an item,//and then Remo Ves the items in priority Order.func Main () {//Some items and their priorities.items: = map[string]int{"Banana": 3, "appl E ": 2," pear ": 4,}//Create a priority queue, put the items in it, and//establish the priority queue (heap) INVARIANTS.PQ : = Make (Priorityqueue, Len (items)) I: = 0for value, Priority: = Range Items {Pq[i] = &item{value:value,priority:p Riority,index:i,}i++}heap. Init (&PQ)//Insert a new item and then modify its priority.item: = &item{value: "Orange", priority:1,}heap. Push (&PQ, item) pq.update (item, Item.value, 5)//Take the items out; They Arrive in decreasing priority order.for PQ. Len () > 0 {item: = heap. Pop (&PQ). (*item) fmt. Printf ("%.2d:%s \ n", Item.priority, Item.value)}}
Reference: https://golang.org/pkg/container/heap/