当前位置:网站首页>Heap basic operation source code Bing
Heap basic operation source code Bing
2022-04-22 06:10:00 【Birds follow February】
Heap.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int type;
typedef type(*function)(type, type);
typedef struct Heap {
int size = 0;
int capcity;
type* array;
function fun;
}Heap;
// initialization
void heapCreate(Heap *heap, type *array, int size, function fun);
// Heap insert
void push(Heap*heap, type);
// Delete heap top
type pop(Heap* heap);
void swap(type*a, type*b);
// Downward adjustments
void adjustDown(Heap* heap, int root);
// Adjust up
void adjustUp(Heap* heap);
// Heap delete
void heapDestory(Heap* heap);
int isEmpty(Heap *heap);
int isFull(Heap *heap);
// Return to the top of the reactor
int top(Heap *heap);
void swap(type*a, type*b) {
int temp = *a;
*a = *b;
*b = temp;
}
void heapCreate(Heap *heap, type *array, int size, function fun) {
assert(heap != NULL);
heap->array = (type*)malloc(2*size * sizeof(type));
assert(heap->array != NULL);
memcpy(heap->array, array,size*sizeof(type));
heap->size = size;
heap->capcity = 2 * size;
heap->fun = fun;
for (int i = (size-2)/2; i >=0 ; i--){
adjustDown(heap, i);
}
}
void push(Heap*heap, type data) {
if (isFull(heap)){
heap->array = (type*)realloc(heap->array, 2 * heap->capcity * sizeof(type));
assert(heap->array != NULL);
heap->capcity *= 2;
}
heap->array[heap->size++] = data;
adjustUp(heap);
}
type pop(Heap* heap) {
if (isEmpty(heap)) {
printf(" The heap is empty ");
return -1;
}
type res = heap->array[0];
heap->array[0] = heap->array[--heap->size];
adjustDown(heap, 0);
return res;
}
void adjustDown(Heap* heap, int root) {
int child = 2 * root + 1;
while (child < heap->size) {
if (child+1<heap->size&&heap->fun(heap->array[child], heap->array[child + 1])){
child +=1;
}
if (heap->fun(heap->array[root],heap->array[child])){
swap(&(heap->array[root]), &(heap->array[child]));
}
else{
break;
}
root = child;
child = 2 * root + 1;
}
}
void adjustUp(Heap* heap) {
int child = heap->size - 1;
if (child<=0){
return;
}
int parent = (child - 1) / 2;
while (child > 0) {
if (heap->fun(heap->array[parent], heap->array[child])) {
swap(&heap->array[parent], &heap->array[child]);
}
else {
break;
}
child = parent;
parent = (child - 1) / 2;
}
}
void heapDestory(Heap* heap) {
free(heap->array);
free(heap);
}
int isEmpty(Heap *heap) {
return heap->size == 0;
}
int isFull(Heap *heap) {
return heap->size == heap->capcity;
}
int top(Heap *heap) {
return heap->array[0];
}
The test file
#include <stdio.h>
#include<Windows.h>
#include "Heap.h"
int big(int a, int b) {
return a>b;
}
int main()
{
Heap heap;
type a[5] = {
5,4,3,2,1 };
heapCreate(&heap, a, 5, big);
pop(&heap);
printf("%d",heap.array[0]);
system("pause");
}
版权声明
本文为[Birds follow February]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220542258900.html
边栏推荐
- Part 90 leetcode refers to the longest substring of offer dynamic programming (VII) that does not contain duplicate characters
- 蓝桥杯嵌入式扩展板学习之DS18B20
- 指针所指的地址值及指针所指对象的值(学习笔记)
- Use of tab control control in MFC
- IFIX question summary Q & A (personal record)
- STL学习笔记0x0001(容器分类)
- Daily learning record -- solving graphviz Chinese garbled code problem
- 通用定时器
- QT drawPixmap和drawImage模糊问题
- 汇编 makefile
猜你喜欢

WGS84 coordinate conversion, map picking, WGS84 coordinate tool recommended

通用定时器

Jeecgboot Online form Development - control Configuration

On the relationship between the Euler angle of RT matrix and the type of pose in Halcon in hand eye calibration

Error in QT: undefined reference to ` widget:: settime()‘

常见面试问题 - 2(计算机网络)

deep learning object detection 精选

Chapter 88 leetcode sword refers to offer dynamic programming (V) maximum value of gifts

Part 75 leetcode exercise (8) 8 String to integer

After compiling the official program of punctual atom stm32f429, it cannot be downloaded with j-link
随机推荐
Record one installation of centos8 + postgresql9 6 + PostGIS's painful experience
QWebsocket通信
AIX6. 1 compile OpenSSL
Leetcode: Sword finger offer 29 Print the matrix clockwise
蓝桥杯嵌入式学习之双路AD采集
jeecgboot-online在线开发3
汇编 makefile
Oracle uses C language to write custom functions
写一个函数的声明,使其返回数组的引用并且该数组包含10个string对象(笔记)
Developing Postgres custom function with C language
Geojson file ShapeFile file batch conversion gadget
opencv图像增强
Swelling corrosion and other morphological treatment
Jeecgboot online development 3
关于常量指针、指针常量的使用--练习题(记录)
deep learning object detection 精选
2020-10-28
Part 75 leetcode exercise (8) 8 String to integer
I/O多路复用(select/poll/epoll)
蓝桥杯嵌入式扩展板学习之数码管