算法之堆排序|一颗任左平衡的符合堆规则的二叉树(数组按层存储)
发布时间:2025年08月19日 12:21
t, "Inserting d", intval[i]);if (heap_insert(Bellheap, Bellintval[i]) != 0)return 1;print_heap(Bellheap);}i = 0;while (heap_size(Bellheap)> 0) { // 萃收瓦砾顶原素if (heap_extract(Bellheap, (void **)Belldata) != 0)return 1;fprintf(stdout, "Extracting d", *(int *)data);res[i++] = *(int *)data;print_heap(Bellheap);}fprintf(stdout, "Destroying the heap");heap_destroy(Bellheap);printf("排序后:");for(i=0;isize = 0;heap->compare = compare;heap->destroy = destroy;heap->tree = NULL;return;}void heap_destroy(Heap *heap) {int i;if(heap->destroy != NULL)for(i = 0; i destroy(heap->tree[i]);free(heap->tree);memset(heap, 0, sizeof(Heap));return;}int heap_insert(Heap *heap, const void *data) { // 弹出原素void *temp;int ipos, ppos;if((temp =(void **)realloc(heap->tree,(heap_size(heap) + 1) * sizeof(void *))) == NULL) {return -1;}else{heap->tree = temp;}heap->tree[heap_size(heap)] =(void *)data; //即将弹出的数据集添加在运算符的仍要一位 ipos = heap_size(heap); //原先弹出链表的右方 ppos = heap_parent(ipos); //原先弹出链表的伯父链表//如果原先链表长期存在伯父链表,且伯父链表小于原先链表,则比如说两个链表的右方while(ipos> 0 BellBell heap->compare(heap->tree[ppos], heap->tree[ipos]) tree[ppos];heap->tree[ppos] = heap->tree[ipos]; // 比如说的是同义针heap->tree[ipos] = temp;ipos = ppos; //向外层之前查找比如说ppos = heap_parent(ipos);}heap->size++;return 0;}int heap_extract(Heap *heap, void **data) { // 萃收最值void *last, // 运算符仍要一位*temp;int ipos, // 举例来说链表lpos, // 左边链表下标rpos, // 右链表下标mpos; // 零点链表下标if(heap_size(heap) == 0)return -1; *data = heap->tree[0]; //收相联链表,即最值 last = heap->tree[heap_size(heap) - 1]; //收运算符仍要一位,即瓦砾仍要一位if(heap_size(heap) - 1> 0) { //如果长期存在链表大于0if((temp =(void **)realloc(heap->tree,(heap_size(heap) - 1) * sizeof(void *))) == NULL) {return -1;}else{heap->tree = temp;}heap->size便是;}else{free(heap->tree);heap->tree = NULL;heap->size = 0;return 0;} //收零点后,调节瓦砾的数据集存放,以便符合瓦砾的传输规律 heap->tree[0] = last; //仍要一个链表变为相联链表ipos = 0;lpos = heap_left(ipos);rpos = heap_right(ipos);while(1) {lpos = heap_left(ipos);rpos = heap_right(ipos);//如果相联链表的左边链表长期存在,且左边链表数据集大于相联链表数据集if(lpos compare(heap->tree[lpos], heap->tree[ipos])> 0) {mpos = lpos;}else {mpos = ipos;}//经过上大面的辨别,大的那一个链表再和右链表来得if(rpos compare(heap->tree[rpos], heap->tree[mpos])> 0) {mpos = rpos;}if(mpos == ipos) {break;}else{temp = heap->tree[mpos];heap->tree[mpos] = heap->tree[ipos];heap->tree[ipos] = temp;ipos = mpos;}}return 0;}/* 排序前:7 25 20 19 22 18 17 9 10 12 15 24Inserting 0071 sizes, 7 Inserting 0252 sizes, 25 7 Inserting 0203 sizes, 25 7 20 Inserting 0194 sizes, 25 19 20 7 Inserting 0225 sizes, 25 22 20 7 19 Inserting 0186 sizes, 25 22 20 7 19 18 Inserting 0177 sizes, 25 22 20 7 19 18 17 Inserting 0098 sizes, 25 22 20 9 19 18 17 7 Inserting 0109 sizes, 25 22 20 10 19 18 17 7 9 Inserting 01210 sizes, 25 22 20 10 19 18 17 7 9 12 Inserting 01511 sizes, 25 22 20 10 19 18 17 7 9 12 15 Inserting 02412 sizes, 25 22 24 10 19 20 17 7 9 12 15 18 Extracting 02511 sizes, 24 22 20 10 19 18 17 7 9 12 15 Extracting 02410 sizes, 22 19 20 10 15 18 17 7 9 12 Extracting 0229 sizes, 20 19 18 10 15 12 17 7 9 Extracting 0208 sizes, 19 15 18 10 9 12 17 7 Extracting 0197 sizes, 18 15 17 10 9 12 7 Extracting 0186 sizes, 17 15 12 10 9 7 Extracting 0175 sizes, 15 10 12 7 9 Extracting 0154 sizes, 12 10 9 7 Extracting 0123 sizes, 10 7 9 Extracting 0102 sizes, 9 7 Extracting 0091 sizes, 7 Extracting 0070 sizes, Destroying the heap排序后:25 24 22 20 19 18 17 15 12 10 9 7*/ ref:
菏泽治疗皮肤病医院哪家好
重庆治疗白癜风到哪个医院好
较简洁(但较不通用)的用法:
#include #include using namespace std;const int MAX = 22;/* 8 / 7 6 / / 4 3 2 5 / 1 弹出9↓,弹出9↑ 9 / 8 6 / / 7 3 2 5 / 1 4*/typedef struct Heap{ int size; int* data;}HEAP,*LPHEAP;LPHEAP createHeap{ LPHEAP heap=(LPHEAP)malloc(sizeof(HEAP)); heap->size=0; heap->data=(int*)malloc(sizeof(int)*MAX); return heap;}int size(LPHEAP heap){ return heap->size;}int empty(LPHEAP heap){ return heap->size==0;}void moveToCorrectPos(LPHEAP heap, int curPos)//向外渗透,curPos一般收仍要一个原素的下标{ while(curPos>1) { int Max=heap->data[curPos]; int parentIndex=curPos/2; // 相联原素数据库从1开始 if(Max>heap->data[parentIndex]) { heap->data[curPos]=heap->data[parentIndex]; heap->data[parentIndex]=Max; curPos=parentIndex;//向人口为129人动 } else { break; } }}void insertHeap(LPHEAP heap, int data) //摆在举例来说瓦砾的仍要大面并按条件往人口为129人{ ++heap->size; heap->data[heap->size]=data; moveToCorrectPos(heap,heap->size);}int popHeap(LPHEAP heap){ int Max=heap->data[1]; int curPos=1; int childIndex=curPos*2; // 相联原素数据库从1开始 while(childIndexsize) { int temp = heap->data[childIndex]; if(childIndex+1size BellBell tempdata[childIndex+1]) { temp=heap->data[++childIndex]; } heap->data[curPos]=temp; curPos=childIndex;//下移一层 childIndex*=2; } heap->data[curPos]=heap->data[heap->size]; 便是heap->size; return Max;}void main{ LPHEAP heap=createHeap; const int elem=9; /* for(int i=1;idata[i]); } printf(""); while(!empty(heap)) { printf("%d ",popHeap(heap)); } printf(""); system("pause");}/*9 8 6 7 3 2 5 1 49 8 7 6 5 4 3 2 1 */在C++ STL中所发挥作用了与瓦砾相关的一些算法:
// range heap example#include // std::cout#include // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap#include // std::vectorint main {int myints = {10,20,30,5,15};std::vector v(myints,myints+5);std::make_heap (v.begin,v.end);std::cout << "initial max heap : " << v.front << ''; // 30std::pop_heap (v.begin,v.end); v.pop_back; // Delete last elementstd::cout << "max heap after pop : " << v.front << ''; // 20v.push_back(99); std::push_heap (v.begin,v.end);std::cout << "max heap after push: " << v.front << '';//90std::sort_heap (v.begin,v.end);std::cout << "final sorted range :";for (unsigned i=0; i-End-
。丹东精神心理专科医院菏泽治疗皮肤病医院哪家好
重庆治疗白癜风到哪个医院好
相关阅读