左式堆

零路徑長:從X到一個不具備兩個兒子的結點的最短路徑的長。ios

性質:算法

任一結點的零路徑長比他的諸兒子結點的零路徑長的最小值多1數據結構

父節點屬性值小於子節點屬性值;this

堆中的任何節點,其左兒子的零路徑長>=右兒子的零路徑長;的二叉樹。spa

下面是左式堆的類型聲明:code

 1 template <typename Comparable>
 2 class LeftistHeap  3 {  4 public:  5  LeftistHeap();  6     LeftistHeap(const LeftistHeap & rhs);  7     ~LeftistHeap();  8 
 9     bool isEmpty() const; 10     const Comparable & findMin() const; 11 
12     void insert(const Comparable & x); 13     void deleteMin(); 14     void deleteMin(Comparable & minItem); 15     void makeEmpty(); 16     void merge(LeftistHeap & rhs); 17 
18     const LeftistHeap & operator=(const LeftistHeap & rhs); 19 
20 private: 21     struct LeftistNode 22  { 23  Comparable element; 24         LeftistNode *left; 25         LeftistNode *right; 26         int npl; 27 
28         LeftistNode(const Comparable & theElement,LeftistNode *lt = NULL,LeftistNode *rt = NULL,int np=0) 29  :element(theElement),left(lt),right(rt),npl(np) 30  { 31  } 32  }; 33     LeftistNode *root; 34     LeftistNode * merge(LeftistHeap *h1,LeftistHeap *h2); 35     LeftistNode * merge1(LeftistHeap *h1,LeftistHeap *h2); 36 
37     void swapChildren(LeftistHeap *t); 38     void reclaimMemory(LeftistHeap *t); 39     LeftistNode * clone(LeftistHeap *t) const; 40 }

合併左式堆的驅動實例:blog

void merge(LeftistHeap & rhs) { if(this == &rhs) return; root = merge(root,rhs.root ); rhs.root = NULL; } LeftistNode * merge(LeftistNode * h1,LeftistNode *h2) { if(h1 == NULL) return h2; if(h2 == NULL) return h1; if(h1->element < h2->element) return mergel(h1,h2); else
        return mergel(h2,h1); }

合併左式堆的實例:遞歸

 1 LeftistNode * mergel(LeftistNode *h1,LeftistNode *h2)  2 {  3     if(h1->left == NULL)  4         h1->left = h2;  5     else
 6  {  7         h1->right = merge(h1->right,h2);  8         if(h1->left->npl < h1->right->npl)  9  swapChildren(h1); 10         h1->npl = h1->right->npl + 1; 11  } 12     return h1; 13 }

左式堆的插入操做:element

1 void insert(const Comparable & x) 2 { 3     root = merge(new LeftListNode(x),root); 4 }

左式堆的deleteMin操做:get

 1  void deleteMin()  2  {  3      if(isEmpty())  4          throw UnderflowException();  5      LeftistNode * oldRoot = root;  6      root = merge(root->left,root->right);  7  delete oldRoot;  8  }  9  void deleteMin(Comparable & minItem) 10  { 11     minItem = findMin(); 12  deleteMin(); 13  }

 

