C语言中堆的TOP-K问题

Heap.h

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

typedef int HPDataType;

typedef struct Heap
{
	HPDataType* arr;
	int size;//有效数据个数
	int capacity;//容量

}HP;
//堆的初始化
void HPInit(HP* php);

//堆的销毁
void HPDestroy(HP* php);

//堆的尾插
void HPPush(HP* php, HPDataType x);

//堆的尾删
void HPPop(HP* php);

//取头结点
HPDataType HPTop(HP* php);

// 判空
bool HPEmpty(HP* php);

//交换
void swap(int* x, int* y);

//上行调整
AdjustUp(HPDataType* arr, int child);

//下行调整
AdjustDown(HPDataType* arr, int parent, int n);

Heap.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"

//堆的初始化
void HPInit(HP* php)
{
	assert(php);

	php->arr = NULL;
	php->capacity = php->size = 0;
}

//堆的销毁
void HPDestroy(HP* php)
{
	assert(php);
	if (php->arr)
	{
		free(php->arr);
	}
	php->arr = NULL;
	php->capacity = php->size = 0;

}

//交换函数
void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

//堆上行
AdjustUp(HPDataType* arr, int child)
{
	int parent = (child - 1) / 2;

	while (child > 0)//child == 0 不需要进入循环
	{
		//建大堆, >
		//建小堆, <

		if (arr[child] < arr[parent])
		{
			swap(&arr[parent], &arr[child]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}


}

//堆的尾插
void HPPush(HP* php, HPDataType x)
{
	assert(php);

	//判断空间够不够
	if (php->capacity == php->size)
	{
		//先判断空间是否为0
		int newCapacity = php->capacity == 0 ? 4 : 2 * php->capacity;
		HPDataType* newnode = (HPDataType*)realloc(php->arr, newCapacity * sizeof(HP));
		if (newnode == NULL)
		{
			perror("malloc fail!");
			exit(1);
		}
		php->arr = newnode;
		php->capacity = newCapacity;
	}
	php->arr[php->size] = x;//这里要先排序再让size++


	//调整堆
	AdjustUp(php->arr, php->size);


	++php->size;//顺序?
}

//堆下行
AdjustDown(HPDataType* arr, int parent, int n)
{
	int child = parent * 2 + 1;//左孩子

	while (child < n)

	{
		//小堆:赵左右孩子中最小的
		//大堆:找左右孩子中最大的

		if (child + 1 < n && arr[child] > arr[child + 1])
		{
			child++;
		}
		if (arr[child] < arr[parent])
		{
			swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}


//堆的尾删
void HPPop(HP* php)
{
	assert(php && php->size);

	//arr[0]  arr[size - 1]交换头尾
	swap(&php->arr[0], &php->arr[php->size - 1]);

	--php->size;

	AdjustDown(php->arr, 0, php->size);//顺序?
}

//判空
bool HPEmpty(HP* php)
{
	assert(php);
	return php->size == 0;
}


//返回堆顶
HPDataType HPTop(HP* php)
{
	assert(php && php->size);

	return php->arr[0];
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1


#include<time.h>
#include"Heap.h"

void swap(int* x, int* y);

//堆排序
void HeapSort(int* arr, int n)
{
	//向下调整时间复杂度优于向上
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(arr, i, n);
	}

	//循环将堆顶数据跟最后位置(会变化,每次减少一个数据)的数据进行交换
	int end = n - 1;
	while (end > 0)
	{
		swap(&arr[0], &arr[end]);
		AdjustDown(arr, 0, n);
		end--;
	}
}

void test01()
{
	HP hp;
	HPInit(&hp);

	int arr[] = { 17, 20, 10, 13, 19, 15 };
	for (int i = 0; i < 6; i++)
	{
		HPPush(&hp, arr[i]);//将数据插入堆
	}
	int k = 3;
	while (!HPEmpty(&hp))
	{
		printf("%d ", HPTop(&hp));
		HPPop(&hp);
	}

	HPDestroy(&hp);
}


//TOP-k
void TOPK()
{
	int k = 0;
	printf("请输入k:");
	scanf("%d", &k);

	const char* file = "data.txt";
	FILE* fout = fopen(file, "r");
	if (fout == NULL)
	{
		perror("fopen fail!");
		exit(1);
	}
	int* minHeap = (int*)malloc(k * sizeof(int));
	if (minHeap == NULL)
	{
		perror("malloc fail!");
		exit(2);
	}

	//从文件中提取前k个数据
	for (int i = 0; i < k; i++)
	{
		fscanf(fout, "%d", &minHeap[i]);
	}

	//建堆--小堆
	for (int i = (k-1-1) / 2; i >= 0; i--)//i为最后一个元素的父节点
	{
		AdjustDown(minHeap, i, k);//调整堆结构
	}
	 
	int x = 0;
	while (fscanf(fout, "%d", &x) != EOF)
	{
		//读取到的数据和堆顶元素进行比较
		//比较谁大,交换入堆
		if (x > minHeap[0])
		{
			minHeap[0] = x;//直接覆盖
			AdjustDown(minHeap, 0, k);//换进来的元素都在堆顶节点
		}
	}

	for (int i = 0; i < k; i++)
	{
		printf("%d ", minHeap[i]);
	}

	fclose(fout);
}










//创建数据---仅一次

//void CreateNDate()
//{
//	int n = 100000;
//	srand(time(0));
//	const char* file = "data.txt";
//	FILE* fin = fopen(file, "w");
//	if (fin == NULL)
//	{
//		perror("fail!");
//		exit(1);
//	}
//	for (int i = 0; i < n; ++i)
//	{
//		int x = (rand() + i )% 1000000;
//		fprintf(fin, "%d\n", x);
//	}
//	fclose(fin);
//}

int main()
{
	/*test01();*/
	//CreateNDate();

	/*CreateNDate();*/

	TOPK();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值