讲解请至博客【数据结构】二叉树的顺序结构及实现,堆,向上调整算法,向下调整算法,数组建堆算法,堆排序
一.堆的实现
Heap.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int HPDataType;
//定义二叉树——堆的数据结构
typedef struct Heap
{
HPDataType* a;//堆的物理结构:数组,类似顺序表的实习方式
int size;//已有的数据个数
int capacity;//容量
}HP;
//函数接口
//打印堆中的元素
void HeapPrint(HP* php);
void HeapInit(HP* php);//接收在主函数中,创建的堆的地址
void HeapDestory(HP* php);//销毁堆
void HeapPush(HP* php, HPDataType x);//插入数据到堆中
void HeapPop(HP* php);//删除堆中的数据
HPDataType HeapTop(HP* php);//获取堆顶元素,即数组的第一个元素
bool HeapEmpty(HP* php);//判断堆是否为空
int HeapSize(HP* php);//堆中的数据个数
Heap.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"
void HeapInit(HP* php)//接收在主函数中,创建的堆的地址
{
assert(php);
php->a = NULL;
php->size = php->capacity = 0;
}
void HeapDestory(HP* php)//销毁堆
{
assert(php);
free(php->a);
php->a = NULL;
php->size = php->capacity = 0;
}
void HeapPrint(HP* php)//打印堆的元素,即打印数组元素
{
assert(php);
for (int i = 0; i < php->size; i++)
{
printf("%d ", php->a[i]);
}
printf("\n");
}
void Swap(HPDataType* e1, HPDataType* e2)
{
HPDataType tmp = *e1;
*e1 = *e2;
*e2 = tmp;
}
//向上调整算法---O(logN)
void AdjustUp(HPDataType* a,int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (a[child] < a[parent])
{
Swap(&a[child], &a[parent]);
//向上调整
child = parent;
parent = (child - 1) / 2;
}
else//满足小堆条件
{
break;
}
}
}
//插入x,保持堆形态
void HeapPush(HP* php, HPDataType x)//插入数据到堆中
{
assert(php);
//扩容
if (php->size == php->capacity)
{
int newcapacity = php->capacity == 0 ? 4 : php->capacity * 2;
HPDataType* tmp = (HPDataType*)realloc(php->a, newcapacity * sizeof(HPDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
php->a = tmp;
php->capacity = newcapacity;
}
php->a[php->size] = x;
php->size++;
//向上调整函数
AdjustUp(php->a, php->size - 1);
}
//向下调整算法---O(logN)
void AdjustDown(HPDataType* a,int size,int parent)
{
//先假设左边的孩子是最小的
int minchild = parent * 2 + 1;
while (minchild < size)
{
//找出小的那个孩子进行交换
if (minchild+1<size && a[minchild + 1] < a[minchild])
{
minchild += 1;
}
if (a[minchild] < a[parent])
{
Swap(&a[parent], &a[minchild]);
parent = minchild;
minchild = 2 * parent + 1;
}
else
{
break;
}
}
}
void HeapPop(HP* php)//删除堆中的数据。保持堆形态
{
assert(php);
assert(!HeapEmpty(php));
//方法:交换堆顶和最后一个元素
//如果用挪动的方式,时间复杂度为O(N),而用向下调整算法,时间复杂度只有O(logN)
Swap(&php->a[0], &php->a[php->size - 1]);
php->size--;
AdjustDown(php->a, php->size, 0);//从堆顶的位置开始,向下调整
}
HPDataType HeapTop(HP* php)//获取堆顶元素,即数组的第一个元素
{
assert(php);
assert(!HeapEmpty(php));
return php->a[0];
}
bool HeapEmpty(HP* php)//判断堆是否为空
{
assert(php);
return php->size == 0;
}
int HeapSize(HP* php)//堆中的数据个数
{
assert(php);
return php->size;
}
Test.h
#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"
int main()
{
int a1[] = { 15,18,19,25,28,34,65,49,27,37 };
HP hp;
HeapInit(&hp);
for (int i = 0; i < sizeof(a1) / sizeof(int); i++)
{
HeapPush(&hp, a1[i]);//一边向堆中插入数据,一边进行向上调整
}
/*HeapPrint(&hp);
HeapPush(&hp, 10);
HeapPrint(&hp);*/
HeapPop(&hp);
HeapPrint(&hp);
while (!HeapEmpty(&hp))
{
printf("%d ", HeapTop(&hp));
HeapPop(&hp);
HeapPrint(&hp);
}
return 0;
}
二.向下(上)调整建堆
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void Swap(int* e1, int* e2)
{
int tmp = *e1;
*e1 = *e2;
*e2 = tmp;
}
void AdjustUp(int* a, int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (a[child] > a[parent])//建大堆
{
Swap(&a[child], &a[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void AdjustDown(int* a, int size, int parent)
{
//先假设左边的孩子是最小的
int minchild = parent * 2 + 1;
while (minchild < size)
{
//找出小的那个孩子
if (minchild + 1 < size && a[minchild + 1] > a[minchild])
{
minchild++;
}
if (a[minchild] > a[parent])
{
Swap(&a[minchild], &a[parent]);
parent = minchild;
minchild = 2 * parent + 1;
}
else
{
break;
}
}
}
void HeapCreate(int* a, int n)
{
//向上调整建堆,第一个元素保持不动
for (int i = 1; i < n; i++)
{
AdjustUp(a, i);//物理上是个数组,在数组的基础上建堆,从第2个数据开始插入,一边插入一边建堆
}
//向下调整建堆
//n-1是最后一个数据的下标,再-1/2算的就是最后一个结点的父节点
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(a, n, i);
}
}
void HeapPrint(int* a, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
int main()
{
int a[] = { 65,100,60,32,50,70 };
//int a[] = { 15,1,19,25,8,34,65,4,27,7 };
HeapCreate(a, sizeof(a) / sizeof(a[0]));
HeapPrint(a, sizeof(a) / sizeof(a[0]));
}
三.堆排序
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void Swap(int* e1, int* e2)
{
int tmp = *e1;
*e1 = *e2;
*e2 = tmp;
}
void AdjustDown(int* a, int size, int parent)
{
//先假设左边的孩子是最小的
int minchild = parent * 2 + 1;
while (minchild < size)
{
//找出大的那个孩子
if (minchild + 1 < size && a[minchild + 1] > a[minchild])
{
minchild++;
}
if (a[minchild] > a[parent])
{
Swap(&a[minchild], &a[parent]);
parent = minchild;
minchild = 2 * parent + 1;
}
else
{
break;
}
}
}
void HeapSort(int* a, int n)
{
//堆排序的大思路,也是一种选择排序
//升序---大堆
//降序---小堆
//先建堆:向下调整建堆---O(N)
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(a, n, i);
}
//此时堆顶是最大元素,交换堆顶元素与最后一个元素
//再将剩下的n-1个元素重新向下调整,找到次大的
int i = 1;
while (i < n)
{
Swap(&a[0], &a[n - i]);
AdjustDown(a, n-i, 0);
i++;
}
}
void HeapPrint(int* a, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
int main()
{
int a[] = { 65,27,34,25,8,15,19,4,1,7 };
HeapSort(a, sizeof(a) / sizeof(int));
HeapPrint(a, sizeof(a) / sizeof(int));
}