左倾堆C++实现

  • Post author:
  • Post category:其他


  1 #include <iostream>
  2 #include <vector>
  3 #include <queue>
  4 using namespace std;
  5 template <typename T>
  6 class LeftlistNode
  7 {
  8 public:
  9     T key;
 10     int npl;
 11     LeftlistNode* left;
 12     LeftlistNode* right;
 13 
 14     LeftlistNode(T& value, LeftlistNode* l = NULL, LeftlistNode* r = NULL, int n = 0):
 15         key(value), left(l), right(r), npl(n){}
 16 };
 17 
 18 template <typename T>
 19 class LeftlistHeap{
 20 private:
 21     LeftlistNode<T>* mRoot;
 22 public:
 23     LeftlistHeap(){}
 24     LeftlistHeap(vector<T>& items){
 25         mRoot = buildHeap(items);
 26     }
 27     ~LeftlistHeap(){
 28         destory(mRoot);
 29     }
 30     void merge(LeftlistHeap<T>* other){
 31         if(this == other)
 32             return;
 33         mRoot = _merge(mRoot, other->mRoot);
 34     }
 35     void insert(T& key){
 36         mRoot = _merge(mRoot, new LeftlistNode<T>(key));
 37     }
 38     void remove(){
 39         LeftlistNode<T> old = mRoot;
 40         mRoot = _merge(mRoot->Left, mRoot->right);
 41         delete old;
 42     }
 43     T get_min(){
 44         return mRoot->key;
 45     }
 46     
 47     void destory(LeftlistNode<T>* & mRoot){
 48         if(mRoot == NULL)
 49             return;
 50         if(mRoot->left != NULL)
 51             destory(mRoot->left);
 52         if(mRoot->right != NULL)
 53             destory(mRoot->right);
 54         delete mRoot;
 55     }
 56     
 57 private:
 58     LeftlistNode<T>* _merge1(LeftlistNode<T>* x, LeftlistNode<T>* y){
 59         if(x == NULL)
 60             return y;
 61         if(y == NULL)
 62             return x;
 63         if(x->key < y->key)
 64             _merge2(x, y);
 65         else
 66             _merge2(y, x);
 67     }
 68     LeftlistNode<T>* _merge2(LeftlistNode<T>* x, LeftlistNode<T>* y){
 69         if(x->left == NULL)
 70             x->left = y;
 71         else{
 72             x->right = _merge1(x->right, y);
 73             if(x->left->npl < x->right->npl){
 74                 LeftlistNode<T>* temp = x->left;
 75                 x->left = x->right;
 76                 x->right = temp;
 77             }
 78             x->npl = x->right->npl + 1;
 79         }
 80         return x;
 81     }
 82 
 83     LeftlistNode<T>* _merge(LeftlistNode<T>* x, LeftlistNode<T>* y){
 84         if(x == NULL && y == NULL)
 85             return NULL;
 86         else if(x == NULL && y != NULL)
 87             return y;
 88         else if(y == NULL && x != NULL)
 89             return x;
 90         else{
 91             if(x->key > y->key){
 92                 LeftlistNode<T>* tmp = x;
 93                 x = y;
 94                 y = tmp;
 95             }
 96             if(x->left == NULL)
 97                 x->left = y;
 98             else{
 99                 x->right = _merge(x->right, y);
100                 if(x->left->npl < x->right->npl){
101                     LeftlistNode<T>* tmp = x->left;
102                     x->left = x->right;
103                     x->right = tmp;
104                 }
105                 x->npl = x->right->npl + 1;
106             }
107         }
108         return x;
109     }
110     LeftlistNode<T>* buildHeap(vector<T>& items){
111         queue<LeftlistNode<T>*> tmp_queue;
112         for(int i = 0; i < items.size(); ++i)
113             tmp_queue.push(new LeftlistNode<T>(items[i]));
114         while(tmp_queue.size() > 1){
115             LeftlistNode<T>* t1 = tmp_queue.front();
116             tmp_queue.pop();
117             LeftlistNode<T>* t2 = tmp_queue.front();
118             tmp_queue.pop();
119             tmp_queue.push(_merge1(t1, t2));
120         }
121         return tmp_queue.front();
122     }
123 
124 };
125 
126 int main(){
127     int a[]= {100,40,24,30,36,20,12,16};
128     vector<int> lt(a, a + 7);
129     LeftlistHeap<int> yj(lt);
130     cout << yj.get_min() << endl;
131     return 0;
132 }

转载于:https://www.cnblogs.com/kHrystal/p/9490924.html