52 previous = next =
this;
67 previous = next =
this;
75 return (
this == this->next);
87 this->next->previous = other->
previous;
97 this->previous->next = this->next;
98 this->next->previous = this->previous;
99 this->next = this->previous =
this;
125 throw string (
"Trying to remove a child from a non-parent");
128 throw string (
"Trying to remove a non-child");
158 out << myData <<
":" << myKey <<
":" << degree <<
":" << mark;
164 throw string(
"Illegal pointer - node is child of itself");
193 Key
key()
const {
return myKey; }
232 if (!rootWithMinKey) {
233 rootWithMinKey = newNode;
235 rootWithMinKey->
insert(newNode);
236 if (newNode->
key() < rootWithMinKey->key()) rootWithMinKey = newNode;
247 rootWithMinKey(NULL), count(0), maxDegree(0) {}
252 while(this->empty() ==
false ) {
262 bool empty()
const {
return count==0;}
270 throw string(
"no minimum element");
271 return rootWithMinKey;
279 out <<
"maxDegree=" << maxDegree <<
" count=" << count <<
" roots=";
281 rootWithMinKey->printAll(out);
296 count += other.
count;
314 if (!rootWithMinKey)
throw string(
"trying to remove from an empty heap");
319 if (rootWithMinKey->child) {
328 }
while (c!=rootWithMinKey->
child);
330 rootWithMinKey->child = NULL;
331 rootWithMinKey->insert(c);
336 if (rootWithMinKey->next == rootWithMinKey) {
338 if (count!=0)
throw string (
"Internal error: should have 0 keys");
339 rootWithMinKey = NULL;
345 vector<PNode> degreeRoots ((maxDegree+250));
346 fill (degreeRoots.begin(), degreeRoots.end(), (
PNode)NULL);
348 PNode currentPointer = rootWithMinKey->
next;
352 currentDegree = currentPointer->
degree;
354 PNode current = currentPointer;
355 currentPointer = currentPointer->
next;
356 while (degreeRoots[currentDegree] != NULL) {
357 PNode other = degreeRoots[currentDegree];
358 if (current->
key() > other->
key())
364 degreeRoots[currentDegree]=NULL;
366 if (currentDegree >= degreeRoots.size())
367 degreeRoots.push_back((
PNode)NULL);
370 degreeRoots[currentDegree] = current;
372 }
while (currentPointer != rootWithMinKey);
375 delete rootWithMinKey;
376 rootWithMinKey = NULL;
378 unsigned int newMaxDegree=0;
379 for (
unsigned int d=0; d<degreeRoots.size(); ++d) {
380 if (degreeRoots[d]) {
381 degreeRoots[d]->
next = degreeRoots[d]->previous = degreeRoots[d];
382 insertNode(degreeRoots[d]);
387 maxDegree=newMaxDegree;
398 if (newKey >= node->
myKey)
399 throw string(
"Trying to decrease key to a greater key");
402 node->
myKey = newKey;
407 if (parent == NULL ) {
408 if (newKey < rootWithMinKey->key())
409 rootWithMinKey = node;
411 }
else if (parent->
key() <= newKey) {
421 }
else if (!parent->
mark) {
437 void remove(
PNode node, Key minusInfinity) {
438 if (minusInfinity >= minimum()->key())
439 throw string(
"2nd argument to remove must be a key that is smaller than all other keys");
440 decreaseKey(node, minusInfinity);
void printAll(ostream &out) const
Print every tree of nodes on the same level as the current one.
Definition: FiboHeap.hpp:177
void merge(const FibonacciHeap &other)
Fibonacci-Heap union procedure.
Definition: FiboHeap.hpp:292
FibonacciHeapNode< Data, Key > * parent
pointer to the parent
Definition: FiboHeap.hpp:42
FibonacciHeapNode()
Default constructor.
Definition: FiboHeap.hpp:45
FibonacciHeap()
Constructor.
Definition: FiboHeap.hpp:246
unsigned int uint
Definition: FiboHeap.hpp:22
bool mark
mark used in the decreaseKey algorithm.
Definition: FiboHeap.hpp:36
A template Fibonacci heap node class.
Definition: FiboHeap.hpp:30
bool empty() const
Check if the heap is empty.
Definition: FiboHeap.hpp:262
PNode rootWithMinKey
a circular d-list of nodes
Definition: FiboHeap.hpp:218
void addChild(FibonacciHeapNode< Data, Key > *other)
Add a child to the node.
Definition: FiboHeap.hpp:108
void decreaseKey(PNode node, Key newKey)
Decrease the key of a node to a given value.
Definition: FiboHeap.hpp:396
FibonacciHeapNode< Data, Key > * child
pointer to the first child in the list of children
Definition: FiboHeap.hpp:41
Key myKey
key associated to the node
Definition: FiboHeap.hpp:32
FibonacciHeapNode< Data, Key > * next
pointer to the next node
Definition: FiboHeap.hpp:40
PNode insert(Data d, Key k)
Insert a node to the heap.
Definition: FiboHeap.hpp:306
Data data() const
Return node's data.
Definition: FiboHeap.hpp:199
FibonacciHeapNode< Data, Key > * PNode
Definition: FiboHeap.hpp:216
~FibonacciHeap()
Destructor.
Definition: FiboHeap.hpp:250
void removeChild(FibonacciHeapNode< Data, Key > *other)
Remove a child node.
Definition: FiboHeap.hpp:123
Data myData
data stored in the node
Definition: FiboHeap.hpp:33
void printTree(ostream &out) const
Print the tree of nodes starting from the current one acting as the root.
Definition: FiboHeap.hpp:157
void deletemin()
Remove the minimum node of the heap, i.e. the Fibonacci Heap Extract Min procedure.
Definition: FiboHeap.hpp:313
FibonacciHeapNode< Data, Key > * previous
pointer to the previous node
Definition: FiboHeap.hpp:39
bool isSingle() const
Check if the node is single.
Definition: FiboHeap.hpp:74
Key key() const
Return node's key.
Definition: FiboHeap.hpp:193
PNode minimum() const
Return the node associated with the minimum key.
Definition: FiboHeap.hpp:268
uint count
total number of elements in heap
Definition: FiboHeap.hpp:219
void remove()
Removes the node from the list.
Definition: FiboHeap.hpp:96
uint degree
number of children. used in the deletemin algorithm.
Definition: FiboHeap.hpp:35
Template class for a Fibonacci Heap data structure.
Definition: FiboHeap.hpp:214
void insert(FibonacciHeapNode< Data, Key > *other)
Inserts a new node after this node.
Definition: FiboHeap.hpp:83
A data class.
Definition: Data.hpp:100
uint maxDegree
maximum degree (=child count) of a root in the circular d-list
Definition: FiboHeap.hpp:220
void printRoots(ostream &out) const
Print the roots in an output stream.
Definition: FiboHeap.hpp:278
FibonacciHeapNode(Data d, Key k)
Constructor.
Definition: FiboHeap.hpp:60
PNode insertNode(PNode newNode)
Insert a node to the heap.
Definition: FiboHeap.hpp:230