最小堆的实现(C/C++)
#include <iostream> using namespace std; #define HEAP_MAX 120 typedef struct _Heap { int size;//元素个数 int capacity;//容量 int* arr;//堆数组 }Heap; //设置最小堆 void SetHeap(Heap& heap, int index) { int temp = heap.arr[index];//待调整的父节点 int parent, children; //cout << "INDEX==" << index << endl; //cout << "heap.arr[index]==" << heap.arr[index] << endl; //子节点下是否还有别的节点 有则进行判断 for (parent = index; (parent * 2 + 1) < heap.size; parent = children) { children = parent * 2 + 1; //子节点判断 if (((children + 1) > heap.size) && (heap.arr[children] > heap.arr[children + 1]))//++错误 { children++; } //父节点小于子节点则跳出 //heap.arr[parent] 错误 if (temp < heap.arr[children]) { //cout << "TEMP == ," << temp << "heap.arr[children]) == " << heap.arr[children] << endl; //cout << "准备跳出" << endl; break; } else { heap.arr[parent] = heap.arr[children]; heap.arr[children] = temp; //cout << "heap.arr[children]=="<<heap.arr[children]<<"\tTEMP=="<< temp << endl; } } } //找到最后一个父节点 void parentUp(Heap& heap) { int i; //for (i = (heap.size / 2 - 1); i < heap.size; i++) for (i = heap.size / 2 - 1; i >= 0; i--) { SetHeap(heap, i); //cout << "找到最后一个父节点(i = (heap.size / 2 - 1)" << i << endl; } } //初始化 bool initHeap(Heap& heap, int* data, int size) { int capacity = (HEAP_MAX > size ? HEAP_MAX : size); //cout << "capacity==" << capacity << endl; heap.arr = new int[capacity];//分配空间 if (!heap.arr)return false; heap.capacity = capacity; heap.size = 0; if (size > 0) { memcpy(heap.arr, data, size * sizeof(int)); heap.size = size; //cout << "size * sizeof(int)" << size * sizeof(int) << endl; parentUp(heap); } else { heap.size = 0; } return true; } //最小值出列 bool OutColumn(Heap& heap, int& values) { if (heap.size < 1) return false; values = heap.arr[0]; //cout << "出队数据:" << values << endl; heap.arr[0] = heap.arr[--heap.size]; SetHeap(heap, 0); return true; } //排序 队尾插入的值 void SotingUp(Heap& heap, int index) { if (index<0 || index>heap.size) return; while (index > 0) { int temp = heap.arr[index]; int parent = (index - 1) / 2; if (parent >= 0)//索引未出界 { //插入的值小于父节点 if (temp < heap.arr[parent]) { heap.arr[index] = heap.arr[parent]; heap.arr[parent] = temp; index = parent; } else//大于父节点直接退出 { break; } } else { break; } } } //队尾新插入一个数据 bool InsertHeap(Heap& heap, int values) { if (heap.size == heap.capacity) { cout << "空间不足" << endl; return false; } int index = heap.size; heap.arr[heap.size++] = values; SotingUp(heap, index); return true; } int main(void) { int data[] = { 3,10,55,99,88,77,65,12,15,20 }; // //int len = size(data);//len==10 int len = sizeof(data) / sizeof(data[0]);//len==10 Heap h; for (int i = 0; i < len; i++) { cout << "原始数据:" << data[i] << endl; // } if (initHeap(h, data, len)) { cout << "初始化成功" << endl; for (int i = 0; i < len; i++) { cout << "排序后:" << h.arr[i] << endl; } } cout << "--------------------新插入--------------------------" << endl; InsertHeap(h, 888); for (int i = 0; i < h.size; i++) { cout << h.arr[i] << endl; } cout << "--------------------堆顶出列--------------------------" << endl; int values = 0; int b = 0; while (OutColumn(h, values)) { cout << values << endl; b++; } cout << "B==" << b << endl; return 0; }