Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

RMHeap.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 
36 #include <functional>
37 #include <random>
38 
39 namespace ogdf {
40 
41 
43 template<typename T>
44 struct RMHeapNode {
45  template<typename, typename>
46  friend class RMHeap;
47 
48 protected:
49  T value;
50 
54 
56  explicit RMHeapNode(const T& nodeValue)
57  : value(nodeValue), parent(nullptr), left(nullptr), right(nullptr) { }
58 };
59 
61 
74 template<typename T, typename C = std::less<T>>
75 class RMHeap : public HeapBase<RMHeap<T, C>, RMHeapNode<T>, T, C> {
77 
78 public:
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 
141 private:
142  std::default_random_engine m_rand;
144 
148  void remove(RMHeapNode<T>* heapNode);
149 
151  static void release(RMHeapNode<T>* heapNode);
152 };
153 
154 template<typename T, typename C>
155 RMHeap<T, C>::RMHeap(const C& cmp, int initialSize)
156  : base_type(cmp), m_rand((std::random_device())()), m_root(nullptr) { }
157 
158 template<typename T, typename C>
160  release(m_root);
161 }
162 
163 template<typename T, typename C>
164 const T& RMHeap<T, C>::top() const {
165  return m_root->value;
166 }
167 
168 template<typename T, typename C>
170  RMHeapNode<T>* heapNode = new RMHeapNode<T>(value);
171  m_root = merge(m_root, heapNode);
172 
173  return heapNode;
174 }
175 
176 template<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 
186 template<typename T, typename C>
187 void RMHeap<T, C>::decrease(RMHeapNode<T>* heapNode, const T& value) {
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 
201 template<typename T, typename C>
203  m_root = merge(m_root, other.m_root);
204  other.m_root = nullptr;
205 }
206 
207 template<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 
245 template<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 
258 template<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 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::HeapBase
Common interface for all heap classes.
Definition: HeapBase.h:48
ogdf::RMHeapNode
Randomized meldable heap node.
Definition: RMHeap.h:44
ogdf::RMHeap::merge
void merge(RMHeap< T, C > &other) override
Merges in values of other heap.
Definition: RMHeap.h:202
ogdf::RMHeap::push
RMHeapNode< T > * push(const T &value) override
Inserts a new node with given value into a heap.
Definition: RMHeap.h:169
ogdf::RMHeap::value
const T & value(RMHeapNode< T > *heapNode) const override
Returns the value of the node.
Definition: RMHeap.h:139
ogdf::RMHeap::top
const T & top() const override
Returns reference to the top element in the heap.
Definition: RMHeap.h:164
ogdf::RMHeap::~RMHeap
virtual ~RMHeap()
Destructs the heap.
Definition: RMHeap.h:159
ogdf::RMHeapNode::RMHeapNode
RMHeapNode(const T &nodeValue)
Creates heap node with a given nodeValue.
Definition: RMHeap.h:56
ogdf::RMHeap::release
static void release(RMHeapNode< T > *heapNode)
Recursively releases memory occupied by heap pointed by heapNode.
Definition: RMHeap.h:259
ogdf::RMHeap::m_rand
std::default_random_engine m_rand
Random values generator.
Definition: RMHeap.h:142
ogdf::RMHeap
Randomized meldable heap implementation.
Definition: RMHeap.h:75
ogdf::RMHeapNode::parent
RMHeapNode< T > * parent
Parent of the node.
Definition: RMHeap.h:51
HeapBase.h
Interface for heap implementations.
ogdf::RMHeap::pop
void pop() override
Removes the top element from the heap.
Definition: RMHeap.h:177
ogdf::RMHeap::m_root
RMHeapNode< T > * m_root
Root node of the heap.
Definition: RMHeap.h:143
ogdf::RMHeapNode::left
RMHeapNode< T > * left
Left child of the node.
Definition: RMHeap.h:52
std
Definition: GML.h:111
ogdf::RMHeapNode::right
RMHeapNode< T > * right
Right child of the node.
Definition: RMHeap.h:53
ogdf::RMHeap::RMHeap
RMHeap(const C &cmp=C(), int initialSize=-1)
Creates empty randomized meldable heap.
Definition: RMHeap.h:155
ogdf::RMHeapNode::value
T value
Value contained in the node.
Definition: RMHeap.h:49
ogdf::RMHeap::remove
void remove(RMHeapNode< T > *heapNode)
Removes given heapNode from the main tree (does not free memory!).
Definition: RMHeap.h:246
Minisat::Internal::remove
static void remove(V &ts, const T &t)
Definition: Alg.h:36
ogdf::RMHeap::decrease
void decrease(RMHeapNode< T > *heapNode, const T &value) override
Decreases value of the given heapNode to value.
Definition: RMHeap.h:187