堆的实现算法

堆的实现算法

代码:
#include
#include
#include <Windows.h>

using namespace std;

#define DEFAULT_CAPACITY 128

//堆的生成
typedef struct _Heap {
int *arr;
int size;
int acpacity;
}_Heap;

//堆的接口
bool heapInit_1(_Heap &heap, int *origal, int size);
bool heapInit_2(_Heap &heap, int *origal, int size);
bool insert(_Heap &heap, int value);
bool popMax(_Heap&heap, int *p_value);
static void adjustUp(_Heap &heap, int index);
static void adjustDown(_Heap &heap, int index);
static void buildHeap(_Heap &heap);
void heapPrint(_Heap &heap);

//遍历所有堆值
static void adjustDown(_Heap &heap, int index) {
int cur = heap.arr[index];

int parent, child;

for (parent = index; parent * 2 + 1 < heap.size; parent = child) {
	//如果父节点的左子节点存在
	child = parent * 2 + 1;

	if (child + 1 < heap.size && heap.arr[child + 1] > heap.arr[child]) {
		child = child + 1;
	}
	  
	//child是最大的子节点的下标
	if (heap.arr[child] > cur) {
		heap.arr[parent] = heap.arr[child];
		heap.arr[child] = cur;
	}
	else {
		break;
	}
}

}

//建堆
static void buildHeap(_Heap &heap) {
if (!heap.arr || heap.size <= 0) {
return;
}

for (int i = (heap.size - 1 - 1) / 2; i >= 0; i--) {
	adjustDown(heap, i);
}

}

//初始化1
bool heapInit_1(_Heap &heap, int *origal, int size) {
if (!origal || size <= 0) {
return false;
}

//容量
int capacity = (DEFAULT_CAPACITY > size ? DEFAULT_CAPACITY : size);

heap.arr = (int*)(malloc(sizeof(int) * capacity));
if (!heap.arr) {
	return false;
}

//容量
heap.acpacity = capacity;

//元素默认为0
heap.size = 0;

//拷贝值
memcpy(heap.arr, origal, size * sizeof(int));

//设置元素值
heap.size = size;

//建堆
buildHeap(heap);

return true;

}

//初始化2
bool heapInit_2(_Heap &heap, int *origal, int size) {
if (!origal || size <= 0) {
return false;
}

//容量
int capacity = (DEFAULT_CAPACITY > size ? DEFAULT_CAPACITY : size);

heap.arr = (int*)(malloc(sizeof(int) * capacity));
if (!heap.arr) {
	return false;
}

//容量
heap.acpacity = capacity;

//默认元素为0
heap.size = 0;

for (int i = 0; i < size; i++) {
	insert(heap, origal[i]);
}

return true;

}

//向上遍历
//插入的节点不能<0 在0节点插入是可以的,并且不能>=size
static void adjustUp(_Heap &heap, int index) {
if (!heap.arr || index < 0 || index >= heap.size) {
return;
}

int cur = heap.arr[index];

int parent, child;

for (child = index; child > 0; child = parent) {
	//父节点
	parent = (child - 1) / 2;

	//插入的节点
	if (parent >= 0 && cur > heap.arr[parent]) {
		heap.arr[child] = heap.arr[parent];
		heap.arr[parent] = cur;
	}
	else {
		//越界结束循环
		break;
	}
}

}

//插入堆
//在最后插入
//插入的时候要判断空间
bool insert(_Heap &heap, int value) {
//防御性编程
if (!heap.arr || heap.acpacity == heap.size || heap.size < 0) {
return false;
}

//插入的节点下标值
int index = heap.size;

//值插入
heap.arr[heap.size++] = value;

//向上便利
adjustUp(heap, index);

return true;

}

bool popMax(_Heap &heap, int * p_value){
if (!p_value || !heap.arr || heap.size <= 0) {
return false;
}

//把数组第一个数
*p_value = heap.arr[0];

//赋值最后一个节点数
heap.arr[0] = heap.arr[--heap.size];

//再次从上面遍历下去
//从0这个节点开始
//从上往下调整
adjustDown(heap, 0);

return true;

}

//输出堆
void heapPrint(_Heap &heap) {
if (!heap.arr || heap.size <= 0) {
return;
}

cout << "元素容量为:" << heap.acpacity << " 元素个数为:" << heap.size << endl;
for (int i = 0; i < heap.size; i++) {
	printf("The %dth element:%d\n", i, *(heap.arr + i));
}
cout << endl;

}

int main(void) {
_Heap heap;

int value = 0;

int origal[] = { 13,15,52,43,20,31,34,55,67,12 };

int size = sizeof(origal) / sizeof(int);

if (!heapInit_2(heap, origal, size)) {
	fprintf(stderr, "建堆失败!\n");
}

//输出堆
heapPrint(heap);

printf("最大元素以此出列: \n");
while (popMax(heap, &value)) {
	printf("%d ", value);
}

cout << endl;


system("pause");

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘉人、LiangYing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值