亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄 搜尋
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫/sql) database/sql/driver(數(shù)據(jù)庫/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語法樹) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達(dá)式) regexp/syntax runtime runtime(運行時) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競爭) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "container/heap"

  • 概述

  • 索引

  • 例子

概述

Heap 包為任何實現(xiàn) heap.Interface 的類型提供堆操作。堆是具有屬性的樹,每個節(jié)點是其子樹中的最小值節(jié)點。

樹中的最小元素是索引為0的根。

堆是實現(xiàn)優(yōu)先級隊列的常用方式。要構(gòu)建一個優(yōu)先級隊列,請將具有(負(fù))優(yōu)先級的 Heap 接口作為 Less 方法的排序來執(zhí)行,因此推送會添加項目,而Pop 將從隊列中移除最高優(yōu)先級的項目。這些例子包括這樣的實現(xiàn);文件example_pq_test.go 具有完整的源代碼。

示例 (IntHeap)

本示例將幾個 int 插入到 IntHeap 中,檢查最小值,并按優(yōu)先級順序刪除它們。

// 本示例演示了使用堆接口構(gòu)建的整數(shù)堆。package mainimport ("container/heap""fmt")// IntHeap是一個整數(shù)的整數(shù)。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和Pop使用指針接收器,因為它們修改切片的長度,// 不僅僅是其內(nèi)容。*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}// 本示例將幾個int插入到IntHeap中,檢查最小值,// 并按優(yōu)先順序刪除它們。func main() {
	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))}}

示例 (PriorityQueue)

本示例使用某些項目創(chuàng)建 PriorityQueue,添加并操作項目,然后按優(yōu)先級順序刪除項目。

// 此示例演示使用堆接口構(gòu)建的優(yōu)先級隊列。package mainimport ("container/heap""fmt")// item是我們在優(yōu)先隊列中管理的item。type Item struct {
	value    string // item的值;任意取值。
	priority int    // 隊列中項目的優(yōu)先級。// 該索引是更新所需的,由heap.Interface方法維護。
	index int // 堆中項目的索引。}// PriorityQueue實現(xiàn)堆。接口并保存項目。type PriorityQueue []*Itemfunc (pq PriorityQueue) Len() int { return len(pq) }func (pq PriorityQueue) Less(i, j int) bool {// 我們希望Pop給我們最高的優(yōu)先權(quán),而不是最低的優(yōu)先權(quán),所以我們使用的比這里更大。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}// 更新修改隊列中某個項目的優(yōu)先級和值。func (pq *PriorityQueue) update(item *Item, value string, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)}// 本示例使用某些項目創(chuàng)建PriorityQueue,添加和操作項目,// 然后按優(yōu)先順序刪除這些項目。func main() {// 一些項目和它們的優(yōu)先級。
	items := map[string]int{"banana": 3, "apple": 2, "pear": 4,}// 創(chuàng)建一個優(yōu)先級隊列,將其中的項目,和// 建立優(yōu)先隊列(堆)不變量。
	pq := make(PriorityQueue, len(items))
	i := 0for value, priority := range items {
		pq[i] = &Item{
			value:    value,
			priority: priority,
			index:    i,}
		i++}
	heap.Init(&pq)// 插入一個新項目,然后修改其優(yōu)先級。
	item := &Item{
		value:    "orange",
		priority: 1,}
	heap.Push(&pq, item)
	pq.update(item, item.value, 5)// 取出項目;以優(yōu)先順序遞減排列。for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		fmt.Printf("%.2d:%s ", item.priority, item.value)}}

索引

  • func Fix(h Interface, i int)

  • func Init(h Interface)

  • func Pop(h Interface) interface{}

  • func Push(h Interface, x interface{})

  • func Remove(h Interface, i int) interface{}

  • type Interface

示例

Package (IntHeap) Package (PriorityQueue)

包文件

heap.go

func Fix

func Fix(h Interface, i int)

Fix 在索引 i 處的元素已更改其值后重新建立堆排序。更改索引為 i 的元素的值,然后調(diào)用 Fix 相當(dāng)于調(diào)用 Remove(h, i),然后再調(diào)用新值的代價。復(fù)雜度為 O(log(n)),其中 n = h.Len()。

func Init

func Init(h Interface)

必須在使用任何堆操作之前初始化堆。Init 對于堆不變式是冪等的,當(dāng)堆不變量可能已經(jīng)失效時可以調(diào)用Init。它的復(fù)雜性是 O(n),其中n = h.Len()。

func Pop

func Pop(h Interface) interface{}

Pop 從堆中刪除最小元素(根據(jù)Less)并返回。復(fù)雜度為 O(log(n)),其中 n = h.Len()。它相當(dāng)于 Remove(h, 0)。

func Push

func Push(h Interface, x interface{})

推動元素 x 到堆上。復(fù)雜度為 O(log(n)),其中 n = h.Len()。

func Remove

func Remove(h Interface, i int) interface{}

從堆中刪除索引 i 處的元素。復(fù)雜度為 O(log(n)),其中 n = h.Len()。

type Interface

任何實現(xiàn)堆的類型 .Interface 可用作具有以下不變式的最小堆(在調(diào)用 Init 之后或數(shù)據(jù)為空或排序后建立):

!h.Less(j, i) for 0 <= i < h.Len() and 2*i+1 <= j <= 2*i+2 and j < h.Len()

請注意,此接口中的 Push 和 Pop 用于調(diào)用包堆的實現(xiàn)。要從堆中添加和刪除東西,請使用 heap.Push 和 heap.Pop。

type Interface interface {
        sort.Interface        Push(x interface{}) // 將x添加為元素Len()        Pop() interface{}   // 刪除并返回元素Len()-1.}
上一篇: 下一篇: