源代码:堆

头文件

#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <math.h>

结构体组成

typedef int HPDataType; 
typedef struct Heap 
{ 
HPDataType* _arry; 
size_t  _size; 
size_t  _capacity; 
}Heap;

Heap函数声明.h

void swap(int *a,int *b);
void HeapInit(Heap *hp,HPDataType* a,size_t n);
void HeapDestory(Heap* hp);

void HeapPrint(Heap *hp);
void CheckCapacity(Heap *hp);
void AdjustDown(HPDataType* a,size_t n,int root);
void HeapPush(Heap *hp,HPDataType x);//插入一个数据,依旧为堆 
void HeapPop(Heap *hp);//删除堆顶的数据,保持为堆 
HPDataType HeapTop(Heap *hp);
int HeapSize(Heap* hp);
int HeapEmpty(Heap* hp);
int HeapSize(Heap* hp); 
int HeapEmpty(Heap* hp); 
HPDataType HeapTop(Heap* hp);
void HeapSort(int* a, int n);

函数实现.c


void swap(int *a,int *b)
{
	int tem;
	tem = *a;
	*a = *b;
	*b = tem;
}

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



void CheckCapacity(Heap *hp)
{
	assert(hp);
	if(hp->_size==hp->_capacity)
	{
	hp->_capacity *= 2;
	hp->_arry = (HPDataType*)realloc(hp->_arry,sizeof(HPDataType)*hp->_capacity);	
	}
	
}

void AdjustUp(HPDataType* a,size_t n,int child)//会出问题 
{
	assert(a);
	//printf("@child:%d",child);
	int parent = (child-1)/2;
	while(child>0)
	{
		if(a[parent] < a[child])
		{
			swap(&a[parent],&(a[child]));
			child  = parent;
			parent = (child-1)/2; 
			//printf("Das parent:%d child:%d",parent,child);
		}
		else
		{
			//printf("Das2");
			break;
		}
	}
	
	
	
}

void AdjustDown(HPDataType* a,size_t n,int root)
//三个大小分别为根,范围,和调整的数组 左右子树需要为有序的大根堆或小根堆 
{
	int parent  = root;
	int child = parent*2+1;
	while(child<n)
	{
	if(child+1<n&&a[child+1]>a[child])
	{
		++child;
	}
	if(a[parent]<a[child])//小根堆 
	{
		HPDataType tmp = a[parent];
		a[parent] = a[child];
		a[child] = tmp;
		parent = child;
	    child =  parent*2+1;
	}
	else
	{
		break;
	}
	}
	
}
void HeapInit(Heap *hp,HPDataType* a,size_t n)
{
	assert(hp&&a);
	hp->_arry = (HPDataType*)malloc(sizeof(HPDataType)*n);
	hp->_size = hp->_capacity = n;
	memcpy(hp->_arry,a,sizeof(HPDataType)*n);	//给 hp->_arry拷贝 sizeof(HPDataType)*n个字节 
	
	for(int i =(hp->_size-2)/2;i>=0;i--)
	{
		AdjustDown( hp->_arry,hp->_size,i);
	}
}

void HeapDestory(Heap* hp)
{
	free(hp->_arry);
	hp->_arry = NULL;
	hp->_capacity = 0;
	hp->_size = 0;
} 

void HeapPush(Heap *hp,HPDataType x)
{
	assert(hp);
	CheckCapacity(hp);
	hp->_arry[hp->_size] = x;
	hp->_size++;
	 
	AdjustUp(hp->_arry,hp->_size,hp->_size-1);
	
}

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

int HeapSize(Heap* hp)
{
	assert(hp);
	return hp->_size;
}

int HeapEmpty(Heap* hp)
{
	assert(hp);
	return( hp->_size==0);//空为真, 
}

HPDataType HeapTop(Heap* hp)
{
assert(hp);
return hp->_arry[0];	

}

void HeapSort(int* a, int n)
{
	Heap hp;
	HeapInit(&hp,a,n);
	int count = 0;
	while(HeapEmpty( &hp)!=1)
	{
		a[count++] = HeapTop(&hp);
		HeapPop(&hp);
	}
	printf("\n");
	for(int i=0; i<n; i++)
{

	printf("  %d  ",a[i]);
	
}	printf("\n");
}

测试函数


void test1()
{
 int a[10]={20,18,16,14,12,10,8,6,4,2};
 Heap hp;
 HeapInit(&hp, a, 10);
  HeapPrint(&hp);
	HeapPush(&hp,19);
	HeapPrint(&hp);
 HeapPop(&hp);
 HeapPop(&hp);
 HeapPop(&hp);
 HeapPop(&hp);
	HeapPrint(&hp);
	printf("\n");
	printf("%d",HeapTop(&hp));
	
}


void test2()
{
	int a[10];
	for(size_t i = 0;i<5;i++)
	a[i] =rand()%20;
	printf("\n");
		
	Heap hp;
	HeapPrint(&hp);
	HeapInit(&hp, a,5);
	HeapPrint(&hp);
}

void test3()
{
	int a[10];
for(size_t i = 0;i<10;i++)
	a[i] =rand()%20;
	HeapSort( a, 10);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值