-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheap.cpp
147 lines (132 loc) · 3.08 KB
/
heap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cassert>
#include <iostream>
#include "heap.hpp"
#include "types.hpp"
template<typename T, typename Comp>
bool Heap<T,Comp>::isLeaf(const Int& pos) const
{
return pos >= (num_>>1) && (pos < num_);
}
template<typename T, typename Comp>
Int Heap<T,Comp>::leftChild(const Int& pos) const
{
return (pos<<1)+1;
}
template<typename T, typename Comp>
Int Heap<T,Comp>::rightChild(const Int& pos) const
{
return ((pos+1)<<1);
}
template<typename T, typename Comp>
Int Heap<T,Comp>::parent(const Int& pos) const
{
return ((pos-1)>>1);
}
template<typename T, typename Comp>
void Heap<T,Comp>::push_back(const T& elem)
{
Int curr = num_++;
resize(num_-1, num_);
data_[curr] = elem;
while((curr != 0) && (Comp::prior(data_[curr], data_[parent(curr)])))
{
swap(data_, curr, parent(curr));
curr = parent(curr);
}
}
template<typename T, typename Comp>
T Heap<T,Comp>::pop_back()
{
if(is_empty())
assert(1==2);
swap(data_, 0, --num_);
if(num_ != 0)
siftDown(0);
resize(num_+1, num_);
return data_[num_];
}
template<typename T, typename Comp>
T Heap<T,Comp>::pop(const Int& i)
{
Int pos = i;
if(num_ <= pos)
assert(1==2);
if(pos == (num_-1))
num_--;
else
{
swap(data_, pos, --num_);
while((pos != 0) && (Comp::prior(data_[pos], data_[parent(pos)])))
{
swap(data_, pos, parent(pos));
pos = parent(pos);
}
if(num_ != 0)
siftDown(pos);
}
resize(num_+1, num_);
return data_[num_];
}
template<typename T, typename Comp>
bool Heap<T,Comp>::is_empty() const
{
return num_ == 0;
}
//private function
template<typename T, typename Comp>
void Heap<T,Comp>::siftDown(const Int& i)
{
Int pos = i;
while(!isLeaf(pos))
{
Int lc = leftChild(pos);
Int rc = rightChild(pos);
if((rc < num_) && Comp::prior(data_[rc], data_[lc]))
lc = rc;
if((Comp::prior(data_[pos], data_[lc])))
break;
swap(data_, pos, lc);
pos = lc;
}
}
template<typename T, typename Comp>
void Heap<T,Comp>::heapify()
{
for(Int i = (num_>>1)-1; i >= 0; --i)
siftDown(i);
}
//resize the buffer
template<typename T, typename Comp>
void Heap<T,Comp>::resize(const Int& old_size, const Int& new_size)
{
if((new_size > cap_) || (new_size < (cap_>>2)))
{
cap_ = ((new_size > cap_) ? (new_size<<1) : (cap_>>1));
T* tmp = new T [cap_];
for(Int i = 0; i < old_size; ++i)
tmp[i] = data_[i];
delete [] data_;
data_ = tmp;
}
}
template<typename T, typename Comp>
void Heap<T,Comp>::push_back(const Heap<T,Comp>& heap)
{
Int size = heap.size();
for(Int i = 0; i < size; ++i)
{
const T val = heap.at(i);
push_back(val);
}
}
template<typename T, typename Comp>
void Heap<T,Comp>::push_back(T* data, const Int& size)
{
for(Int i = 0; i < size; ++i)
{
T val = data[i];
push_back(val);
}
}
template class Heap<Int,Min>;
template class Heap<EdgeTuple,EdgeTupleMin>;