C语言堆的函数实现

文章详细描述了如何使用C语言实现堆数据结构,包括Swap、AdjustUp、AdjustDown、HeapInit、HeapPush、HeapPop、HeapTop等函数,以及它们在排序和数据管理中的应用。
摘要由CSDN通过智能技术生成

HP.c

//建立大堆
#include"HP.h"
//工具函数(辅助操作
void Swap(HPDataType* a, HPDataType* b)
{
    HPDataType mid = *a;
    *a = *b;
    *b = mid;
}
void AdjustUp(heap* hp,int n)
{
#if 1
    for (int child = n, parent = (child - 1) / 2; child > 0; child = parent, parent = (child - 1) / 2)//孩子最多到根停止,孩子父亲置换更迭
    {
        if (hp->arr[child] > hp->arr[parent])
        {
            Swap(hp->arr + child, hp->arr + parent);
        }
        else
            break;
    }
#endif
#if 0//第二种写法
    int child = n;
    int parent = (n - 1) / 2;
    while (child > 0)
    {
        if (hp->arr[child] > hp->arr[parent])
        {
            Swap(hp->arr + child, hp->arr + parent);
            child = parent;
            parent = (child - 1) / 2;
        }
        else
            break;
    }
#endif
}
//基本操作函数

//初始化(也可以建立hp,返回hp空间,这里直接依赖其他函数建立传参hp)
void HeapInit(heap* hp, HPDataType* a, int arrysize)
{
    //考入数组
    hp->capacity = hp->size = arrysize;
    hp->arr = (HPDataType*)malloc(sizeof(HPDataType) * arrysize);
    assert(hp->arr);
    memcpy(hp->arr, a, sizeof(HPDataType) * arrysize);
    //堆排序
#if 0//向上调整建堆,时间复杂度N*LOGN
    for (int i = 1; i < arrysize; i++)
    {
        AdjustUp(hp, i);
    }
#endif
#if 1//向下调整建堆时间复杂度On
    for (int i = (hp->size - 2) / 2; i >= 0; i--)
    {
        AdjustDown(hp, i);
    }
#endif
}

//插入排序
void HeapPush(heap* hp, HPDataType x)
{
    if (hp->size == hp->capacity)
    {
        int newcapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
        HPDataType* pcur = (HPDataType*)realloc(hp->arr,sizeof(HPDataType) * newcapacity);
        if (!pcur)
        {
            perror("realloc->");
            exit(1);
        }
        hp->arr = pcur;
        hp->capacity = newcapacity;
    }
    hp->arr[hp->size] = x;
    hp->size++;
    AdjustUp(hp, hp->size - 1);
}
void AdjustDown(heap* hp, int n)
{
    assert(hp);
    int parent = n;
    int bigchild = parent * 2 + 1;//左孩子等于父亲乘2+1,右等于乘2+2
    while (bigchild < hp->size)
    {
        if (parent*2+2 <hp->size&&hp->arr[bigchild] < hp->arr[parent * 2 + 2])
            bigchild = parent * 2 + 2;//要换之前找到最大的孩子
        if (hp->arr[bigchild] > hp->arr[parent])
            Swap(hp->arr + bigchild, hp->arr + parent);
        else
            break;
        //
        parent = bigchild;
        bigchild = parent * 2 + 1;
    }
}
void HeapPop(heap* hp)
{
    assert(hp);
    assert(hp->arr);
    Swap(hp->arr, hp->arr + hp->size - 1);
    hp->size--;
    AdjustDown(hp, 0);
}
HPDataType HeapTop(heap* hp)
{
    assert(hp);
    assert(hp->arr);
    return hp->arr[0];
}
// 堆的数据个数
int HeapSize(heap* hp)
{
    assert(hp);
    return hp->size;
}
// 堆的判空
int HeapEmpty(heap* hp)
{
    return hp->size == 0;
}
void HeapDestroy(heap* hp)
{
    assert(hp);
    free(hp->arr);
    hp->arr = NULL;
    hp->size = hp->capacity = 0;
}

HP.h

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
//堆是完全二叉树,物理结构是顺序表,逻辑结构是完全二叉树
typedef int HPDataType;
typedef struct {
    HPDataType* arr;
    int capacity;
    int size;
}heap;
//传数组初始化,销毁,插入,头删排序,查询最大/小值,向上调整,向下调整,判空,数据个数
//建立大堆
void Swap(HPDataType* a, HPDataType* b);
void AdjustUp(heap* hp);
void HeapInit(heap* hp, HPDataType* a, int arrysize);
void AdjustDown(heap* p, int n);
void HeapPush(heap*hp,HPDataType x);
void HeapPop(heap* hp);
HPDataType HeapTop(heap* hp);
// 堆的数据个数
int HeapSize(heap* hp);
// 堆的判空
int HeapEmpty(heap* hp);
void HeapDestroy(heap* hp);

main.c

#include"HP.h"
int main()
{
    int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
    heap p;
    heap* hp = &p;
    HeapInit(hp, a, 10);
    printf("top = %d \n", HeapTop(hp));
    HeapPop(hp);
    printf("top = %d \n", HeapTop(hp));
    HeapPush(hp, 12);
    printf("top = %d \n", HeapTop(hp));
    HeapDestroy(hp);
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值