当前位置:网站首页>Go -- maximum heap and minimum heap
Go -- maximum heap and minimum heap
2022-06-30 10:21:00 【Fallacious】
The biggest pile ( Big pile top ): The heap top element remains at its maximum
The smallest pile ( Small cap pile ): The heap top elements are kept to a minimum
1、 The structure of the pile
A heap is a complete binary tree ( A complete binary tree is part of a full binary tree ), According to the structure of the tree, it can be represented by an array , As shown in the following figure, the complete binary tree can be represented as an array on the right 
As a large top pile , The following features also need to be observed :
- The root node is the maximum value of the whole heap
- The value of each node is not less than its child nodes
Corresponding to the array :
a[1] Is the maximum
The node is i, Then the child node is 2i and 2i+1, namely a[i]>=a[2i] && a[i]>=a[2i+1]
The node is i, Then the parent node is i/2
2、 Insert elements into the heap
When you insert a new element , You need to find its position in the array
//less() Compare element sizes ,exch() Swap two element positions
func push(k int, datas *[]int){
for i>1 && less((*datas)[i/2],(*datas)[i]){
// Compare i/2 and i The size of the position element , Exit when comparing to the root node
exch(&(*datas)[i/2], &(*datas)[i]) // i/2 The location element is less than i Location elements , The parent node element is smaller than the child node , Need to swap
i = i/2
}
}
func less(a int,b int) bool{
return a < b
}
3、 Pop up the elements in the heap
Pop up the largest element , Swap the last element with the largest element , Then adjust the position of the last element to the correct position
func pop(i int, datas []int){
length:=len(datas)-2
for 2*i<=length{
max:=0
if 2*i<length && less(datas[2*i], datas[2*i+1]){
// Find two child nodes 2i and 2i+1 Maximum of
max=2*i+1
}
if !less(datas[i], datas[max]){
// The parent node is larger than the child node , The exchange is over
break
}
// Child is greater than parent , In exchange for
exch(&datas[i], &datas[max])
i = max
}
}
4、go Heap in
go Provided in container/heap To implement heap , The types of elements in the heap need to implement heap.Interface This interface .
The following is an example of the maximum heap :
type IntHeap []int
func (h IntHeap) Len() int {
return len(h) }
func (h IntHeap) Less(i, j int) bool {
return h[i] > h[j] }
// The smallest pile
//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{
}) {
*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
}
func main() {
h := &IntHeap{
3, 1, 2, 5}
heap.Init(h)
heap.Push(h, 4)
for h.Len() > 0 {
fmt.Printf("%d ", heap.Pop(h))
}
}
If there is any wrong , Please point out ~
边栏推荐
- Launch of Rural Revitalization public welfare fund and release of public welfare bank for intangible cultural heritage protection of ancient tea tree
- Shell script functions
- 事件对象的说明》
- G code explanation | list of the most important G code commands
- KOREANO ESSENTIAL打造气质职场范
- What is the real performance of CK5, the king machine of CKB?
- 事件委托的使用与说明》
- 打通供应链 深圳礼品展助跨境电商寻破局之道
- Jump table introduction
- KOREANO ESSENTIAL打造气质职场范
猜你喜欢

JUL简介

NLopt--非线性优化--原理介绍及使用方法

A brief introduction to database mysql

G code explanation | list of the most important G code commands
![JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]](/img/01/6829e85bf28431eb06e70b87ceaaff.jpg)
JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]

Didn't receive robot state (joint angles) with recent timestamp within 1 seconds

Article content cannot be copied

JS obtient la chaîne spécifiée spécifiant la position du caractère & sous - chaîne spécifiant la plage de position du caractère 【 détails simples 】

机器人系统动力学——惯性参数

光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述
随机推荐
The human agent of kDa, Jinbei kd6, takes you to explore the metauniverse
Splendid China: public welfare tourism for the middle-aged and the elderly - entering Foshan nursing home
KOREANO ESSENTIAL打造气质职场范
“昆明城市咖啡地图”活动再度开启
Description of event object
How to seize the opportunity of NFT's "chaos"?
Ant s19xp appeared in 140t, why is it called the computing power ceiling by the world
Some domestic image sources
After recording 7000 IELTS words in 100 sentences, there are only 1043 words (including simple words such as I and you)
G code explanation | list of the most important G code commands
Who should the newly admitted miners bow to in front of the chip machine and the graphics card machine
Magnetic levitation 3D lamp
7. development of mobile login function
磁悬浮3D灯
Koreano essential creates a professional style
Questions about cookies and sessions
Shell script functions
How does the diode work?
What is the real performance of CK5, the king machine of CKB?
鼠标右键打开cmd(命令行)