当前位置: 首页 > news >正文

无锡市建设工程质监站网站怎么做自媒体

无锡市建设工程质监站网站,怎么做自媒体,wordpress template name,网站建设设计ppt二叉树——堆存储1.堆的初始化2. 堆的销毁3.堆的插入4.堆的删除5.堆的打印6.取堆顶的数据7.堆的数据个数8.堆的判空9.堆的构建10.向上调整11.向下调整12.使用堆进行排序13.交换14.完整代码🌟🌟hello,各位读者大大们你们好呀🌟&…

二叉树——堆存储

  • 1.堆的初始化
  • 2. 堆的销毁
  • 3.堆的插入
  • 4.堆的删除
  • 5.堆的打印
  • 6.取堆顶的数据
  • 7.堆的数据个数
  • 8.堆的判空
  • 9.堆的构建
  • 10.向上调整
  • 11.向下调整
  • 12.使用堆进行排序
  • 13.交换
  • 14.完整代码

🌟🌟hello,各位读者大大们你们好呀🌟🌟
🚀🚀系列专栏:【数据结构的学习】
📝📝本篇内容:使用数组实现二叉树堆的存储
⬆⬆⬆⬆上一篇:二叉树(一)
💖💖作者简介:轩情吖,请多多指教(> •̀֊•́ ) ̖́-

使用数组来存储,一般使用数组只适合完全二叉树,因为不是完全二叉树会有空间的浪费。而现实中只有堆才会使用数组来存储。二叉树顺序存储在物理上是一个数组,在逻辑上是一棵二叉树。
大堆:树中所有的父亲都大于等于孩子
小堆:树中的所有父亲都小于等于孩子
将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆
任何一个数组,可以看做完全二叉树,但不一定是堆

1.堆的初始化

void HeapInit(Heap* hp)// 堆的初始化
{assert(hp);hp->arr = NULL;hp->capacity = 0;hp->size = 0;
}

2. 堆的销毁

void HeapDestory(Heap* hp)// 堆的销毁
{assert(hp);free(hp->arr);//先得把开辟的动态空间释放掉,不然会造成内存泄露hp->capacity = hp->size = 0;//重新设置为0
}

3.堆的插入

