Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
RMHeap.h
Go to the documentation of this file.
1
32#pragma once
33
35
36#include <functional>
37#include <random>
38
39namespace ogdf {
40
41
43template<typename T>
44struct RMHeapNode {
45 template<typename, typename>
46 friend class RMHeap;
47
48protected:
50
54
58};
59
61
74template<typename T, typename C = std::less<T>>
75class RMHeap : public HeapBase<RMHeap<T, C>, RMHeapNode<T>, T, C> {
77
78public:
85 explicit RMHeap(const C& cmp = C(), int initialSize = -1);
86
93 virtual ~RMHeap();
94
96 const T& top() const override;
97
104 RMHeapNode<T>* push(const T& value) override;
105
111 void pop() override;
112
122 void decrease(RMHeapNode<T>* heapNode, const T& value) override;
123
131 void merge(RMHeap<T, C>& other) override;
132
139 const T& value(RMHeapNode<T>* heapNode) const override { return heapNode->value; }
140
141private:
142 std::default_random_engine m_rand;
144
149
151 static void release(RMHeapNode<T>* heapNode);
152};
153
154template<typename T, typename C>
156 : base_type(cmp), m_rand((std::random_device())()), m_root(nullptr) { }
157
158template<typename T, typename C>
160 release(m_root);
161}
162
163template<typename T, typename C>
164const T& RMHeap<T, C>::top() const {
165 return m_root->value;
166}
167
168template<typename T, typename C>
171 m_root = merge(m_root, heapNode);
172
173 return heapNode;
174}
175
176template<typename T, typename C>
178 RMHeapNode<T>* root = m_root;
179 m_root = merge(m_root->left, m_root->right);
180 if (m_root != nullptr) {
181 m_root->parent = nullptr;
182 }
183 delete root;
184}
185
186template<typename T, typename C>
188 heapNode->value = value;
189 if (heapNode == m_root) {
190 return;
191 }
192
193 remove(heapNode);
194 heapNode->left = nullptr;
195 heapNode->right = nullptr;
196 heapNode->parent = nullptr;
197
198 m_root = merge(m_root, heapNode);
199}
200
201template<typename T, typename C>
203 m_root = merge(m_root, other.m_root);
204 other.m_root = nullptr;
205}
206
207template<typename T, typename C>
209 if (a == nullptr) {
210 return b;
211 }
212 if (b == nullptr) {
213 return a;
214 }
215
216 if (this->comparator()(a->value, b->value)) {
217 if (m_rand() % 2 == 0) {
218 a->left = merge(a->left, b);
219 if (a->left != nullptr) {
220 a->left->parent = a;
221 }
222 } else {
223 a->right = merge(a->right, b);
224 if (a->right != nullptr) {
225 a->right->parent = a;
226 }
227 }
228 return a;
229 } else {
230 if (m_rand() % 2 == 0) {
231 b->left = merge(b->left, a);
232 if (b->left != nullptr) {
233 b->left->parent = b;
234 }
235 } else {
236 b->right = merge(b->right, a);
237 if (b->right != nullptr) {
238 b->right->parent = b;
239 }
240 }
241 return b;
242 }
243}
244
245template<typename T, typename C>
247 RMHeapNode<T>* merged = merge(heapNode->left, heapNode->right);
248 if (heapNode == heapNode->parent->left) {
249 heapNode->parent->left = merged;
250 } else {
251 heapNode->parent->right = merged;
252 }
253 if (merged != nullptr) {
254 merged->parent = heapNode->parent;
255 }
256}
257
258template<typename T, typename C>
260 if (heapNode == nullptr) {
261 return;
262 }
263
264 release(heapNode->left);
265 release(heapNode->right);
266 delete heapNode;
267}
268
269}
Interface for heap implementations.
Common interface for all heap classes.
Definition HeapBase.h:50
virtual const T & value(const Handle handle) const =0
Returns the value of that handle.
Randomized meldable heap implementation.
Definition RMHeap.h:75
virtual ~RMHeap()
Destructs the heap.
Definition RMHeap.h:159
RMHeapNode< T > * push(const T &value) override
Inserts a new node with given value into a heap.
Definition RMHeap.h:169
static void release(RMHeapNode< T > *heapNode)
Recursively releases memory occupied by heap pointed by heapNode.
Definition RMHeap.h:259
void merge(RMHeap< T, C > &other) override
Merges in values of other heap.
Definition RMHeap.h:202
RMHeap(const C &cmp=C(), int initialSize=-1)
Creates empty randomized meldable heap.
Definition RMHeap.h:155
void pop() override
Removes the top element from the heap.
Definition RMHeap.h:177
RMHeapNode< T > * m_root
Root node of the heap.
Definition RMHeap.h:143
const T & top() const override
Returns reference to the top element in the heap.
Definition RMHeap.h:164
const T & value(RMHeapNode< T > *heapNode) const override
Returns the value of the node.
Definition RMHeap.h:139
std::default_random_engine m_rand
Random values generator.
Definition RMHeap.h:142
void remove(RMHeapNode< T > *heapNode)
Removes given heapNode from the main tree (does not free memory!).
Definition RMHeap.h:246
void decrease(RMHeapNode< T > *heapNode, const T &value) override
Decreases value of the given heapNode to value.
Definition RMHeap.h:187
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.
Definition GML.h:110
Randomized meldable heap node.
Definition RMHeap.h:44
RMHeapNode< T > * parent
Parent of the node.
Definition RMHeap.h:51
T value
Value contained in the node.
Definition RMHeap.h:49
RMHeapNode< T > * left
Left child of the node.
Definition RMHeap.h:52
RMHeapNode(const T &nodeValue)
Creates heap node with a given nodeValue.
Definition RMHeap.h:56
RMHeapNode< T > * right
Right child of the node.
Definition RMHeap.h:53