优先队列(大顶堆实现)

  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     constexpr int size() const  { return SIZE; }
 16 
 17    inline 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         {
 29             get->insertrs(data);
 30             get->rson->father = get;
 31             block = get->rson;
 32         }
 33         //开始处理结点上升
 34         while (block->father && (block->data > block->father->data))
 35         {
 36             heap::swap(block, block->father);
 37             block = block->father;
 38         }
 39     }
 40 
 41    inline constexpr T pop()//开始处理结点下沉删除
 42     {
 43         binaryNode<T>* node = Max;
 44         T dat = node->data;
 45         binaryNode<T>*tail=this->operator[](INDEX--);
 46         if (node==tail) { SIZE--; return dat; }//当最后一个元素时直接退出。
 47         heap::swap(node, tail);//最顶端的节点和最后的节点之间交换
 48         if (tail->father->lson && tail->father->rson) { tail->father->rson = NULL;}
 49         else { tail->father->lson = NULL; }
 50         delete tail;
 51         //这里都是处理最后交换后的节点元素
 52         while (node->lson && node->rson) {
 53             if (node->data < node->lson->data || node->data < node->rson->data)
 54             {
 55                 if (node->lson->data > node->rson->data)
 56                 {
 57                     heap::swap(node->lson, node);
 58                     node = node->lson;
 59                 }
 60                 else
 61                 {
 62                     heap::swap(node->rson, node);
 63                     node = node->rson;
 64                 }
 65             }//节点下沉
 66             else break;//如果当前节点比子节点的元素都大则停止
 67         }
 68         SIZE--;
 69         return dat;
 70 
 71     }
 72 
 73 
 74     binaryNode<T>* Max;
 75     heap() = default;
 76     heap(T data, binaryNode<T>* Max = new binaryNode<T>()) :Max(Max) { Max->data = data; INDEX++; SIZE++; }
 77     //不变式
 78     constexpr bool empty() const {
 79         if (SIZE == 0 || Max == nullptr || Max->data == NULL)return true;
 80 
 81         return false;
 82     }
 83 
 84     constexpr T top() const { return Max->data; }
 85 
 86     void traverse() {
 87         for (auto i = 1, tree = 1; i <= INDEX; ++i) {
 88             if (this->operator[](i)->data)
 89                 cout << this->operator[](i)->data << ends;
 90             if (!((i + 1) % (2 * tree))) {
 91                 cout << endl;
 92                 tree *= 2;
 93             }
 94         }
 95         cout << endl;
 96     }
 97 
 98     binaryNode<T>* operator [](int index) {
 99         binaryNode<T>* node = Max;
100         if (index == 1)return Max;
101         const vector<int>& vec = return_vec(index);
102         for (auto i = vec.crbegin(); i != vec.crend(); ++i)
103         {
104             if (*i && node->rson)
105                 node = node->rson;
106             else if (!(*i) && node->lson)
107                 node = node->lson;
108         }
109         return node;
110 
111     }
112 private:
113 
114     vector<int> return_vec(int index) {
115         vector<int>vec;
116         while (index != 1) {
117             vec.push_back(index % 2);
118             index /= 2;
119         }
120         return vec;
121     }
122 
123     void swap(binaryNode<T>* lson, binaryNode<T>* rson) {
124         T t = lson->data;
125         lson->data = rson->data;
126         rson->data = t;
127     }//不能更换为std::swap因为必须要换值而不是换地址;
128 
129     int INDEX = 0;
130     int SIZE = 0;
131 
132 
133 };
134 
135 int main()
136 {
137     vector<int>vec{ 60,28,45,40,20,25,35,30,10,15 };
138     priority_queue<int>hea;
139     heap<int>h(50);
140     /*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'});*/
141     for (auto i = vec.begin(); i != vec.end(); ++i)
142         h.push(std::forward<int>(*i));
143     h.traverse();
144    
145     cout << "自己写的堆" << endl;
146     while (!h.empty())
147     {
148         cout << "data: "<<h.top() << endl;
149         h.pop();
150 
151     }
152     hea.push(50);
153     for (auto i = vec.cbegin(); i != vec.cend(); i++)
154         hea.push(*i);
155     cout << endl;
156     cout << "STL的堆" << endl;
157     while (!hea.empty())
158     {
159         cout << hea.top() << ends;
160         hea.pop();
161     }
162 
163 
164 }

 

posted @ 2020-10-31 00:07  世界を変える御宅族  阅读(418)  评论(0编辑  收藏  举报