void HeapPush(Heap* hp, Type x)//堆的插入
{assert(hp);if (hp->size == hp->capacity)//如果大小相同则需要扩容{int new = hp->arr == NULL ? 4 : hp->capacity * 2;//如果是NULL就开辟4个空间,否则就翻倍Type* tmp =(Type*)realloc(hp->arr, sizeof(Type)*new);//当第一个参数是NULL时,realloc的作用相等于mallocif (tmp == NULL)//判断realloc是否开辟成功{perror("realloc fail");exit(-1);}hp->capacity = new;hp->arr = tmp;}hp->arr[hp->size] = x;hp->size++;AdjustUp(hp->arr, hp->size - 1);//向上调整}

4.堆的删除

void HeapPop(Heap* hp)// 堆的删除
{hp->arr[0] = hp->arr[hp->size - 1];//把最后一个结点(指的是数组里最后一个元素)赋给根节点,就能删除大根堆hp->size--;AdjustDown(hp->arr,hp->size,0);//向下调整(但必须要做一些调整,不然堆就会出现问题)
}

5.堆的打印

void HeapPrint(Heap* hp)//堆的打印
{   for (int i = 0; i < hp->size; i++){printf("%d ",hp->arr[i]);}printf("\n");
}

6.取堆顶的数据

Type HeapTop(Heap* hp)//取堆顶的数据
{assert(hp);assert(hp->size>0);return hp->arr[0];
}

7.堆的数据个数

int HeapSize(Heap* hp)// 堆的数据个数
{assert(hp);return hp->size;}

8.堆的判空

bool HeapEmpty(Heap* hp)//堆的判空
{assert(hp);return hp->size ==0;
}

9.堆的构建

void HeapCreate(Heap* hp, Type* a, int n)//堆的构建
{HeapInit(hp);hp->arr = malloc(sizeof(Type) * n);if (hp->arr == NULL){perror("malloc fail");exit(-1);}memcpy(hp->arr,a,sizeof(Type)*n);hp->size += n;hp->capacity += n;//向上调整/*for (int i = 0; i < n; i++){AdjustUp(hp->arr, i);}*///向下调整int i = (n - 1 - 1) / 2;for (; i >= 0; i--){AdjustDown(hp->arr,n,i);}
}

10.向上调整

void AdjustUp(Type* arr,int child)//向上调整
{while (child>0)//当child为0的时候。说明已经没有父节点了,到了根节点{int parent = (child - 1) / 2;//算出父节点if (arr[parent] > arr[child])//判断是否父节点小于孩子节点{swap(&arr[parent],&arr[child]);//交换child = parent;//把父节点的下标赋给孩子节点,继续判断}else{break;}}
}

11.向下调整

AdjustDown(Type* arr, int size, int parent)//向下调整
{
int child = parent * 2 + 1;//先求出孩子结点的下标,并假设左子树的值比右子树的大while (child<size){//要小心右子树可能没有,所以需要判断if (child+1<size&&arr[child] > arr[child + 1])//如果不是,调换,右子树正好是左子树的下标+1{child++;}if (arr[parent]>arr[child]){swap(&arr[parent], &arr[child]);parent = child;child = parent * 2 + 1;}else{break;}}}

12.使用堆进行排序

void HeapSort(Type* arr, int n)//使用堆进行排序
{//升序//大堆来实现//向下调整int end = n - 1;while (end > 0){swap(&arr[0], &arr[end]);//先交换AdjustDown(arr, end, 0);end--;}
}

13.交换

void swap(Type* s1,Type* s2)//交换
{Type tmp = *s1;//先保存一下s1*s1 = *s2;*s2 = tmp;
}

14.完整代码

//头文件
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>
typedef int Type;
typedef struct Heap//在现实使用中只有堆才会使用数组来存储
{Type* arr;int size;int capacity;
}Heap;
void HeapInit(Heap* hp);// 堆的初始化
void HeapDestory(Heap* hp);// 堆的销毁
void HeapPush(Heap* hp,Type x);//堆的插入
void HeapPop(Heap* hp);// 堆的删除
void HeapPrint(Heap* hp);//堆的打印
Type HeapTop(Heap* hp);//取堆顶的数据
int HeapSize(Heap* hp);// 堆的数据个数
bool HeapEmpty(Heap* hp);// 堆的判空
void HeapCreate(Heap* hp,Type* a, int n);//堆的构建
void AdjustUp(Type* arr, int child);//向上调整
AdjustDown(Type* arr, int size, int parent);//向下调整
void HeapSort(Type* arr, int n);//使用堆进行排序
void swap(Type* s1, Type* s2);//交换
//函数实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "Heap.h"
void HeapInit(Heap* hp)// 堆的初始化
{assert(hp);hp->arr = NULL;hp->capacity = 0;hp->size = 0;
}
void HeapDestory(Heap* hp)// 堆的销毁
{assert(hp);free(hp->arr);//先得把开辟的动态空间释放掉,不然会造成内存泄露hp->capacity = hp->size = 0;//重新设置为0
}void swap(Type* s1,Type* s2)//交换
{Type tmp = *s1;//先保存一下s1*s1 = *s2;*s2 = tmp;
}void AdjustUp(Type* arr,int child)//向上调整
{while (child>0)//当child为0的时候。说明已经没有父节点了,到了根节点{int parent = (child - 1) / 2;//算出父节点if (arr[parent] > arr[child])//判断是否父节点小于孩子节点{swap(&arr[parent],&arr[child]);//交换child = parent;//把父节点的下标赋给孩子节点,继续判断}else{break;}}
}
void HeapPush(Heap* hp, Type x)//堆的插入
{assert(hp);if (hp->size == hp->capacity)//如果大小相同则需要扩容{int new = hp->arr == NULL ? 4 : hp->capacity * 2;//如果是NULL就开辟4个空间,否则就翻倍Type* tmp =(Type*)realloc(hp->arr, sizeof(Type)*new);//当第一个参数是NULL时,realloc的作用相等于mallocif (tmp == NULL)//判断realloc是否开辟成功{perror("realloc fail");exit(-1);}hp->capacity = new;hp->arr = tmp;}hp->arr[hp->size] = x;hp->size++;AdjustUp(hp->arr, hp->size - 1);//向上调整}AdjustDown(Type* arr, int size, int parent)//向下调整
{
int child = parent * 2 + 1;//先求出孩子结点的下标,并假设左子树的值比右子树的大while (child<size){//要小心右子树可能没有,所以需要判断if (child+1<size&&arr[child] > arr[child + 1])//如果不是,调换,右子树正好是左子树的下标+1{child++;}if (arr[parent]>arr[child]){swap(&arr[parent], &arr[child]);parent = child;child = parent * 2 + 1;}else{break;}}}void HeapPop(Heap* hp)// 堆的删除
{hp->arr[0] = hp->arr[hp->size - 1];//把最后一个结点(指的是数组里最后一个元素)赋给根节点,就能删除大根堆hp->size--;AdjustDown(hp->arr,hp->size,0);//向下调整(但必须要做一些调整,不然堆就会出现问题)
}void HeapPrint(Heap* hp)//堆的打印
{   for (int i = 0; i < hp->size; i++){printf("%d ",hp->arr[i]);}printf("\n");
}void HeapCreate(Heap* hp, Type* a, int n)//堆的构建
{HeapInit(hp);hp->arr = malloc(sizeof(Type) * n);if (hp->arr == NULL){perror("malloc fail");exit(-1);}memcpy(hp->arr,a,sizeof(Type)*n);hp->size += n;hp->capacity += n;//向上调整/*for (int i = 0; i < n; i++){AdjustUp(hp->arr, i);}*///向下调整int i = (n - 1 - 1) / 2;for (; i >= 0; i--){AdjustDown(hp->arr,n,i);}
}Type HeapTop(Heap* hp)//取堆顶的数据
{assert(hp);assert(hp->size>0);return hp->arr[0];
}int HeapSize(Heap* hp)// 堆的数据个数
{assert(hp);return hp->size;}bool HeapEmpty(Heap* hp)//堆的判空
{assert(hp);return hp->size ==0;
}void HeapSort(Type* arr, int n)//使用堆进行排序
{//升序//大堆来实现//向下调整int end = n - 1;while (end > 0){swap(&arr[0], &arr[end]);//先交换AdjustDown(arr, end, 0);end--;}
}

🌸🌸二叉树(二)的知识大概就讲到这里啦,博主后续会继续更新更多数据结构的相关知识,干货满满,如果觉得博主写的还不错的话,希望各位小伙伴不要吝啬手中的三连哦!你们的支持是博主坚持创作的动力!💪💪

http://www.ds6.com.cn/news/15192.html

相关文章:

  • 上海做网站找谁互联网营销策划是做什么的
  • 南海建设工程交易中心网站石家庄百度seo
  • 怎么做网站的推广商丘seo外包
  • 帮别人做网站开什么内容的专票营销策划经典案例
  • 网站如何快速被百度收录网站推广计划书范文
  • 广州电子商城网站建设seo收录查询工具
  • 做智能网站系统口碑营销是什么
  • 电商网站后台管理系统百度seo关键词优化方案
  • 网站图片展示源码抚州seo排名
  • wordpress pdf阅读独立站seo外链平台
  • 兼职20网站开发公司网站策划宣传
  • 国外市场网站推广公司网络推广项目代理
  • 设计网站都有哪些360上网安全导航
  • 免费素材网站 可商用2345浏览器网站进入
  • 做网站的备案资料百度统计官网
  • 多个域名 一个网站重庆网站seo教程
  • 深圳建网站培训学校建设网站公司
  • 重庆潼南网站建设公司seo公司 引擎
  • 上海网站建设天锐科技济南网站优化公司
  • 宠物网站开发与实现专业网络推广公司排名
  • 广西智能网站建设哪家有不用流量的地图导航软件
  • 做视频小网站犯法吗百度指数的基本功能
  • 做网站珊瑚橙颜色怎么搭配好看短视频入口seo
  • wordpress后台作用点击精灵seo
  • 做图书馆网站模板网店运营工作内容
  • 做会计应关注什么网站网络营销师证
  • 乐清柳市阿里巴巴做网站的百度收录查询接口
  • 蔬菜派送网站怎么做百度竞价排名什么意思
  • 中山网站的建设seo深圳优化
  • 易企互联网站建设郑志平爱站网创始人