【数据结构】-初阶-堆

理解数据结构:C语言实现初级堆
本文介绍数据结构中的基本概念——堆,并通过C语言实现了一个初级的堆数据结构,包括创建、插入和删除操作。源代码包含Heap.h头文件和Heap.c实现文件,适合初学者学习。

Heap.h

typedef int  HPDataType;

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

void HeapCreate(Heap* hp, HPDataType* a, int n);

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

void HeapSort(int*a, int n);

int * getLeastNumbers(int *a, int arrSize, int k);

void PrintTopK(int * a, int n);

Heap.c

void Swap(int * p1, int* p2)
{
	int tmp = *p1;
	*p1= *p2;
	*p2 = tmp;
	
}

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

}

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

void HpPrint(Heap* hp)
{
	for (int i = 0; i < hp->_size; i++)
	{
		printf("%d ", hp->_a[i]);
		
	}
	printf("\n");
	
}


void HeapCreate(Heap* hp, HPDataType* a, int n)
{
	assert(hp&&a);
	hp->_a = (HPDataType*)malloc(sizeof(HPDataType)*n);
	hp->_size = hp->_capacity = n;
	memcpy(hp->_a, a, sizeof(HPDataType)*n);

	for (int i = (hp->_size - 1 - 1) / 2; i >= 0; --i)
	{
		AdjustDown(hp->_a, hp->_size, i);
	}
}

void HeapDestory(Heap* hp)
{
	assert(hp);
	free(hp->_a);
	hp->_a = NULL;
	hp->_size = hp->_capacity = 0;
}
void HeapPush(Heap* hp, HPDataType x)
{
	assert(hp);
	if (hp->_size == hp->_capacity)
	{
		hp->_capacity *= 2;
		hp->_a = (HPDataType*)realloc(hp->_a, hp->_capacity*sizeof(HPDataType));
		assert(hp->_a);
	}
	hp->_a[hp->_size] = x;
	hp->_size++;
	AdjustUp(hp->_a, hp->_size, hp->_size - 1);

}
void HeapPop(Heap* hp)
{
	assert(hp);
	Swap(&hp->_a[0], &hp->_a[hp->_size - 1]);
	hp->_size--;
	AdjustDown(hp->_a, hp->_size, 0);
}

HPDataType HeapTop(Heap* hp)
{
	assert(hp);
	return hp->_a[0];
}
int HeapSize(Heap* hp)
{
	assert(hp);
	return hp->_size;
}
int HeapEmpty(Heap* hp)
{
	assert(hp);
	return hp->_size == 0 ? 1 : 0;
}
void AdjustUp(int * a, int n, 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;
		}
	}
}

int * getLeastNumbers(int *a, int arrSize, int k)
{
	if (k == 0)
	{
		return NULL;
	}
	int * arrRet = (int *)malloc(sizeof(int)*k);
	for (int i = 0; i < k; i++)
	{
		arrRet[i] = a[i];
	}

	for (int j = (k - 1 - 1) / 2; j >= 0; j--)
	{
		AdjustDown(arrRet, k, j);
	}
	for (int i = k; i < arrSize; i++)
	{
		if (a[i] < arrRet[0])
		{
			arrRet[0] = a[i];
			AdjustDown(arrRet, k, 0);
		}
	}
	return arrRet;
}

void PrintTopK(int * a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
		
	printf("\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值