-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci_Heap.h
212 lines (184 loc) · 4.22 KB
/
Fibonacci_Heap.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef FIBONACCI_HEAP_H
#define FIBONACCI_HEAP_H
#include <string>
#include <vector>
#include <list>
#include <limits>
#include <fstream>
#include <cmath>
using namespace std;
#include "NodoF.h"
template<class T, class D>
class Fibonacci_heap{
private:
int fh_size;
list<NodoT*> fb_heap;
NodoT * nodo_min;
public:
Fibonacci_heap(): fh_size{0}{}
NodoT* Insert(T key, D *data){
NodoT *n = new NodoT(key, data);
fb_heap.push_back(n);
if(nodo_min == nullptr || key < nodo_min->key){
nodo_min = n;
}
++fh_size;
return n;
}
NodoT* Insert(T key){
NodoT *n = new NodoT(key);
fb_heap.push_back(n);
if(nodo_min == nullptr || key < nodo_min->key){
nodo_min = n;
}
++fh_size;
return n;
}
NodoT* GetNewMinNodo(){
if(fb_heap.size() > 0){
auto aux = *fb_heap.begin();
for(auto &a:fb_heap){
if(a->key < aux->key){
aux = a;
}
}
return aux;
}
else
return nullptr;
}
NodoT* GetMinNodo(){
return nodo_min;
}
T GetMinNodoValue(){
if(nodo_min != nullptr)
return nodo_min->key;
else
return -1;
}
NodoT* DeleteMin(){
// retorna el nodo minimo y lo borra
if(fh_size > 0){
NodoT* aux = GetMinNodo();
auto itr_child = aux->children.begin();
while(itr_child != aux->children.end()){
// insertamos todos sus hijos en el heap
fb_heap.push_back(*itr_child);
++itr_child;
}
fb_heap.remove(aux);
--fh_size;
Compactar();
nodo_min = GetNewMinNodo();
return aux;
}
else
return nullptr;
}
// unir dos nodos siempre pone el nodo resultado en el primer paramentro
NodoT* Unir_dos_nodos(NodoT* &a, NodoT* &b){
if(a->key < b->key){
a->rank = a->rank +1;
b->padre = a;
a->children.push_back(b);
fb_heap.remove(b);
return a;
}
else{
b->rank = b->rank +1;
a->padre = b;
b->children.push_back(a);
fb_heap.remove(b);
a = b;
return a;
}
}
void Compactar(){
int tm = log2(fh_size) + 1;
vector<NodoT*> v(tm);
fill(v.begin(), v.end(), nullptr);
NodoT* nodo_aux;
int id_aux;
for(auto i=fb_heap.begin(); i!=fb_heap.end(); ++i){
if(v[(*i)->rank] != nullptr){
id_aux = (*i)->rank;
nodo_aux = Unir_dos_nodos((*i),v[(*i)->rank]);
v[id_aux] = nullptr;
--i; // para volver a verificar este nodo
}
else{
v[(*i)->rank] = (*i);
}
}
}
void print(){
for(auto &a:fb_heap){
cout << a->key << "\t";
}
cout << endl;
}
void PrintSubTree(NodoT* root) {
if (root) {
for(auto it : root->children){
PrintSubTree(it);
cout << it->key << " ";
}
}
}
void PrintSubTreeSpecial(NodoT* root) {
if (root) {
for(auto it : root->children){
cout << root->key << " -> ";
cout << it->key << ";\n";
cout << it->key << " -> ";
PrintSubTree(it);
}
}
}
void printSpecial() {
for(auto it = fb_heap.begin(); it != fb_heap.end(); it++){
cout << (*it)->key << ";\n";
// cout << "==Begin Printing sub tree==\n";
PrintSubTreeSpecial(*it);
// cout << "\n==End Printing sub tree==\n";
}
}
void generatePDF() {
fstream file("FibonacciHeap.vz", fstream::out | fstream::trunc);
if (file.is_open()) {
file << "digraph G\n";
file << "{\n";
printSpecial(file);
file << "}\n";
file.close();
system("dot -Tpdf FibonacciHeap.vz -o FibonacciHeap.pdf");
}
}
void PrintSubTreeSpecial(fstream& file, NodoT* root) {
if (root) {
file << "\"" << root <<"\" [\n";
file << "\tlabel = "<< root->key <<"\n]\n";
for(auto it : root->children){
if (it->key != root->key) {
file << "\"" << root <<"\" -> ";
file << "\"" << it << "\";\n";
/*
file << root->m_key << " -> ";
file << it->m_key << ";\n";*/
PrintSubTreeSpecial(file, it);
}
}
}
}
void printSpecial(fstream& file) {
for(auto it = fb_heap.begin(); it != fb_heap.end(); it++) {
file << "\"" << (*it) <<"\" [\n";
file << "\tlabel = "<< (*it)->key <<"\n]\n";
PrintSubTreeSpecial(file, *it);
}
}
int get_size() {
return fh_size;
}
};
#endif // FIBONACCI_HEAP_H