当前位置:网站首页>Large top heap, small top heap and heap sequencing
Large top heap, small top heap and heap sequencing
2022-07-02 14:37:00 【Kallou】
Heap structure is a special logical structure , Through the heap sorting algorithm, the disorder can be stacked O(logn) The time sequence is a large top heap or a small top heap , So as to quickly obtain the maximum and minimum values .
1. The physical and logical structure of the heap
The logical structure of the heap is a Perfect binary tree , The actual physical structure in memory is a Sequential array .
A complete binary tree is a kind of tree in which every layer is Value from left to right until the layer is full, and then it will grow to a deeper structure .
The mapping relationship between the array of the heap and the heap structure satisfies : No i The left child of the first element is the 2*i+1 The elements of , The right child is the number one 2*i+2 The elements of , The parent node is the (i-1)/2 ( Take the whole number ,0 The parent node of bit is itself ) The elements of .


2. Large top pile and small top pile
Big pile top That is, for any one in the heap subtree Come on , Of the subtree Maximum It must be its The root node .

Small cap pile That is, for any one in the heap subtree Come on , Of the subtree minimum value It must be Its root node .

3. Implementation of related functions of heap structure
This example is a program that can sort the unordered heap into a large top heap , See notes for the meaning of each function .( The program does not give the main function , If you need to run, please write it yourself )
/*……………………………………… Heap sort O(logN)………………………………………*/
// The function of heapinsert An overloaded version of
// Three parameters in this version ,vec Represents an array that is already a large root heap ,size Represents the significant bits in the array ,num For the element to be inserted
template<typename T>
void heapinsert(vector<T>& vec, int size,T num)
{
vec.push_back(num);
while (vec[size] > vec[(size - 1) / 2])
{
swap(vec[(size - 1) / 2], vec[size]);
size = (size - 1) / 2;
}
return;
}
// The function of heapinsert An overloaded version of
// Two parameters in this version ,vec Indicates the heap to be inserted ,size Indicates the significant bit of the heap in this insertion
template<typename T>
void heapinsert(vector<T>& vec, int size)
{
while (vec[size] > vec[(size - 1) / 2])
{
swap(vec[(size - 1) / 2], vec[size]);
size = (size - 1) / 2;
}
return;
}
// Pile dive
template<typename T>
void _heapify(vector<T>& vec,int size)
{
int index = 0;
int left = index * 2 + 1;
while (left < size)
{
// This sentence is used to judge which child is older , Returns the subscript of the older child
//A?B:C The grammar rule of ? If the previous is true, it returns B False returns C,
// In this statement, if the right child crosses the boundary , be && The left is false ,? It was false before , return left;
// Keep looking back without crossing the line , Judge whether the left and right children are big or small .
int largest = left + 1 < size && vec[left] < vec[left + 1] ? left + 1 : left;
if (vec[index] < vec[largest])
{
swap(vec[index], vec[largest]);
index = largest;
left = index * 2 + 1;
}
else
break;
}
}
// Pop up maximum
template<typename T>
T pop_max(vector<T>& vec, int size)
{
// Swap the top of the heap element with the end of the heap
swap(vec[0], vec[size - 1]);
// Before the end of the heap ( The tail of the heap is not included ) Stack of heapify operation
_heapify(vec,size-1);
return vec[size-1];
}
// Heap sort
template<typename T>
void heapSort(vector<T>& vec)
{
if (vec.empty() || vec.size() < 2)
return;
// Change the input array into a large root heap
for (int i = 0; i < vec.size(); ++i)
heapinsert(vec,i);
for (int i = vec.size(); i > 0; --i)
pop_max(vec, i);
}
Heap sorting is compared with the parent node every time , If it is larger than the parent node, it will float up one layer , Therefore, the maximum number of floats is the number of layers of the heap , Heap is a complete binary tree , According to the properties of complete binary tree, the relationship between the number of layers and the number of elements is 2^h-1,h Is the number of layers , Therefore, the time complexity of heap sorting is O(logn).
Pile dive The meaning of is after taking out the root node of the heap , Sort the remaining nodes to restore them according to the original rules , Its time complexity is also O(logn).
Therefore, a complete unordered heap fetches the maximum value ( minimum value ) The time complexity of is O(2logn), The constant term can be ignored , In the end, it's still O(logn)
边栏推荐
- 关于Flink框架窗口(window)函数最全解析
- Openharmony notes --------- (4)
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
- Methods of software testing
- <口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
- 提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
- taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
- Fabric. JS manual bold text iText
- <口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
猜你喜欢
实现一个多进程并发的服务器
Yolov3 & yolov5 output result description
YoloV6训练:训练自己数据集遇到的各种问题
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
What is erdma? Popular science cartoon illustration
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
Actual combat sharing of shutter screen acquisition
fatal: unsafe repository is owned by someone else 的解决方法
Yyds dry goods inventory software encryption lock function
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
随机推荐
Xilinx Vivado set *. svh as SystemVerilog Header
freemarker的使用
实现一个多进程并发的服务器
Fabric. JS zoom canvas
篇9:XShell免费版安装
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
Delete element (with transition animation)
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
Quarkus learning IV - project development to deployment
Using computed in uni app solves the abnormal display of data () value in tab switching
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Basic knowledge of QT original code
Yyds dry goods inventory software encryption lock function
taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
Fatal: unsafe repository is owned by someone else
Actual combat sharing of shutter screen acquisition
Uniapp automated test learning
Data Lake (11): Iceberg table data organization and query
求轮廓最大内接圆
Some interview suggestions for Android programmers "suggestions collection"