当前位置:网站首页>C语言二叉树与建堆
C语言二叉树与建堆
2022-07-07 04:33:00 【pythoncjavac++】
目录
二叉树有关概念
二叉树:二叉树是每个节点最多有两个子树的树结构。
节点度:一个节点含有的子树的个数。
兄弟节点:具有相同父节点的节点互称为兄弟节点。
叶子节点或终端节点:没有任何子节点的节点称为叶子节点。
父节点、子节点:如果一个节点下面连接多个节点,那么该节点称为父节点,它下面的节点称为子 节点。
树的度:所有结点的度数的最大值。二叉树的度小于等于2。
树的深度:从根节点开始(其深度为0)自顶向下逐层累加的。
树的高度:从叶子节点开始(其高度为0)自底向上逐层累加的。
根节点:一棵树最上面的节点称为根节点。
节点的祖先:从根到该节点所经分支上的所有节点。
森林:由m棵互不相交的树组成的集合。
二叉树有关计算公式
1.n个节点的二叉树一共有((2n)!)/(n! * (n+1)!)种
2.n层二叉树的第n层最多为2^(n-1)个
3.二叉树节点计算公式 N = n0+n1+n2,度为0的叶子节点比度为2的节点数多一个。N=1*n1+2*n2+1
4.具有n个节点的完全二叉树的深度为log2(n) + 1
5. 树的高度:从根节点到所有叶节点中最大的边的数目。树的深度:从根节点到所有叶节点中最多的节点数目。
下面是计算父子间关系(重点)
leftchild=parent*2+1
rightchild=parent*2+2
parent=(child-1)/2
接下来是如何创建二叉树
二叉树的创建
typedef int HPDataType;
typedef struct Heap
{
HPDataType* a;
int size;
int capacity;
}HP;
二叉树的初始化
void HeapInit(HP* php)
{
php->a = NULL;
php->size = php->capacity = 0;
}
二叉树的销毁
void HeapDestroy(HP* php)
{
assert(php);
free(php->a);
php->a = NULL;
php->size = php->capacity = 0;
}
二叉树的插入
void swap(HPDataType* a, HPDataType* b)
{
HPDataType as = *a;
*a = *b;
*b = as;
}
void AdjustUpd(HPDataType* a, int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (a[parent] < a[child])
{
swap(&a[parent], &a[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void HeapPush(HP* php, HPDataType x)
{
assert(php);
if (php->size == php->capacity)
{
int newca = php->capacity == 0 ? 4 : php->capacity * 2;
HPDataType* newhp = (HPDataType*)realloc(php->a, newca*sizeof(HPDataType));
if (newhp == NULL)
{
perror("realloc");
exit(-1);
}
php->a = newhp;
php->capacity = newca;
}
php->a[php->size] = x;
php->size++;
AdjustUpd(php->a, php->size-1);
}
二叉树的删除
void HeapPop(HP* php)
{
assert(php);
if (HeapEmpty(php))
{
return;
}
php->a[0] = php->a[php->size - 1];
php->size--;
AdjustDownd(php->a,php->size,0);
}
判断二叉树是否为空以及二叉树元素个数
bool HeapEmpty(HP* php)
{
return php->size == 0;
}
int HeapSize(HP* php)
{
assert(php);
return php->size ;
}
建堆
//小堆
void AdjustDown1(HPDataType* a, int size, int parent)
{
int child = parent * 2 + 1;
while (child < size)
{
if (child+1 < size && a[child + 1] < a[child])
{
child++;
}
if (a[child] < a[parent])
{
swap(&a[child], &a[parent]);
parent = child;
child = parent *2 +1;
}
else
break;
}
}
//大堆
void AdjustDownd(HPDataType* a, int size, int parent)
{
int child = parent * 2 + 1;
while (child < size)
{
if (child + 1 < size && a[child + 1] > a[child])
{
child++;
}
if (a[child] > a[parent])
{
swap(&a[child], &a[parent]);
parent = child;
child = parent *2 +1;
}
else
break;
}
}
这里分别是建大堆与小堆的代码
边栏推荐
- Resource create package method
- Technology cloud report: from robot to Cobot, human-computer integration is creating an era
- Numbers that appear only once
- 2022制冷与空调设备运行操作复训题库及答案
- What is the difference between TCP and UDP?
- [webrtc] m98 Screen and Window Collection
- JS get all date or time stamps between two time stamps
- Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
- 开源生态|打造活力开源社区,共建开源新生态!
- php导出百万数据
猜你喜欢
Linux server development, SQL statements, indexes, views, stored procedures, triggers
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
Use and analysis of dot function in numpy
resource 创建包方式
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
今日现货白银操作建议
【webrtc】m98 screen和window采集
【数学笔记】弧度
Codeforces Global Round 19
随机推荐
《动手学深度学习》(四) -- 卷积神经网络 CNN
Common method signatures and meanings of Iterable, collection and list
resource 创建包方式
Shell 脚本的替换功能实现
2022焊工(初级)判断题及在线模拟考试
【webrtc】m98 screen和window采集
Use and analysis of dot function in numpy
Most elements
2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct
numpy中dot函数使用与解析
Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
【性能压测】如何做好性能压测?
探索Cassandra的去中心化分布式架构
Leetcode 43 String multiplication (2022.02.12)
[UVM foundation] what is transaction
[UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
Cnopendata American Golden Globe Award winning data
Figure out the working principle of gpt3
Installing postgresql11 database under centos7
【数学笔记】弧度