下面是左式堆的應用實例:

 1 #ifndef LeftistHeap_h__  2 #define LeftistHeap_h__
 3 #define    NULL    0
 4 // ******************公共操做*********************  5 // void insert( x ) --> 插入x  6 // deleteMin( minItem ) --> 刪除最小元素  7 // Comparable findMin( ) --> 返回最小元素  8 // bool isEmpty( ) --> 爲空返回true,不然返回false  9 // void makeEmpty( ) --> 清空  10 // void merge( rhs ) --> 合併rhs到本堆中  11 // ******************說明********************************  12 // 代碼根據數據結構與算法分析中的代碼編寫
 13 template <typename Comparable>
 14 class LeftistHeap  15 {  16 public:  17  LeftistHeap():root(NULL)  18  {}  19     LeftistHeap(const LeftistHeap &rhs)  20  {  21         *this = rhs;  22  }  23     ~LeftistHeap()  24  {  25  makeEmtpy();  26  }  27     //插入x元素
 28     void insert(const Comparable &x)  29  {  30         root = merge(new LeftistNode(x), root);  31  };  32     //爲空返回true,不然返回false
 33     bool isEmpty() const
 34  {  35         return root == NULL;  36  }  37     //找到最小元素並返回其值
 38     const Comparable& findMin() const
 39  {  40         if(!isEmpty())  41             return root->element;  42  }  43     //刪除最小元素
 44     void deleteMin()  45  {  46         if(isEmpty())  47             return;  48         LeftistNode* oldroot = root;  49         root = merge(root->left, root->right);  50  delete oldroot;  51  }  52     //刪除最小元素,並將其值賦予minItem
 53     void deleteMin(Comparable &minItem)  54  {  55         minItem = findMin();  56  deleteMin();  57  }  58     //清空
 59     void makeEmtpy()  60  {  61  reclaimMemory(root);  62         root = NULL;  63  }  64     //將另外一個堆合併到本堆中
 65     void merge(LeftistHeap &rhs)  66  {  67         if(this == &rhs)  68             return;  69         root = merge(root, rhs.root);  70         rhs.root = NULL;  71  }  72     const LeftistHeap& operator=(const LeftistHeap &rhs)  73  {  74         if(this != &rhs)  75  {  76  makeEmtpy();  77             root = clone(rhs.root);  78  }  79         return *this;  80  }  81 private:  82     struct LeftistNode  83  {  84  Comparable element;  85         LeftistNode    *left;  86         LeftistNode    *right;  87         int npl;  88         LeftistNode(const Comparable &e, LeftistNode *l = NULL,  89             LeftistNode *r = NULL, int n = 0)  90  :element(e), left(l), right(r), npl(n)  91  {}  92  };  93     LeftistNode *root;  94     //處理非通常狀況,並遞歸調用merge1
 95     LeftistNode* merge(LeftistNode *h1, LeftistNode *h2)  96  {  97         if(h1 == NULL)  98             return h2;  99         if(h2 == NULL) 100             return h1; 101         if(h1->element < h2->element) 102  merge1(h1, h2); 103         else
104  merge1(h2, h1); 105  } 106     //合併兩個節點 107     //h1是具備最小元素的根節點
108     LeftistNode* merge1(LeftistNode *h1, LeftistNode *h2) 109  { 110         if(h1->left == NULL) 111             h1->left = h2; 112         else
113  { 114             h1->right = merge(h1->right, h2); 115             if(h1->left->npl < h1->right->npl) 116  swapChildren(h1); 117             h1->npl = h1->right->npl + 1; 118  } 119         return h1; 120  } 121     void swapChildren(LeftistNode *t) 122  { 123         LeftistNode *tmp = t->left; 124         t->left = t->right; 125         t->right = tmp; 126  } 127     void reclaimMemory(LeftistNode *t) 128  { 129         if(t != NULL) 130  { 131             reclaimMemory(t->left); 132             reclaimMemory(t->right); 133  delete t; 134  } 135  } 136     LeftistNode *clone(LeftistNode *t) const
137  { 138         if(t == NULL) 139             return NULL; 140         else
141             return new LeftistNode(t->element, clone(t->left), clone(t->right)); 142  } 143 }; 144 #endif // LeftistHeap_h__
 1 #include "LeftistHeap.h"
 2 #include <cstdlib>
 3 #include <ctime>
 4 #include <iostream>
 5 using namespace std;  6 int main()  7 {  8     LeftistHeap<int> leftistHeapA, leftistHeapB, leftistHeapC;  9     srand((unsigned)time(0)); 10     for(int i=0; i< 1000; i++) 11  { 12         int t = rand(); 13  leftistHeapA.insert(t); 14  } 15     for(int i=0; i< 1000; i++) 16  { 17         int t = rand(); 18  leftistHeapB.insert(t); 19  } 20  leftistHeapA.merge(leftistHeapB); 21  leftistHeapA.merge(leftistHeapC); 22     int t; 23     while(leftistHeapA.isEmpty() == false) 24  { 25  leftistHeapA.deleteMin(t); 26         cout<<t<<" "; 27  } 28     cout<<endl; 29  getchar(); 30     return 0; 31 }
相關文章
相關標籤/搜索