堆的相关接口

heap.h

#pragma once
#include <assert.h>
#include <malloc.h>
#include <stdio.h>


typedef int HPDataType;
typedef struct Heap
{
	HPDataType* _a;
	int _size;
	int _capacity;
}Heap;

void AdjustUp(HPDataType *a, int n, int child);
void AdjustDown(HPDataType *a, int n, int root);
void HeapInit(Heap* hp, HPDataType* a, int n);
void HeapDestory(Heap* hp);
void HeapPush(Heap* hp, HPDataType x);
void HeapPop(Heap* hp);
HPDataType HeapTop(Heap* hp);
int HeapSize(Heap* hp);
int HeapEmpty(Heap* hp);
void HeapPrint(Heap* hp);

// 不要直接调Heap
void HeapSort(HPDataType* a, int n);

void TestHeap();

heap.c

#include"heap.h"
void swap(HPDataType * x1, HPDataType* x2)
{
	HPDataType * x = x1;
	x1 = x2;
	x2 = x;
}
void AdjustUp(HPDataType * _a, int n, int child)
{
	assert(_a);
	int parent = (child - 1) / 2;
	while (parent >= 0)
	{
		if (_a[parent] < _a[child])
		{
			swap(&_a[parent], &_a[child]);
			child = parent;
			parent = (child - 1) / 2;
			
		}
		else
		{
			break;
		}
	}
	printf("\n");
}

void AdjustDown(HPDataType *_a, int n, int root)
{
	int parent = root;
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (_a[child + 1]>_a[child])
		{
			++child;
		}
		if (_a[child] < _a[parent])
		{
			swap(&_a[child], &_a[parent]);
		}
		else
		{
			break;
		}
	}
}

void HeapInit(Heap* hp, HPDataType* _a, int n)
{
	int i;
	assert(hp != NULL&&_a != 0);
	hp->_a = (HPDataType*)malloc(sizeof(HPDataType)*n);
	hp->_size = n;
	hp->_capacity = n;
	for (i = 0; i < n; i++)
	{
		hp->_a[i] = _a[i];
	}
	for (i = (n - 2) / 2; i >= 0; --i)
	{
		AdjustDown(hp->_a, hp->_size, i);
	}
}
void HeapDestory(Heap* hp)
{
	assert(hp != NULL);
	free(hp->_a);
	hp->_a = NULL;
	hp->_size = hp->_capacity = 0;
}
void HeapPush(Heap* hp, HPDataType x)
{
	assert(hp != NULL);
	if (hp->_size == hp->_capacity)
	{
		hp->_capacity *= 2;
		hp->_a = (HPDataType*)realloc(hp->_a,sizeof(HPDataType)*hp->_capacity);
	}
	hp->_a[hp->_size] = x;
	hp->_size++;
}
void HeapPop(Heap* hp)
{
	assert(hp != NULL);
	swap(&hp->_a[0], &hp->_a[hp->_size - 1]);
	hp->_size--;
	AdjustDown(hp->_a, hp->_size,0);
}
HPDataType HeapTop(Heap* hp)
{
	assert(hp != NULL);
	return hp->_a[0];
}
int HeapSize(Heap* hp)
{
	assert(hp != NULL);
	return hp->_size;
}
int HeapEmpty(Heap* hp)
{
	assert(hp != NULL);
	return hp->_size = 0 ? 0 : 1;
}
void HeapPrint(Heap* hp)
{
	assert(hp != NULL);
	for (int i = 0; i < hp->_size; ++i)
	{
		printf("%d",hp->_a[i]);
	}
	printf("\n");
}

// 不要直接调Heap
void HeapSort(HPDataType* _a, int n)
{
	int i, end;
	for (i = (n - 2) / 2; i >= 0; --i)
	{
		AdjustDown(_a, n, i);
	}
	end = n - 1;
	while (end)
	{
		swap(&_a[0], &_a[end]);
		AdjustDown(_a, n, i);
		--end;
	}
}

test.c

#include"heap.h"
void TestHeap()
{
	int ret = 0;
	int size = 0;
	int n = 0;
	int arr[] = { 53, 17, 78, 9, 45, 65, 87, 23, 31 };
	Heap hp;
	HeapInit(&hp, arr, sizeof(arr) / sizeof(HPDataType));
	HeapPrint(&hp);
	HeapPush(&hp, 6);
	HeapPrint(&hp);
	HeapPop(&hp);
	HeapPrint(&hp);
	ret = HeapTop(&hp);
	printf("%d\n", ret);
	size = HeapSize(&hp);
	printf("%d\n", size);
	n = HeapEmpty(&hp);
	if (n == 0)
	{
		printf("堆为空\n");
	}
	else
	{
		printf("堆不为空\n");
	}
	
	

}
int main()
{
	TestHeap();
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值