日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

优先队列与堆

發布時間:2024/9/5 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 优先队列与堆 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建: 2018/05/18

完成: 2018/05/30?

?

優先隊列
?優先隊列

?● 插入新元素不變, 和隊列一樣

?● 取出的是最大/最小元素

?

?運算

?規定元素都要帶key

?主要元素

?● Insert(key, data) 插入元素(key: data)

?● DeleteMin/DeleteMax: 刪除并返回最大/最小值

?● GetMinimum/GetMaximum: 不刪除并獲取最大/最小值?

?輔助運算

?● k-Smallest/k-Largest 獲取第k大/小的元素

?● size: 獲取元素數

?● Heap Sort: 根據優先度(k)來排序

?應用

?● 壓縮數據: 哈夫曼編碼(人名Huffman)

?●?迪杰斯特拉算法(最短路徑算法)

?●?最小生成樹算法

?● 選取: 選取第k小的元素

??
??
??
?堆

?● 所有節點值>=/<=子節點值的樹

堆是完全二叉樹

?● 葉子在最下面兩層, 最下層葉子都在左邊

?● 所有有分支的節點都有2個分支

?種類

?● 最大堆: 根最大

?● 最小堆: 根最小

?實現二叉堆

?

//------------------------------------------------ // 堆的型聲明 //------------------------------------------------ struct Heap {int *array; // 數據int count; // 收容的數據數量int capicity; // 可收容的數據容量int heap_type; // 堆的種類: 最大堆, 最小堆 }; //------------------------------------------------ // 函數聲明 //------------------------------------------------ struct Heap *createHeap(int capicity, int heap_type); // 創建堆 int parent(struct Heap *h, int i); // 堆的父節點 int leftChild(struct Heap *h, int i); // 左子節點 int rightChild(struct Heap *h, int i); // 右子節點 int getMaximun(struct Heap *h); // 獲取最大節點 void percolateDown(struct Heap *h, int i); // 堆化 int deleteMax(struct Heap *h); // 刪除元素 void insertIntoHeap(struct Heap *h, int data); // 插入新元素 void insertIntoHeapV2(struct Heap *h, int data); // 優化版 插入新元素 void resizeHeap(struct Heap *h); // 已滿的容量變為2倍 void destroyHeap(struct Heap *h); // 刪除堆 void buildHeap(struct Heap *h, int A[], int n); // 通過數組構建堆 // 應用: 堆排序 void heapSort(int A[], int n); // 堆排序 //------------------------------------------------ // 函數實現 //------------------------------------------------ struct Heap *createHeap(int capicity, int heap_type) { // 創建堆struct Heap *h = (struct Heap *)malloc(sizeof(struct Heap));if (h==NULL) {printf("memory error\n");return NULL;}h->heap_type = heap_type;h->capicity = capicity;h->count = 0;h->array = (int *)malloc(sizeof(int)*capicity);if (h->array == NULL) {printf("memory error");return NULL;}return h; }// 注意: 忘記了數量關系就自己算一遍 // l(o) = (l(A)-1)/2 = (l(b)-2)/2 // l(a) = 2*l(o) + 1 // l(b) = 2*(l) int parent(struct Heap *h, int i) { // 堆的父節點if (i<0 || i>h->count) {return -1;}return (i-1)/2; }int leftChild(struct Heap *h, int i) { // 左子節點int left = 2*i+1;if (left>h->count) {return -1;}return left; } int rightChild(struct Heap *h, int i) { // 右子節點int right = 2*i+2;if (right>h->count) {return -1;}return right; }int getMaximun(struct Heap *h) { // 獲取最大節點if (h->count == 0) {return -1;}return h->array[0]; }void percolateDown(struct Heap *h, int i) { // 對位置i處的元素進行堆化// 注意: 除i外其他所有地方都滿足堆int l, r, max, temp;l = leftChild(h, i);r = rightChild(h, i);if (l == -1 && r == -1) { // 這個可以不要: 不存在子節點則下面的max = ireturn;}if (l != -1 && h->array[l] > h->array[i]) { // 左子節點存在且大于當前節點max = l;} else {max = i;}if (r != -1 && h->array[r] > h->array[max]) { // 右子節點存在且大于 [當前節點, 左子節點].maxmax = r;}if (max != i) {temp = h->array[i];h->array[i] = h->array[max];h->array[max] = temp;percolateDown(h, max);} else {return;}}int deleteMax(struct Heap *h) { // 刪除元素int data;if (h->count == 0) { // 堆里沒有數據return -1;}data = h->array[0];h->array[0] = h->array[h->count-1];h->count--;percolateDown(h, 0);return data; }void insertIntoHeap(struct Heap *h, int data) { // 插入新元素int parentNodeIndex, i;if (h->count >= h->capicity-1) {resizeHeap(h); // 已滿則容量變為2倍 }h->count += 1;i = h->count-1;h->array[h->count+1] = data;parentNodeIndex = parent(h, i);while (parentNodeIndex >= 0 && h->array[parentNodeIndex] < h->array[i]) {int temp = h->array[i];h->array[i] = h->array[parentNodeIndex];h->array[parentNodeIndex] = temp;i = parentNodeIndex;parentNodeIndex = parent(h, i);} }void insertIntoHeapV2(struct Heap *h, int data) { // 優化版 插入新元素int i;if (h->count >= h->capicity-1) {resizeHeap(h); // 已滿則容量變為2倍 }h->count += 1;i = h->count-1;while (i>=1 && data > h->array[(i-1)/2]) { // 優化邏輯h->array[i] = h->array[(i-1)/2];i = (i-1)/2;}h->array[i] = data; }void resizeHeap(struct Heap *h) { // 已滿的容量變為2倍int *old_array = h->array, i;h->array = (int *)malloc(sizeof(int) * (h->capicity*2));if (h->array == NULL) {printf("memory error\n");return;}for (i=0; i<h->capicity; i++) {h->array[i] = old_array[i];}h->capicity *= 2;free(old_array); }void destroyHeap(struct Heap *h) { // 刪除堆if (h==NULL) {return;}if (h->array == NULL) {free(h);}free(h->array);free(h);h = NULL; }void buildHeap(struct Heap *h, int A[], int n) { // 通過數組構建堆int i;if (h==NULL) {return;}while (n > h->capicity) {resizeHeap(h);}for (i = 0; i < n; i++) {h->count++;h->array[i] = A[i];}for (i = (n-1)/2; i >= 0; i--) {percolateDown(h, i);} } // 應用: 堆排序 void heapSort(int A[], int n) { // 堆排序int i;struct Heap *h = createHeap(n, 1);buildHeap(h, A, n);for (i = 0; i < n; i++) {A[i] = deleteMax(h);} }

?

?

??
??
??

轉載于:https://www.cnblogs.com/lancgg/p/9054221.html

總結

以上是生活随笔為你收集整理的优先队列与堆的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。