0.1) 本文文字描述部分转自 数据结构与算法分析, 旨在理解 优先队列——左式堆 的基础知识;
0.2) 本文核心思路均为原创, 源代码部分借鉴 数据结构与算法分析 ;
0.3) for original source code, please visit https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter6/p145_leftist_heapnode
1)相关定义git
1.1)零路径长度定义: 到没有两个儿子的节点最短距离, 即零路径长Npl 定义为 从 X 到一个没有两个儿子的 节点的最短路径的长;也即, 非叶子节点到叶子节点的最少边数,其中NULL的零路径长为-1, 叶子节点的零路径长为0;(干货——零路径长的定义—— 非叶子节点到叶子节点的最少边数,很是重要,由于左式堆的定义是基于零路径长的定义的) github
1.2)左式堆定义:一棵具备堆序性质的二叉树 + 零路径长:左儿子 ≧ 右儿子 + 父节点 = min{儿子} +1;(干货——左式堆的定义是创建在具备堆序性的二叉树上,而不是二叉堆上)算法
2)merge操做原则: 根值大的堆与根值小的堆的右子堆合并;(干货——merge操做原则)
3)merge操做存在的三种状况(设堆H1的根值小于H2)数据结构
左式堆合并原则:大根堆H2与小根堆H1的右子堆合并 (干货——左式堆合并原则)
具体分三种状况(设堆H1的根值小于H2) post
Conclusion) 如今才知道,左式堆的merge操做实际上是一个递归的过程, 看以下解析; (干货——这是最后解析merge操做啦)
spa
Attention once again)code
source code at a glance )blog
#include "leftist_heap.h" // swap the left and the right in priority queue. void swap(PriorityQueue h1) { PriorityQueue temp; temp = h1->left; h1->left = h1->right; h1->right = temp; } // analog print directories and files name in the BinaryTree, which involves postorder traversal. void printPreorder(int depth, TreeNode root) { int i; if(root) { for(i = 0; i < depth; i++) printf(" "); printf("%d\n", root->value); printPreorder(depth + 1, root->left); // Attention: there's difference between traversing binary tree and common tree. printPreorder(depth + 1, root->right); } else { for(i = 0; i < depth; i++) printf(" "); printf("NULL\n"); } } // insert an element with value into the priority queue. PriorityQueue insert(ElementType value, PriorityQueue pq) { TreeNode node; node = (TreeNode)malloc(sizeof(struct TreeNode)); if(!node) { Error("failed inserting, for out of space !"); return pq; } node->left = NULL; node->right = NULL; node->nullPathLen = 0; node->value = value; if(pq == NULL) // means that just only creating a node with value. { return node; } else { return merge(node, pq); } } // return the minimal between a and b. int minimal(int a, int b) { return a > b ? b : a; } // merge the priority queue h1 and h2. PriorityQueue merge(PriorityQueue h1, PriorityQueue h2) { if(h1 == NULL) { return h2; } else if(h2 == NULL) { return h1; } if(h1->value > h2->value) { return innerMerge(h2, h1); } else { return innerMerge(h1, h2); } } // merge the priority queue h1 and h2. PriorityQueue innerMerge(PriorityQueue h1, PriorityQueue h2) { if(h1->left == NULL) { h1->left = h2; } else { h1->right = merge(h1->right, h2); } // update the null path length if(h1->right == NULL) { h1->nullPathLen = 0; } else { h1->nullPathLen = minimal(h1->left->nullPathLen, h1->right->nullPathLen) + 1; // exchange the left and the right if(h1->left->nullPathLen < h1->right->nullPathLen) { swap(h1); } } return h1; } // delete the minimal element in the priority queue. PriorityQueue deleteMin(PriorityQueue h1) { PriorityQueue left; PriorityQueue right; if(!h1) { Error("failed deleteMin, for the root doesn't point to any position!"); return NULL; } left = h1->left; right = h1->right; free(h1); return merge(left, right); } int main() { PriorityQueue h1; PriorityQueue h2; int data[] = {21, 10, 23, 14, 3, 26, 17, 8}; int data2[] = {18, 12, 33, 24, 6, 37, 7, 18}; int i; h1 = insert(data[0], NULL); for(i=1; i<8; i++) { h1 = insert(data[i], h1); } printf("\n=== after the leftist heap h1 is merged===\n"); printPreorder(1, h1); h2 = insert(data2[0], NULL); for(i=1; i<8; i++) { h2 = insert(data2[i], h2); } printf("\n=== after the leftist heap h2 is merged===\n"); printPreorder(1, h2); h1 = merge(h1, h2); printf("\n=== after both h1 and h2 are merged===\n"); printPreorder(1, h1); h1 = deleteMin(h1); printf("\n=== after executing deleteMin operation ===\n"); printPreorder(1, h1); return 0; }
printing results are as follows) 递归
Attention for analog between results and the 2 images above.