一,堆的理解
如果有一个关键码的集合K = {k0,k1, k2,…,kn-1},把它的所有元素按完全二叉树的顺序存储方式存储在一个一维数组中,并满足:Ki <= K2i+1 且 Ki<= K2i+2 (Ki >= K2i+1 且 Ki >= K2i+2) i = 0,1,2…,则称为小堆(或大堆)。将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。
堆的性质:
1.堆中某个节点的值总是不大于或不小于其父节点的值;
2.堆总是一棵完全二叉树。
二,堆的创建
下面我们给出一个数组,这个数组逻辑上可以看做一颗完全二叉树,但是还不是一个堆,现在我们通过算法,把它构建成一个堆。根节点左右子树不是堆,我们怎么调整呢?这里我们从倒数的第一个非叶子节点的子树开始调整,一直调整到根节点的树,就可以调整成堆。
三,堆的插入
在有序的堆中,插入一个数据,那么将这个先插入到堆的最后一部分,然后根据向上调整算法,然后使它到达最合适的位置,由堆的性质可以给出。
四,堆的删除
删除堆是删除堆顶的数据,将堆顶的数据根最后一个数据一换,然后删除数组最后一个数据,再进行向下调整算法。【这里的意思,我们删除是由堆顶开始,但是由于我们删除最后一个比较快速,不用做多与的步骤,那么可以使用交换,然后删除,就可以达到这样子了】
五,堆的实现代码
《heap.h》
#include<iostream>
#include<stdio.h>heap.h
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
#include<string.h>
typedef int HPDataType;
typedef struct Heap
{
HPDataType* _array;
int _size;
int _capacity;
};
void HeapInit(Heap* hp, HPDataType* a, int n);
void HeapDestory(Heap* hp);
void HeapPrint(Heap* hp);
void HeapPush(Heap* hp, HPDataType x);
void HeapPop(Heap* hp);
int HeapSize(Heap* hp);
int HeapEmpty(Heap* hp);
// 堆排序
void HeapSort(int* a, int n);
《heap.c》
#include"Heap.h"
void AdjustDown(HPDataType* a, size_t n, int root)
{
//调整使其成为小堆
assert(a);
//找左右孩子小的那个
int parent = root;
int child = parent * 2 + 1;
while (child < n)
{
if (child + 1 < n && a[child + 1] < a[child])
{
++child;
}
if (a[child] < a[parent])
{
HPDataType tmp = a[child];
a[child] = a[parent];
a[parent] = tmp;
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void HeapInit(Heap* hp, HPDataType* a, size_t n)
{
assert(hp && a);
hp->_array = (HPDataType*)malloc(sizeof(HPDataType)*n);
hp->_size = hp->_capacity = n;
memcpy(hp->_array, a, sizeof(HPDataType)*n);
//通过向下调整使其成为小堆
for (int i = (hp->_size - 1 - 1) / 2; i >= 0; --i)
{
AdjustDown(hp->_array, hp->_size, i);
}
}
void HeapDestroy(Heap* hp)
{
assert(hp);
free(hp->_array);
hp->_size = hp->_capacity = 0;
}
void HeapPrint(Heap* hp)
{
assert(hp);
for (size_t i = 0; i < hp->_size; ++i)
{
printf("%d ", hp->_array[i]);
}
printf("\n");
}
void AdjustUp(HPDataType*a, size_t n, size_t child)//向上调整
{
assert(a);
size_t parent = (child - 1) / 2;
while (child > 0)
{
if (a[child] < a[parent])
{
//交换child和parent
HPDataType tmp = a[child];
a[child] = a[parent];
a[parent] = tmp;
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void HeapPush(Heap* hp, HPDataType x)//插入数据是在末尾插入
{
assert(hp);
if (hp->_size == hp->_capacity)//判断数组空间是否已满
{
hp->_capacity *= 2;
hp->_array = (HPDataType*)realloc(hp->_array, hp->_capacity*sizeof(HPDataType));//增容
assert(hp->_array);//判断增容是否失败
}
hp->_array[hp->_size] = x;
hp->_size++;
//调堆 向上调
AdjustUp(hp->_array , hp->_size, hp->_size - 1);
}
void Swap(HPDataType* p1, HPDataType*p2)
{
HPDataType tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void HeapPop(Heap* hp)
{
assert(hp);
Swap(&hp->_array[0], &hp->_array[hp->_size - 1]);
hp->_size--;
AdjustDown(hp->_array, hp->_size, 0);
}
int HeapSize(Heap* hp)
{
if(hp == NULL){
return 0;
}
return hp->_size;
}
int HeapEmpty(Heap* hp)
{
if(hp == NULL){
return 0;
}
return hp ->_size == 0 ? 1 : 0;
}
//堆排序
void HeapSort(HPDataType*a, size_t n)
{
//建小堆
for (int i = (n - 1 - 1) / 2; i >= 0; --i)
{
AdjustDown(a, n, i);
}
size_t end = n - 1;
while (end > 0)
{
Swap(&a[0], &a[end]);
AdjustDown(a, end, 0);
--end;
}
}
int main()
{
Heap hp;
int a[] = { 1, 4, 3, 6, 5, 2, 9, 7, 8 };
HeapInit(&hp, a, sizeof(a) / sizeof(a[0]));
HeapPrint(&hp);
HeapPush(&hp, 15);
HeapPrint(&hp);
HeapPop(&hp);
HeapPrint(&hp);
HeapSort(a,sizeof(a)/sizeof(a[0]));
HeapPrint(&hp);
HeapSize(&hp);
HeapPrint(&hp);
HeapEmpty(&hp);
HeapPrint(&hp);
return 0;
}
当然,我相信这里可能还是没有实现到最好,如果哪里有问题的话可以提及出来,互相帮助,谢谢