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

数字营销1+x网站虚拟主机网站

数字营销1+x网站,虚拟主机网站,徐州建设工程交易网江苏本源,网络策划专业本篇博客给大家带来的是用C语言来实现数据结构树和二叉树的实现! 🐟🐟文章专栏:数据结构 🚀🚀若有问题评论区下讨论,我会及时回答 ❤❤欢迎大家点赞、收藏、分享! 今日思想&#xff…

本篇博客给大家带来的是用C++语言来实现数据结构树和二叉树的实现!

🐟🐟文章专栏:数据结构

🚀🚀若有问题评论区下讨论,我会及时回答

❤❤欢迎大家点赞、收藏、分享!

今日思想:你现在的懒惰将来你会买单的!

         续接上文【C++】树和二叉树的实现(上)-CSDN博客

 一、二叉树的实现方法

         在上文我们知道二叉树有两种方法(顺序结构和链式结构),顺序结构实现二叉树的底层是数组,那么在现实生活中我们把堆(一种二叉树)使用顺序结构来存储,最后二叉树的实现方法有:堆的初始化、堆的销毁、向上调整、向下调整、入堆、出堆、判断堆是否为空、取堆顶数据。接下来我们一一实现。

        1、堆的初始化

        既然堆的底层是数组,那么我们先确定堆的结构体。

代码实例:

//Heap.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//堆的结构
typedef int HPDataType;
typedef struct Heap
{HPDataType* arr;int size;//有效数据个数int capacity;//空间大小
}HP;

堆的初始化:

//Heap.h
//堆的初始化
void HPInit(HP* php);
//Heap.c
#include"Heap.h"
//堆的初始化
void HPInit(HP* php)
{assert(php);php->arr = NULL;php->size = php->capacity = 0;
}

         2、堆的销毁

        堆的销毁和初始化差不多这里不展开说。

代码实例:

//Heap.h
//堆的销毁
void HPDestory(HP* php);
//堆的销毁
void HPDestory(HP* php)
{assert(php);if (php->arr)free(php->arr);php->arr = NULL;php->size = php->capacity = 0;
}

        3、向上调整

        我们有个堆假设是小堆(如图),如果我们在size位置插入0,那么这样就不符合小堆的结 构,所以我们要向上调整。上偏文章我们可知二叉树的特点:如果 i 是孩子下标,父结点下标等于(i-1)/ 2,假设 i 为父结点下标那么右孩子下标等于2i+2,左孩子等于2i+1。

        思路:通过插入数据的下标求父结点的下标然后对比父结点和孩子结点的大小来判断是否符合小堆结构,不符合就交换。

代码实例:

