堆的实现算法
代码:
#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;
}