优先队列(大顶堆实现)

  1 #include <iostream>
  2 #include<vector>
  3 #include<binaryNode.hpp>
  4 #include<queue>
  5 //自己的头文件放最后防止被依赖
  6 
  7 using namespace std;
  8 template<class T>class heap {
  9 public:
 10     
 11    /* void push(initializer_list<T>datalist) {
 12         for (auto i = datalist.begin(); i != datalist.end(); ++i)
 13                 push(*i);
 14     }*/
 15     int size() { return SIZE; }
 16 
 17     void push(T&& data) {
 18         INDEX++;
 19         SIZE++;
 20         binaryNode<T>* get = this->operator[](INDEX);
 21         binaryNode<T>* block=new binaryNode<T>();
 22         if (!get->lson) {
 23             get->insertls(data);
 24            get->lson->father = get;
 25            block = get->lson;
 26         }
 27         else
 28         {get->insertrs(data);
 29         get->rson->father = get;
 30         block = get->rson;
 31         }
 32         //开始处理结点上升
 33         while (block->father&&(block->data > block->father->data))
 34         {
 35             heap::swap(block, block->father);
 36             block = block->father;
 37             
 38         }
 39     }
 40     
 41     T pop()//开始处理结点下沉删除
 42     {
 43         binaryNode<T>* node = Max;
 44         bool flag;
 45 
 46         while (node->lson&&node->rson) {
 47             if (node->lson->data > node->rson->data)
 48             {
 49                 heap::swap(node->lson, node);
 50                 node = node->lson;
 51                 flag = true;
 52                
 53             }
 54             else
 55             {
 56                 heap::swap(node->rson, node);
 57                 node = node->rson;
 58                 flag = false;
 59           
 60             }
 61         }
 62        if(flag)
 63        {
 64            auto cp = node->data;
 65             node->father->lson->data = NULL;
 66             return cp;
 67        }
 68        else {
 69            auto cp = node->data;
 70            node->father->rson->data = NULL;
 71            return cp;
 72        }
 73        SIZE--;
 74     }
 75 
 76    
 77     binaryNode<T>* Max;
 78     heap() = default;
 79     heap(T data, binaryNode<T>* Max = new binaryNode<T>()) :Max(Max) { Max->data = data; INDEX++; SIZE++; }
 80     
 81     bool empty() {
 82         if (Max == nullptr || Max->data == NULL)return true;
 83         return false;
 84     }
 85 
 86     T top() { return Max->data; }
 87 
 88     
 89 private:
 90 
 91     
 92      binaryNode<T>* operator [](int index) {
 93         binaryNode<T>* node = Max;
 94         if (index == 1)return Max;
 95         const vector<int>&vec = return_vec(index);
 96         for (auto i = vec.crbegin(); i != vec.crend(); ++i)
 97         {
 98             if (*i&&node->rson)
 99                 node = node->rson;
100             else if(!(*i)&&node->lson)
101                 node = node->lson;
102             }
103         return node;
104            
105     }
106 
107     void traverse() {
108         for (auto i = 1, tree = 1; i <= INDEX; ++i) {
109             if (this->operator[](i)->data)
110                 cout << this->operator[](i)->data << ends;
111             if (!((i + 1) % (2 * tree))) {
112                 cout << endl;
113                 tree *= 2;
114             }
115         }
116         cout << endl;
117     }
118 
119     vector<int> return_vec(int index) {
120         vector<int>vec;
121         while (index != 1) {
122             vec.push_back(index % 2);
123             index /= 2;
124         }
125         return vec;
126     }
127     
128     void swap(binaryNode<T>* lson, binaryNode<T>* rson) {
129         T t = lson->data;
130         lson->data = rson->data;
131         rson->data = t;
132     }//不能更换为std::swap因为必须要换值而不是换地址;
133 
134     int INDEX = 0;
135     int SIZE = 0;
136 
137 
138 };
139 
140 int main()
141 {
142     vector<int>vec{ 60,28,45,40,20,25,35,30,10,15 };
143     priority_queue<int>hea;
144     heap<int>h(50);
145     /*h.push({'Q','Z','W','S','X','E','C','D','R','V','F','T','G','B','Y','H','N','U','J','M','I','K','O','L','P'});*/
146     for (auto i = vec.begin(); i != vec.end(); ++i)
147         h.push(std::forward<int>(*i));
148     cout << "自己写的堆" << endl;
149     while(!h.empty())
150   { 
151       cout << h.top() << ends;
152       h.pop();
153 }
154     hea.push(50);
155     for (auto i = vec.cbegin(); i != vec.cend(); i++)
156         hea.push(*i);
157     cout << endl;
158     cout << "STL的堆" << endl;
159     while (!hea.empty())
160     {
161         cout << hea.top() << ends;
162         hea.pop();
163     }
164 
165 
166 }

参考链接https://blog.csdn.net/mawming/article/details/46473755?utm_medium=distribute.pc_relevant.none-task-blog-title-2&spm=1001.2101.3001.4242

原文地址:https://www.cnblogs.com/otakus/p/13904923.html