//Heap.h
//向上调整
void AdjustUp(HPDataType* arr, int child);
//交换两个值
void Swap(HPDataType* x, HPDataType* y);
//Heap.c
//交换两个值
void Swap(HPDataType* x, HPDataType* y)
{int tmp = *x;*x = *y;*y = tmp;//向上调整
void AdjustUp(HPDataType* arr, int child)
{int parent = (child - 1) / 2;while (child > 0){//小堆: <//大堆: >if (arr[child] < arr[parent]){swap(&arr[child], &arr[parent]);child = parent;parent = (child - 1) / 2;}else{break;}}
}

        4、入堆

        入堆的思想和向上调整差不多,只要我们往数组的size位置插入数据,首先我们要判断空间是否满了,如果满了申请新的空间,没有就插入然后向上调整,如果是小堆就要返回小堆的结构。

代码实例:

//Heap.h
//入堆
void HPPush(HP* php, HPDataType x);
//Heap.c
//入堆
void HPPush(HP* php, HPDataType x)
{assert(php);//判断空间是否足够if (php->size == php->capacity){//增容int newCapacity = php->capacity == 0 ? 4 : 2 * php->capacity;HPDataType* tmp = (HPDataType*)realloc(php->arr, newCapacity * sizeof(HPDataType));if (tmp == NULL){perror("realloc fail!");exit(1);}php->arr = tmp;php->capacity = newCapacity;}//空间足够直接插入php->arr[php->size] = x;//向上调整AdjustUp(php->arr, php->size);++php->size;
}

5、向下调整

        向下调整和出堆联动,大家可以先看下面的出堆,根据出堆我们把堆顶和最后的数据交换,假设这是个大堆,而10在堆顶(如图),这不符合大堆的结构,我们需要向下调整。

思路:根据10的下标来找10的右孩子和左孩子,左孩子和右孩子比较谁大和父结点交换,交换完之后再让parent来到child的位置,child来到25的位置然后重复操作。

代码实例:

//Heap.h
//向下调整
void AdjustDown(HPDataType* arr, int parent, int n);
//Heap.c
//向下调整
void AdjustDown(HPDataType* arr, int parent, int n)
{int child = parent * 2 + 1;while (child < n){//大堆: <//小堆: >if (child + 1 < n && arr[child] < arr[child + 1]){child++;}//大堆:>//小堆:<if (arr[child] > arr[parent]){Swap(&arr[child], &arr[parent]);parent = child;child = parent * 2 + 1;}else{break;}}

6、出堆与判断堆是否为空

        出堆其实就是把堆顶的数据和最后一个数据交换,然后size--;到时候如果插入新数据就可以覆盖最后的数据,size--之后要向下调整保证符号堆(大堆或者小队)的结构。

代码实例:

//Heap.h
//出堆
void HPPop(HP* php);
//判断堆是否为空
bool HPEmpty(HP* php);
//Heap.c
//判断堆是否为空
bool HPEmpty(HP* php)
{assert(php);return php->size == 0;
}
//出堆
void HPPop(HP* php)
{assert(!HPEmpty(php));swap(&php->arr[0], &php->arr[php->size - 1]);php->size--;//向下调整AdjustDown(php->arr, 0, php->size);
}

 7、堆的打印

        有时候我们需要打印堆来看看是否符号自己的预期。

代码实例:

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

8、取堆顶数据

        取堆顶的数据就是数组下标为0的数据。

代码实例:

//Heap.h
//取堆顶数据
HPDataType HPTop(HP* php);
//Heap.c
//取堆顶数据
HPDataType HPTop(HP* php)
{assert(!HPEmpty(php));return php->arr[0];
}

9、完整的代码

//Heap.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//堆的结构
typedef int HPDataType;
typedef struct Heap
{HPDataType* arr;int size;//有效数据个数int capacity;//空间大小
}HP;//堆的初始化
void HPInit(HP* php);
//堆的销毁
void HPDestory(HP* php);
//堆的打印
void HPPrint(HP* php);
//交换两个值
void Swap(HPDataType* x, HPDataType* y);
//向上调整
void AdjustUp(HPDataType* arr, int child);
//向下调整
void AdjustDown(HPDataType* arr, int parent, int n);
//入堆
void HPPush(HP* php, HPDataType x);
//出堆
void HPPop(HP* php);
//取堆顶数据
HPDataType HPTop(HP* php);
//判断堆是否为空
bool HPEmpty(HP* php);
//Heap.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"
//堆的初始化
void HPInit(HP* php)
{assert(php);php->arr = NULL;php->size = php->capacity = 0;
}
//堆的销毁
void HPDestory(HP* php)
{assert(php);if (php->arr)free(php->arr);php->arr = NULL;php->size = php->capacity = 0;
}
//交换两个值
void Swap(HPDataType* x, HPDataType* y)
{int tmp = *x;*x = *y;*y = tmp;
}
//向上调整
void AdjustUp(HPDataType* arr, int child)
{int parent = (child - 1) / 2;while (child > 0){//小堆: <//大堆: >if (arr[child] > arr[parent]){swap(&arr[child], &arr[parent]);child = parent;parent = (child - 1) / 2;}else{break;}}
}
//入堆
void HPPush(HP* php, HPDataType x)
{assert(php);//判断空间是否足够if (php->size == php->capacity){//增容int newCapacity = php->capacity == 0 ? 4 : 2 * php->capacity;HPDataType* tmp = (HPDataType*)realloc(php->arr, newCapacity * sizeof(HPDataType));if (tmp == NULL){perror("realloc fail!");exit(1);}php->arr = tmp;php->capacity = newCapacity;}//空间足够直接插入php->arr[php->size] = x;//向上调整AdjustUp(php->arr, php->size);++php->size;
}
//判断堆是否为空
bool HPEmpty(HP* php)
{assert(php);return php->size == 0;
}
//向下调整
void AdjustDown(HPDataType* arr, int parent, int n)
{int child = parent * 2 + 1;while (child < n){//大堆: <//小堆: >if (child + 1 < n && arr[child] < arr[child + 1]){child++;}//大堆:>//小堆:<if (arr[child] > arr[parent]){Swap(&arr[child], &arr[parent]);parent = child;child = parent * 2 + 1;}else{break;}}
}
//出堆
void HPPop(HP* php)
{assert(!HPEmpty(php));swap(&php->arr[0], &php->arr[php->size - 1]);php->size--;//向下调整AdjustDown(php->arr, 0, php->size);
}
//堆的打印
void HPPrint(HP* php)
{for (int i = 0; i < php->size; i++){printf("%d ", php->arr[i]);}printf("\n");
}
//取堆顶数据
HPDataType HPTop(HP* php)
{assert(!HPEmpty(php));return php->arr[0];
}

完!!


文章转载自:

http://Rg2QriXo.hhskr.cn
http://Co5xO3Ns.hhskr.cn
http://XsCipMkp.hhskr.cn
http://8o4EDesX.hhskr.cn
http://6U1EYDYz.hhskr.cn
http://hSQ9fVn2.hhskr.cn
http://Ab2lPtx4.hhskr.cn
http://Xoyv1CUk.hhskr.cn
http://orLJfL9x.hhskr.cn
http://2TInZ0G8.hhskr.cn
http://iLIHTmZL.hhskr.cn
http://5i1gu3Zy.hhskr.cn
http://QX2Jr6LP.hhskr.cn
http://CCUHMjlp.hhskr.cn
http://31O4AGJ8.hhskr.cn
http://fyLPYANr.hhskr.cn
http://CDerWvN6.hhskr.cn
http://DhlvBog2.hhskr.cn
http://56v0hilu.hhskr.cn
http://coU1c5pY.hhskr.cn
http://wapAC4Mf.hhskr.cn
http://xMUDar0A.hhskr.cn
http://BPQ07t4i.hhskr.cn
http://9CCaGKqF.hhskr.cn
http://t9uBMvDD.hhskr.cn
http://PP5curE1.hhskr.cn
http://yw2wJG1O.hhskr.cn
http://ZYbWVhZB.hhskr.cn
http://yax8WdMI.hhskr.cn
http://173lxkzQ.hhskr.cn
http://www.dtcms.com/wzjs/642318.html

相关文章:

  • 公司怎么注册自己的网站杭州企业网站制作
  • 外贸网站有哪些wordpress 远程访问
  • 网站建设开发语建设部网站1667号下载
  • asp做网站的优势是什么鞍山网站建设营销
  • 网站失败的原因网站建设采购项目
  • 岳池发展建设集团有限公司门户网站网站内部数据搜索怎么做
  • 万网网站建设教程北京美陈设计制作公司
  • 上海建设手机网站本地视频怎么生成链接
  • 博客网站开发框架wordpress微信公众号管理
  • 南京谁做免费网站企业网络推广方案怎么做
  • 淘客没有网站难做国外学做咖啡的网站
  • html网站的规划与建设6甘肃网站建设哪家便宜
  • 百度网站权重排行一台服务器一个固定ip怎样做两个网站
  • 手机网站 搜索优化 百度嘉兴网站开发公司
  • wordpress仿站教程2016广州电子商务网站建设 v
  • 电子商务网站建设过程上百度推广 免费做网站
  • 网站编程用什么语言网站收录量低怎么做
  • apache 多个网站wordpress 媒体分类
  • 论前端对网站建设的重要性如何做网站推
  • 网站建设开发费入什么科目广州网站开发网络公司
  • 学校校园网站餐饮手机网站建设
  • 安徽省建设监理有限公司网站室内设计效果图怎么做
  • 做像美团淘宝平台网站多少钱微信分享链接转换wordpress
  • 环保网站建设说明书ps转页面wordpress插件
  • 茶叶网络推广方案网站建设seoppt
  • 海山免费网站建设自己制作游戏的app
  • 文章类网站选什么内容如今做哪些网站能致富
  • 网站不备案不能访问洛阳建网站
  • 做推广适合哪些网站吗沈阳建设工程信息网官网 安全中项网
  • 商城网站数据库陕西建设厅官网首页