From f59a87660994eebc7eb43517d9e4a25f15377d6d Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Thu, 1 Aug 2019 02:42:53 +0800 Subject: [PATCH] =?UTF-8?q?TODO:=20=E5=AE=8C=E6=88=90Pop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/heap.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/heap.h b/src/heap.h index 1827a7b..ae95270 100644 --- a/src/heap.h +++ b/src/heap.h @@ -1,5 +1,6 @@ #include +#include using namespace std; @@ -53,6 +54,54 @@ public: T pop() { + if(this->data.size() == 0) { + throw "size 错误"; + } + T result = this->data[0]; + T pvalue = this->data.pop_back(); + unsigned long cur_idx = 0; + unsigned long size = this->data.size(); + unsigned long c1, c2, cidx; + + for(;cur_idx < size;) { + cidx = cur_idx << 1; + + c2 = cidx + 2; + if(c2 < size) { + T cvalue2 = this->data[c2]; + + c1 = cidx + 1; + T cvalue1 = this->data[c1]; + + if (this->compare(cvalue1, cvalue2) >= 0 ){ + cidx = c1; + cvalue = cvalue1; + } else { + cidx = c2; + cvalue = cvalue2; + } + } else { + + c1 = cidx + 1 + if c1 < h.size { + cvalue1 = h.elements[c1] + cidx = c1 + cvalue = cvalue1 + } else { + break + } + + } + + if h.Compare(cvalue, downvalue) > 0 { + h.elements[curidx] = cvalue + curidx = cidx + } else { + break + } + } + this->data[cur_idx] = pvalue; + return result; } }; \ No newline at end of file