数据结构 堆 的讲解与操作(C语言)实现

堆的概念与结构

概念:如果有一个关键码的集合K={ k0,k1 ,k2 ,…,kn-1 },把它的所有元素按完全二叉树的顺序存储方式存储 在一个一维数组中,并满足K i<=K 2*i+1且Ki<=K 2*i+2(K i>=K 2*i+1且Ki>=K 2*i+2) i = 0,1,2...,则称为小堆(或大堆)。将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。

性质:

  • 堆中某个节点的值总是不大于或不小于其父节点的值;
  • 堆总是一棵完全二叉树。

结构:

1.大堆

2.小堆

 堆(大根堆)的实现:

堆的接口:

#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 HeapInit(HP*hp);
//堆的销毁
void HeapDestroy(HP*hp);
//交换数值
void Swap(HPDataType *px, HPDataType *py);
//向上调整
void AdjustUp(int *a,int child);
//向下调整
void AdjustDown(int *a, int n, int parent);
//堆的插入
void HeapPush(HP*hp,HPDataType x);
//堆的删除
void HeapPop(HP*hp);
//取堆顶的数据
HPDataType HeapTop(HP*hp);
//打印堆
void HeapPrint(HP*hp);
//堆的判空
bool HeapEmpty(HP*hp);
//堆的数据个数
int HeapSize(HP*hp);

堆的创建:

void HeapInit(HP*hp)
{
    assert(hp);
    hp->a=NULL;
    hp->capacity=hp->size=0;
}

堆的销毁:

void HeapDestroy(HP*hp)
{
    assert(hp);
    free(hp->a);
    hp->capacity=hp->size=0;
}

交换数值:

void Swap(HPDataType*px, HPDataType*py)
{
    HPDataType tmp = *px;
    *px=*py;
    *py=tmp;
}

 向上调整:

void AdjustUp(int*a, int child)
{
	assert(a);
	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 HeapPush(HP*hp,HPDataType x)
{
    assert(hp);
    if(hp->capacity==hp->size)
    {
        size_t newCapacity=hp->capacity==0?4:hp->capacity*2;
        HPDataType * tmp=(HPDataType*)realloc(hp->a,newCapacity*sizeof(HPDataType));
        if(tmp==NULL)
        {
            printf("realloc is failn");
        }
        hp->a=tmp;
        hp->capacity=newCapacity;
        
    }
    hp->a[hp->size-1]=x;
    hp->size++;
    //向上调整
    AdjusiUp(hp->a,hp->size-1);
}

向下调整:

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[child],&a[parent]);
            parent=child;
            child=parent*2+1;
        }
        else
        {
            break;
        }
    }
}

堆的判空:

bool HeapEmpty(HP*hp)
{
    assert(hp);
    return hp->size==0;
}

堆的删除:

void HeapPop(HP*hp)
{
	assert(hp);
    //堆的判空
	assert(!HeapEmpty(hp));
    //交换数值
	Swap(&hp->a[0], &hp->a[hp->size - 1]);
	hp->size--;
    //向下调整
	AdjustDown(hp->a, hp->size, 0);
}

取堆顶的数据:

HPDataType HeapTop(HP*hp)
{
    assert(hp);
    assert(!HeapEmpty(hp));
    return hp->a[0];
}

打印堆:

void HeapPrint(HP*hp)
{
    assert(hp);
    assert(!HeapEmpty(hp));
    for(int i=0; i<hp->size;i++)
    {
        printf("%d",hp->a[i]);
    }
    printf("n");
}

堆的数据个数:

int HeapSize(HP*hp)
{
    assert(hp);
    return hp->size;
}

以上就是对堆的讲解与操作实现。

感谢老铁看到这里,如果文章对你有用的话请给我一个支持一下,感激不尽!

在下才疏学浅,一点浅薄之见,欢迎各位大佬指点

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>