满二叉树的节点个数 65,100,70,32,50,60
第一层1个节点,第二层2^1个节点,以此类推,总共有:
2^0+2^1+……2^h-2+2^h-1 个节点
完全二叉树的结点范围
完全二叉树也可能最后一层是满的,也可能最后一层只有一个:
所以完全二叉树的结点范围为:(2^(h-1),2^h-1);
小堆 大堆
二叉树的存储方式
逻辑存储方式就是我们画的树状图,物理存储就是数组
非完全二叉树不适合用数组存储,会造成空间浪费
既然是数组,那就有下标
通过父节点找子节点:
有了下标我们就可以通过找节点了,一个公式:
leftchild=parent*2+1;
right=parent*2+2;
同样的,可以逆推出来:
parent=(chid/2)-1;
代码:
#include"Heap.h"
int main()
{
HP tr;
HPInit(&tr);
int a[] = { 65,100,70,32,50,60 };
for (int i = 0; i<sizeof(a) / sizeof(int); ++i)
{
HPpush(&tr, a[i]);
}
return 0;
}
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int HPDatatype;
typedef struct Heap
{
HPDatatype* a;
int size;
int HPcapacity;
}HP;
void HPInit(HP* hp);
void HPDestry(HP* hp);
void HPpush(HP* hp, HPDatatype x);
#define _CRT_SECURE_NO_WARNINGS
#include"Heap.h"
void HPInit(HP*hp)
{
assert(hp);
hp->a = NULL;
hp->HPcapacity = 0;
hp->size = 0;
}
void HPDestry(HP* hp)
{
assert(hp);
free(hp->a);
hp->a = NULL;
hp->HPcapacity = 0;
hp->size = 0;
}
void ADjustup(HPDatatype* a,int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (a[child] < a[parent])
{
int tmp = a[child];
a[child]=a[parent] ;
a[parent] = tmp;
child = parent;
parent = (child-1)/2;
}
else
{
break;
}
}
}
void HPpush(HP* hp, HPDatatype x)
{
assert(hp);
if (hp->HPcapacity == hp->size)//如果数组里面什么都没有,没有栈顶元素也没有容量,也就是top和capacity都=0,,那么就扩容
{
int newcapcity = hp->HPcapacity == 0 ? 4 : hp->HPcapacity * 2;
HPDatatype* tmp = (HPDatatype*)realloc(hp->a, newcapcity*sizeof(HPDatatype));
if (tmp == NULL)
{
perror("malloc falil");
return;
}
hp->a = tmp;
hp->HPcapacity = newcapcity;
}
hp->a[hp->size] = x;//把要入栈的值给栈顶元素
hp->size++;//栈顶元素++,方便下个值输入
ADjustup(hp->a,hp->size-1);
}
当我们在main函数想调试看一下的时候
自动跳到了push函数那里,然后报了个警告
这样的
提示一般都是数组越界,或者malloc了没有释放或者是什么。
最后查了一圈发现是在main函数没有调用初始化函数,导致了 hp->a是NULL值。
加了初始化函数之后就好了,然后我们来看一下调试:
(把断点设到最后,直接跳过去)
我们的数组刚开始是{65,100,70,32,50,60}
经过我们的运算之后就变成了{32,50,60,100,65,70},成了一个小堆
我们变一下:
我们
发现变成大堆了:
OK,大堆小堆实现了,我们来实现一下删除,假如我们从头节点开始删,然后先将头节点与我们的最后一个节点交换,然后进行向下调整
为什么
会报错?
改了之后就不报错了:
可以看见没走完HPpop时,我们的二叉树有6个数,走完之后s只剩五个了,哪一个就是被删掉了。
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int HPDatatype;
typedef struct Heap
{
HPDatatype* a;
int size;
int HPcapacity;
}HP;
void HPInit(HP* hp);
void HPDestry(HP* hp);
void HPpush(HP* hp, HPDatatype x);
void HPpop(HP* hp);
bool Emptry(HP* hp);
void swap(int* a, int* b);
void HPDestry(HP* hp);
void HPpop(HP* hp);
int HPtop(HP* hp);
void AdjustDown(int* a, int n, int parent);
void ADjustup(HPDatatype* a, int child);
#define _CRT_SECURE_NO_WARNINGS
#include"Heap.h"
void HPInit(HP* hp)
{
assert(hp);
hp->a = NULL;
hp->HPcapacity = 0;
hp->size = 0;
}
void HPDestry(HP* hp)
{
assert(hp);
free(hp->a);
hp->a = NULL;
hp->HPcapacity = 0;
hp->size = 0;
}
void swap(int* p1, int* p2)
{
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void ADjustup(HPDatatype* a, int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (a[child] >a[parent])
{
int tmp = a[child];
a[child] = a[parent];
a[parent] = tmp;
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void HPpush(HP* hp, HPDatatype x)
{
assert(hp);
if (hp->HPcapacity == hp->size)//如果数组里面什么都没有,没有栈顶元素也没有容量,也就是top和capacity都=0,,那么就扩容
{
int newcapcity = hp->HPcapacity == 0 ? 4 : hp->HPcapacity * 2;
HPDatatype* tmp = (HPDatatype*)realloc(hp->a, newcapcity * sizeof(HPDatatype));
if (tmp == NULL)
{
perror("malloc falil");
return;
}
hp->a = tmp;
hp->HPcapacity = newcapcity;
}
hp->a[hp->size] = x;//把要入栈的值给栈顶元素
hp->size++;//栈顶元素++,方便下个值输入
ADjustup(hp->a, hp->size - 1);
}
int HPtop(HP* hp)
{
assert(hp);
assert(!Emptry(hp));
return hp->a[0];
}
bool Emptry(HP* hp)
{
assert(hp);
return hp->size == 0;
}
void AdjustDown(int* a, int n, int parent)
{
int child = parent * 2 + 1;
while (child < n)
{
// 选出左右孩子中小/大的那个
if (child + 1 < n
&& a[child + 1] > a[child])
{
++child;
}
if (a[child] > a[parent])
{
swap(&a[parent], &a[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void HPpop(HP* hp)
{
assert(hp);
swap(&hp->a[0], &hp->a[hp->size-1]);
hp->size--;
AdjustDown(hp->a, hp->size, 0);
}
int main()
{
HP tr;
HPInit(&tr);
int a[] = { 65,100,70,32,50,60 };
for (int i = 0; i < sizeof(a) / sizeof(int); ++i)
{
HPpush(&tr, a[i]);
}
HPpop(&tr);
return 0;
}
只用ADjustup 和 ADjustdown来建堆
把一个数快速排序成一个堆:
数组:
排完之后的样子:
是一个小堆:
思路:写一个函数HPqsort,HPqsort用来实现这个效果。
首先把数组传给HPqsort,然后HPqsort接收数组的值传给ADjustup函数,利用ADjustup函数实现向上调整的效果。
void HPqsort(int* a, int n)
{//向上调整
for (int i = 1; i < n; i++)
{
ADjustup(a, i);
}
int main()
{
int a[] = { 7,8,3,5,1,9,5,4 };
HPqsort(a, sizeof(a) / sizeof(int));
return 0;
}
删除:
代码:
void HPqsort(int* a, int n)
{//向上调整
for (int i = 1; i < n; i++)
{
ADjustup(a, i);
}
//向下调整
int end = n - 1;
while (end)
{
swap(&a[0],&a[end]);
AdjustDown(a, end, 0);
--end;
}
}
int main()
{
int a[] = { 7,8,3,5,1,9,5,4 };
HPqsort(a, sizeof(a) / sizeof(int));
return 0;
}