<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # package heap `import "container/heap"` heap包提供了對任意類型(實現了heap.Interface接口)的堆操作。(最小)堆是具有“每個節點都是以其為根的子樹中最小值”屬性的樹。 樹的最小元素為其根元素,索引0的位置。 heap是常用的實現優先隊列的方法。要創建一個優先隊列,實現一個具有使用(負的)優先級作為比較的依據的Less方法的Heap接口,如此一來可用Push添加項目而用Pop取出隊列最高優先級的項目。 Example (IntHeap) ``` // This example demonstrates an integer heap built using the heap interface. package heap_test import ( "container/heap" "fmt" ) // An IntHeap is a min-heap of ints. type IntHeap []int func (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 modify the slice's length, // not just its contents. *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } // This example inserts several ints into an IntHeap, checks the minimum, // and removes them in order of priority. func Example_intHeap() { h := &IntHeap{2, 1, 5} heap.Init(h) heap.Push(h, 3) fmt.Printf("minimum: %d\n", (*h)[0]) for h.Len() > 0 { fmt.Printf("%d ", heap.Pop(h)) } // Output: // minimum: 1 // 1 2 3 5 } ``` Example (PriorityQueue) ``` // This example demonstrates a priority queue built using the heap interface. package heap_test import ( "container/heap" "fmt" ) // An Item is something we manage in a priority queue. type Item struct { value string // The value of the item; arbitrary. priority int // The priority of the item in the queue. // The index is needed by update and is maintained by the heap.Interface methods. index int // The index of the item in the heap. } // A PriorityQueue implements heap.Interface and holds Items. type PriorityQueue []*Item func (pq PriorityQueue) Len() int { return len(pq) } func (pq PriorityQueue) Less(i, j int) bool { // We want Pop 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 = i pq[j].index = j } func (pq *PriorityQueue) Push(x interface{}) { n := len(*pq) item := x.(*Item) item.index = n *pq = append(*pq, item) } func (pq *PriorityQueue) Pop() interface{} { old := *pq n := len(old) item := old[n-1] item.index = -1 // for safety *pq = old[0 : n-1] return item } // update modifies the priority and value of an Item in the queue. func (pq *PriorityQueue) update(item *Item, value string, priority int) { item.value = value item.priority = priority heap.Fix(pq, item.index) } // This example creates a PriorityQueue with some items, adds and manipulates an item, // and then removes the items in priority order. func Example_priorityQueue() { // Some items and their priorities. items := map[string]int{ "banana": 3, "apple": 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 := 0 for value, priority := range items { pq[i] = &Item{ value: value, priority: priority, 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 ", item.priority, item.value) } // Output: // 05:orange 04:pear 03:banana 02:apple } ``` ## Index * [type Interface](#Interface) * [func Init(h Interface)](#Init) * [func Push(h Interface, x interface{})](#Push) * [func Pop(h Interface) interface{}](#Pop) * [func Remove(h Interface, i int) interface{}](#Remove) * [func Fix(h Interface, i int)](#Fix) ### Examples * [package (IntHeap)](#example-package--IntHeap) * [package (PriorityQueue)](#example-package--PriorityQueue) ## type [Interface](https://github.com/golang/go/blob/master/src/container/heap/heap.go#L30 "View Source") ``` type Interface interface { sort.Interface Push(x interface{}) // 向末尾添加元素 Pop() interface{} // 從末尾刪除元素 } ``` 任何實現了本接口的類型都可以用于構建最小堆。最小堆可以通過heap.Init建立,數據是遞增順序或者空的話也是最小堆。最小堆的約束條件是: ``` !h.Less(j, i) for 0 <= i < h.Len() and 2*i+1 <= j <= 2*i+2 and j < h.Len() ``` 注意接口的Push和Pop方法是供heap包調用的,請使用heap.Push和heap.Pop來向一個堆添加或者刪除元素。 ## func [Init](https://github.com/golang/go/blob/master/src/container/heap/heap.go#L41 "View Source") ``` func Init(h Interface) ``` 一個堆在使用任何堆操作之前應先初始化。Init函數對于堆的約束性是冪等的(多次執行無意義),并可能在任何時候堆的約束性被破壞時被調用。本函數復雜度為O(n),其中n等于h.Len()。 ## func [Push](https://github.com/golang/go/blob/master/src/container/heap/heap.go#L52 "View Source") ``` func Push(h Interface, x interface{}) ``` 向堆h中插入元素x,并保持堆的約束性。復雜度O(log(n)),其中n等于h.Len()。 ## func [Pop](https://github.com/golang/go/blob/master/src/container/heap/heap.go#L61 "View Source") ``` func Pop(h Interface) interface{} ``` 刪除并返回堆h中的最小元素(不影響約束性)。復雜度O(log(n)),其中n等于h.Len()。等價于Remove(h, 0)。 ## func [Remove](https://github.com/golang/go/blob/master/src/container/heap/heap.go#L71 "View Source") ``` func Remove(h Interface, i int) interface{} ``` 刪除堆中的第i個元素,并保持堆的約束性。復雜度O(log(n)),其中n等于h.Len()。 ## func [Fix](https://github.com/golang/go/blob/master/src/container/heap/heap.go#L85 "View Source") ``` func Fix(h Interface, i int) ``` 在修改第i個元素后,調用本函數修復堆,比刪除第i個元素后插入新元素更有效率。 復雜度O(log(n)),其中n等于h.Len()。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看