-
Notifications
You must be signed in to change notification settings - Fork 0
/
FibonacciHeapViz.hpp
76 lines (57 loc) · 2.08 KB
/
FibonacciHeapViz.hpp
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
#ifndef FIBONACCI_HEAP_VIZ_HPP
#define FIBONACCI_HEAP_VIZ_HPP
#include <list>
#include <iostream>
#include "FibonacciHeap.hpp"
template<class T>
class FibonacciHeapViz : public FibonacciHeap<T> {
private:
std::size_t _id(Element<T> *x) {
return std::hash<Element<T>*>{}(x);
}
void _exportDotNode(std::ostream &out, Element<T> *x) {
if (x == nullptr) return;
if (x->isMarked()) {
out << _id(x) << "[fillcolor=grey, style=filled]" << std::endl;
}
out << _id(x) << "[label=\"" << x->getKey() << " (" << x->getDegree() << ")\"]" << std::endl;
out << _id(x) << " -> " << _id(x->getLeft()) << "[color=red]" << std::endl;
out << _id(x) << " -> " << _id(x->getRight()) << "[color=blue]" << std::endl;
if (x->getParent() != nullptr) {
out << _id(x) << " -> " << _id(x->getParent()) << "[color=green]" << std::endl;
}
}
void _exportDot(std::ostream &out, Element<T> *x, Element<T> *stop = nullptr, std::list<std::size_t> level = std::list<std::size_t>()) {
if (x == nullptr) return;
if (stop != nullptr && x == stop) {
out << "{rank=same;";
for (const auto &n : level) {
out << " " << n;
}
out << ";}" << std::endl;
return;
}
this->_exportDotNode(out, x);
if (x->getChild() != nullptr) {
out << _id(x) << " -> " << _id(x->getChild()) << "[color=black]" << std::endl;
this->_exportDot(out, x->getChild());
}
if (stop == nullptr) {
stop = x;
}
level.push_back(_id(x));
this->_exportDot(out, x->getRight(), stop, level);
}
public:
void exportDOT(std::ostream &out = std::cout) {
out << "digraph G {" << std::endl;
if (this->min == nullptr) {
out << "}" << std::endl;
return;
}
out << "min -> " << _id(this->min) << std::endl;
this->_exportDot(out, this->min);
out << "}" << std::endl;
}
};
#endif // FIBONACCI_HEAP_VIZ_HPP