堆的基本操作

相关元素的定义:

  • 顺序表实现
typedef int HPDataType;
typedef struct Heap {
	HPDataType* a;
	int size;
	int capacity;
}HP;

头文件内容:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<assert.h>

typedef int HPDataType;
typedef struct Heap {
	HPDataType* a;
	int size;
	int capacity;
}HP;

void AdjustUp(HPDataType* a, int child);
void AdjustDown(HPDataType *a, int n, int parent);
void Swap(HPDataType* px, HPDataType* py);
void HeapInit(HP* hp);
void HeapDestory(HP* hp);
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 HeapSort(HPDataType* a, int n);
void PrintTopK(HPDataType* a, int n, int k);
void TestTopk();

Heap.c中相关接口实现:

#include"Heap.h"
void Swap(HPDataType* px, HPDataType* py) {	//	交换函数
	HPDataType tmp = *px;
	*px = *py;
	*py = tmp;
}
void HeapInit(HP* hp) {	//	初始化堆
	assert(hp);
	hp->a = NULL;
	hp->size = 0;
	hp->capacity = 0;
}
void HeapDestory(HP* hp) {
	assert(hp);
	free(hp->a);
	hp->size = 0;
	hp->capacity = 0;
}
void AdjustUp(HPDataType* 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 HeapPrint(HP* hp) {
	for (int i = 0; i < hp->size; i++) {
		printf("%d ", hp->a[i]);
	}
	printf("\n");
}
void HeapPush(HP* hp, HPDataType x) {
	assert(hp);
	if (hp->capacity == hp->size) {	//考虑扩容
		int newCapacity = hp->capacity == 0 ? 4 : 2 * (hp->capacity);
		HPDataType* tmp = realloc(hp->a, sizeof(HPDataType)*newCapacity);
		if (tmp == NULL) {
			printf("realloc fail\n");
			exit(-1);
		}
		hp->a = tmp;
		hp->capacity = newCapacity;
	}
	hp->size++;
	hp->a[hp->size] = x;

	AdjustUp(hp->a, hp->size - 1);
}
bool HeapEmpty(HP* hp) {
	assert(hp);

	return hp->size == 0;
}
int HeapSize(HP* hp) {
	assert(hp);
	
	return hp->size;
}
HPDataType HeapTop(HP* hp) {	//读取堆顶元素

	assert(hp);
	assert(!HeapEmpty(hp));

	return hp->a[0];
}
void AdjustDown(HPDataType* 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;
		}
	}
}
//删除栈顶元素,删除栈顶元素时,栈顶元素和最后一个元素的位置交换,然后向下调整
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);
}
void HeapSort(HPDataType* a, int n) {
	

	for (int i = (n - 1 - 1) / 2; i >= 0; i--) {
		AdjustDown(a, n, i);
	}
	for (int end = n - 1; end > 0; end--) {
		Swap(&a[end], &a[0]);
		AdjustDown(a, end, 0);
	}
}

void PrintTopK(HPDataType* a, int n, int k)
{
	HP hp;
	HeapInit(&hp);
	// 创建一个K个数的小堆
	for (int i = 0; i < k; ++i)
	{
		HeapPush(&hp, a[i]);
	}

	// 剩下的N-K个数跟堆顶的数据比较,比他大,就替换他进堆
	for (int i = k; i < n; ++i)
	{
		if (a[i] > HeapTop(&hp))
		{
			HeapPop(&hp);
			HeapPush(&hp, a[i]);
			//hp.a[0] = a[i];
			//AdjustDown(hp.a, hp.size, 0);
		}
	}

	HeapPrint(&hp);

	HeapDestroy(&hp);
}

void TestTopk()
{
	int n = 1000000;
	int* a = (int*)malloc(sizeof(int)*n);
	srand(time(0));
	for (size_t i = 0; i < n; ++i)
	{
		a[i] = rand() % 1000000;
	}
	// 再去设置10个比100w大的数
	a[5] = 1000000 + 1;
	a[1231] = 1000000 + 2;
	a[5355] = 1000000 + 3;
	a[51] = 1000000 + 4;
	a[15] = 1000000 + 5;
	a[2335] = 1000000 + 6;
	a[9999] = 1000000 + 7;
	a[76] = 1000000 + 8;
	a[423] = 1000000 + 9;
	a[3144] = 1000000 + 10;
	PrintTopK(a, n, 10);
}

main.c的相关内容:

#include"Heap.h"
int main() {
	int a[] = { 70,56,30,25,15,10,75,33,50,69 };
	HP hp;
	HeapInit(&hp);
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
		printf("%d ", a[i]);
	}
	printf("\n");
	HeapSort(a, sizeof(a) / sizeof(a[0]));
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
		printf("%d ", a[i]);
	}
	printf("\n");
	
	return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值