当前位置:网站首页>I realize large top stack with C I
I realize large top stack with C I
2022-07-24 13:00:00 【MT_ one hundred and twenty-five】
One 、 The realization of the heap
1. Heap structure :
The logical structure is similar to A tree “ Trees ”


2. Type of heap :
Large top pile structure : A special kind of tree : Each child node is smaller than the parent node
Small top pile structure : Empathy : Each child node is larger than the parent node
Structure diagram :

2. Big pile top Code implementation :( Here we implement heap Use a sequential table )
① initialization :
typedef int Heaptype;
typedef struct Heap
{
Heaptype* head;
int size; // Record the total heap capacity
int capacity; // Record the total number of current data
}Heap;
// Initialization of the heap
void Heap_Init(Heap* pphead)
{
assert(pphead);
pphead->head = NULL;
pphead->size = 0;
pphead->capacity = 0;
}② insert data :
Implementation details : Data is inserted at the same time , Adjust the data structure , Keep the maximum number of tree tops at all times
If the newly inserted node is larger than the parent node , To exchange , Therefore, this link of structural adjustment is independent
namely :“ Adjust up ”.
Adjust up : Because when inserting data : New data is stored at the end of the sequence table by default , Therefore, it is logically at the bottom of the heap ,
therefore , When the new data is larger than the parent node , It will swap with the parent node that is logically above it , be called
Adjust up .
notes : Upward adjustment sometimes more than once , It may also be adjusted many times , Until each node is in its corresponding position .
Flow chart :

Minutiae : Because the data is stored in a sequential table , Therefore, the subscript of the child node is consistent with that of the parent node
The formula :parent = (child - 1)/2 ;( It will be understood when the calculation is carried out according to this formula )
// Adjust up
void adjust_up(Heap* pphead)
{
int child = pphead->capacity;
int parent = (child - 1) / 2;
for (; pphead->head[parent] < pphead->head[child];)// Judge whether it conforms to the big top pile
{
swap(&(pphead->head[parent]),&(pphead->head[child]));// swapping
child = parent;
parent = (child - 1) / 2;
}
}
// insert data
void Heap_push(Heap* pphead, Heaptype data)
{
assert(pphead);
// Judge whether the capacity is full and expand
Heap_realloc(pphead);
pphead->head[pphead->capacity] = data;
// Adjust up
adjust_up(pphead);
pphead->capacity++;
}③ Delete data : To avoid disrupting the structure of the whole heap by directly deleting the header data ,
Here, first exchange the header data and tail data , Then delete the tail data , Then use “ Downward adjustments ”, namely : Compare and adjust the replaced top data downward with its child nodes , Until it conforms to the large top pile structure .
notes :1. When the big top stack is adjusted downward , The parent node should exchange with the larger of the two child nodes , So compare .
2. The calculation formula of subscript here is : Left the child :child = (parent * 2) + 1
The right child :child = (parent * 2) + 2
3. Avoid crossing boundaries when calculating child subscripts , Avoid comparing the parent node with data that does not belong to the heap .
// Delete data
void Heap_pop(Heap* pphead)
{
assert(pphead);
assert(pphead->capacity > 0); // Prevent the heap from being empty
// Exchange top data with tail data
swap(&(pphead->head[0]),&( pphead->head[pphead->capacity-1]));
pphead->capacity--;
// Downward adjustments
adjust_down(pphead,0);
}
// Downward adjustments
void Adjust_down(int* a, int size, int parent)
{
assert(a);
for (int child = parent * 2 + 1; child <= size; child = parent * 2 + 1)
{
// By default, the left child paper is used to compare with the parent node , If the right child paper is larger than the left child paper and does not cross the boundary, exchange
if (a[child] > a[child + 1] && child + 1 <= size)
child = child + 1;
// Compare child paper and parent node
if (a[child] < a[parent])
{
int temp = a[child];
a[child] = a[parent];
a[parent] = temp;
}
// Continue to compare downward
parent = child;
}
}④ Capacity expansion || Sentenced to empty || Top data || Destroy the pile
// Judge whether to expand capacity
void Heap_realloc(Heap* pphead)
{
assert(pphead);
if (pphead->size == pphead->capacity)
{
Heaptype Newsize = (pphead->size == 0) ? 4 : (pphead->size * 2);
Heaptype* Newhead = (Heaptype*)realloc(pphead->head,sizeof(Heaptype) * Newsize);
if (Newhead == NULL)
{
perror("realloc");
exit(-1);
}
pphead->head = Newhead;
pphead->size = Newsize;
}
}
// Determine whether the heap is empty
bool Heap_Empty(Heap* pphead)
{
if (pphead->capacity)
return false;
return true;
}
// Take the top data
Heaptype Heap_top(Heap* pphead)
{
return pphead->head[0];
}
// Destroy the pile
void Heap_Destory(Heap* pphead)
{
assert(pphead);
free(pphead->head);
pphead->head = NULL;
pphead->size = 0;
pphead->capacity = 0;
}Thank you for your visit !!!
边栏推荐
- 25. Middle order traversal of binary tree
- Usage of swipemenurecyclerview
- 猿人学第六题
- Behind the rapid growth, Huawei cloud Wulanchabu data center is the green way
- Windivert:可抓包,修改包
- Research on data governance quality assurance
- About packaging objects
- 1.9. 触摸按钮(touch pad)测试
- 自己实现is_default_constructible
- C language course design -- hotel management system
猜你喜欢
How to render millions of 2D objects smoothly with webgpu?

26. Reverse linked list II

Custom scroll bar

基于matlab的声音识别

27. Longest increasing subsequence

Cluster construction based on kubernetes v1.24.0 (III)

Nearly 65billion pieces of personal information were illegally handled in seven years, and the investigation of didi network security review case was announced

20201127 use markdown to draw UML diagrams, graphviz installation experience hematemesis finishing

开山之作造假!Science大曝Nature重磅论文学术不端,恐误导全球16年

【C语言】详细的文件操作相关知识
随机推荐
Localstorage
cookie
I used annotations to configure the serialization of redis in one module project, and then introduced this module in another module. Why is this configuration
How to render millions of 2D objects smoothly with webgpu?
权限系统就该这么设计,yyds
setAttribute、getAttribute、removeAttribute
Prototype inheritance
Proxy
基于Kubernetes v1.24.0的集群搭建(一)
Speech processing based on MATLAB
Introduction to the use of thread (2) thread
34. Add two numbers
Teach you how to use power Bi to realize four kinds of visual charts
27. Longest increasing subsequence
23. Spiral matrix
国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确
Relevant laws of animation movement (judge where to move according to parameters)
如何使用autofs挂载NFS共享
Redis(13)----浅谈Redis的主从复制
32. Maximum path sum in binary tree