如何實現最小堆,這是我按照書上寫的類以及定義的函式,那位大佬可以給我講一下如何在main函式中實現嗎?(本人剛開始學習資料結構,如果有很低階的錯誤請擔待)
類:#pragma once
#include
using namespace std;
class minheap
;typedef struct minheap_in minheap;
int size(minheap *xheap);
int min(minheap *xheap);
void initminheap(minheap *xheap,int maxheapsize);
minheap *insert(minheap_in *xheap,int x);
minheap *deletemin(minheap_in *xheap,int *x);
void makeheap(minheap *xheap,int a,int size,int arraysize);
void heapsort(int a,int n);
};函式定義:
#include 「minheap.h」
int minheap::size(minheap *xheap)//獲取小堆根的大小
int minheap::min(minheap *xheap)//獲取根元素
void minheap::initminheap(minheap *xheap, int maxheapsize)//初始化小堆根
minheap::minheap *minheap::insert(minheap_in *xheap, int x)//在小根堆中插入新元素
i = xheap->heapsize++;//將根節點的左兒子賦值給i
while (i!=0&&xheap[(i-1)/2])//x小於其父節點(根節點)時的操作
xheap->heap[i] = x;//插入新元素
return xheap;
}minheap::minheap *minheap::deletemin(minheap_in *xheap, int *x)//每次重建小根堆刪除第0個資料(根節點)
else
return xheap;}}
void minheap::makeheap(minheap xheap, int a, int size, int arraysize)//構造小根堆
if (y >= xheap->heap[ic])
xheap->heap[(ic - 1) / 2] = xheap->heap[ic];
ic = ic * 2 + 1;
}xheap->heap[(ic - 1) / 2] = y;}}
void minheap::heapsort(int a, int n)
}main,是我自己寫的,不會寫,是錯誤的。
#include
#include"minheap.h"
using namespace std;
int main()
;heap_run.initminheap(&xheap,6);
for (i = 0; i < 10; i++)
heap_run.makeheap(&xheap, minheaparrary, 10, 10);
for (i = 0; i < 6; i++)
system(「pause」);
return 0;
}希望看到的大佬幫我解決以下,謝謝!!
最小堆的實現
這邊實現的是最小堆,最小堆是這樣定義的,首先堆是一棵完全二叉樹,每乙個節點都有乙個權值,滿足的條件是,父節點的權值總是大於等於子節點的權值 include using namespace std 最小堆類的定義如下 template class minheap template void minhe...
C 最小堆實現
mini heap.h pragma once include template class mini heap node heap 最小堆 uint32 t max size 最大儲存數 uint32 t size 儲存數 擴容 void expansion 刪除指定下標節點 void del n...
priority queue的實現最小堆的用法
priority queue呼叫 stl裡面的make heap pop heap push heap 演算法實現,也算是堆的另外一種形式。先寫乙個用 stl 裡面堆演算法實現的與真正的stl裡面的 priority queue用法相似的priority queue,以加深對 priority qu...