10 #ifndef _BINARY_HEAP_H
11 #define _BINARY_HEAP_H
13 #include <boost/concept_check.hpp>
29 template<
typename elt_t,
typename key_t>
31 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<elt_t>));
32 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<key_t>));
43 template<
typename InputIter>
44 explicit binary_heap(InputIter elt_first, InputIter elt_last, InputIter key_first);
47 void insert(
const elt_t& elt,
const key_t& key);
55 void replace(
const elt_t& elt,
const key_t& key);
58 int size()
const override {
return __sz; }
59 bool empty()
const override {
return __sz == 0; }
62 void update_key(
const elt_t& elt,
const key_t& key);
71 typename std::map<elt_t, int>::iterator elt_it;
72 bool operator<(
const node_t& other)
const {
return key < other.key; }
80 std::map<elt_t, int> nodeof;
83 std::vector<node_t> heaparr;
86 typename std::map<elt_t, int>::iterator make_entry(
const elt_t& elt) {
87 if (nodeof.find(elt) == nodeof.end())
89 return nodeof.find(elt);
91 void update_entry(
int idx) { (heaparr[idx].elt_it)->second = idx; }
94 inline int __parent(
int node) {
return (node - 1) / 2; }
95 inline int __left_child(
int node) {
return node * 2 + 1; }
96 inline int __right_child(
int node) {
return node * 2 + 2; }
97 void __sift_up(
int node);
98 void __sift_down(
int node);
101 template<
typename elt_t,
typename key_t>
106 template<
typename elt_t,
typename key_t>
107 template<
typename InputIter>
110 InputIter key_first) {
112 while (elt_first != elt_last) {
113 auto loc = make_entry(*elt_first);
114 heaparr[__sz++] = node_t {*key_first, loc};
119 for (
int i = __sz / 2; i >= 0; i--)
123 template<
typename elt_t,
typename key_t>
126 auto loc = make_entry(elt);
127 heaparr.push_back(node_t {key, loc});
133 template<
typename elt_t,
typename key_t>
135 return (heaparr[0].elt_it)->first;
139 template<
typename elt_t,
typename key_t>
141 return heaparr[0].key;
144 template<
typename elt_t,
typename key_t>
146 nodeof.erase(heaparr[0].elt_it);
147 std::swap(heaparr[0], heaparr[__sz - 1]);
152 template<
typename elt_t,
typename key_t>
156 throw std::underflow_error(
"Empty heap");
157 auto loc = make_entry(elt);
158 nodeof.erase(heaparr[0].elt_it);
159 heaparr[0] = node_t {key, loc};
163 template<
typename elt_t,
typename key_t>
166 if (nodeof.find(elt) == nodeof.end())
167 throw std::domain_error(
"The element doesn't exist");
168 int index_in_heap = nodeof[elt];
169 heaparr[index_in_heap] = node_t {key, heaparr[index_in_heap].elt_it};
170 __sift_up(index_in_heap);
171 __sift_down(index_in_heap);
174 template<
typename elt_t,
typename key_t>
180 node_t temp = heaparr[node];
181 while (node != 0 && temp < heaparr[__parent(node)]) {
182 heaparr[node] = heaparr[__parent(node)];
184 node = __parent(node);
186 heaparr[node] = temp;
190 template<
typename elt_t,
typename key_t>
191 void binary_heap<elt_t, key_t>::__sift_down(
int node) {
193 int left_child = __left_child(node);
194 int right_child = __right_child(node);
196 if (left_child < __sz && heaparr[left_child] < heaparr[min_node])
197 min_node = left_child;
198 if (right_child < __sz && heaparr[right_child] < heaparr[min_node])
199 min_node = right_child;
201 if (min_node != node) {
202 std::swap(heaparr[node], heaparr[min_node]);
204 update_entry(min_node);
205 __sift_down(min_node);
212 template<
typename elt_t>
235 #endif // _BINARY_HEAP_H
key_t key_type
Definition: binary_heap.h:37
void replace(const elt_t &elt, const key_t &key)
Definition: binary_heap.h:153
void delete_min()
Definition: binary_heap.h:224
bool empty() const override
Definition: binary_heap.h:59
void insert(const elt_t &elt)
Definition: binary_heap.h:218
Definition: binary_heap.h:213
binary_heap()
Definition: binary_heap.h:102
const elt_t & get_min_elt() const
Definition: binary_heap.h:134
const elt_t & get_min() const
Definition: binary_heap.h:221
void update_key(const elt_t &elt, const key_t &key)
Definition: binary_heap.h:164
Definition: binary_heap.h:30
void delete_min()
Definition: binary_heap.h:145
void replace(const elt_t &elt)
Definition: binary_heap.h:225
const key_t & get_min_key() const
Definition: binary_heap.h:140
int size() const
Definition: binary_heap.h:228
elt_t elt_type
Definition: binary_heap.h:36
int size() const override
Definition: binary_heap.h:58
void insert(const elt_t &elt, const key_t &key)
Definition: binary_heap.h:124
bool empty() const
Definition: binary_heap.h:229