Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit a4ad1a3

Browse files
committed
FibHeap.cpp
1 parent 2cc6dcf commit a4ad1a3

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

FibHeap.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//
2+
// algorithm - some algorithms in "Introduction to Algorithms", third edition
3+
// Copyright (C) 2018 lxylxy123456
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef MAIN
20+
#define MAIN
21+
#define MAIN_FibHeap
22+
#endif
23+
24+
#ifndef FUNC_FibHeap
25+
#define FUNC_FibHeap
26+
27+
#include <vector>
28+
#include "utils.h"
29+
30+
template <typename T>
31+
class FNode {
32+
public:
33+
FNode(T k, FNode<T>* P): key(k), degree(0), p(P), child(nullptr),
34+
left(nullptr), right(nullptr), mark(false) {}
35+
// FNode<T>* recursive_destruct() {}
36+
T key;
37+
size_t degree;
38+
FNode<T> *p, *child, *left, *right;
39+
bool mark;
40+
};
41+
42+
template <typename T>
43+
class FibHeap {
44+
public:
45+
FibHeap() { MakeFibHeap(); }
46+
void MakeFibHeap() {
47+
n = 0;
48+
min = nullptr;
49+
}
50+
FNode<T>* FibHeapInsert(const T& k) {
51+
FNode<T>* x = new FNode<T>(k, nullptr);
52+
if (!min) {
53+
x->left = x;
54+
x->right = x;
55+
min = x;
56+
} else {
57+
x->left = min->left;
58+
x->right = min;
59+
min->left = x;
60+
x->left->right = x;
61+
if (x->key < min->key)
62+
min = x;
63+
}
64+
n++;
65+
return x;
66+
}
67+
FNode<T>* FibHeapMinimum() {
68+
return min;
69+
}
70+
FibHeap<T> FibHeapUnion(FibHeap<T>& rhs) {
71+
FibHeap<T> H;
72+
if (min) {
73+
H.min = min;
74+
if (rhs.min) {
75+
FNode<T> *ll = min->left, *rl = rhs.min->left;
76+
min->left = rl;
77+
rhs.min->left = ll;
78+
ll->right = rhs.min;
79+
rl->right = min;
80+
if (rhs.min < min)
81+
H.min = rhs.min;
82+
}
83+
} else
84+
H.min = rhs.min;
85+
H.n = n + rhs.n;
86+
MakeFibHeap();
87+
rhs.MakeFibHeap();
88+
return H;
89+
}
90+
// ~FibHeap() { if (min) { delete min->recursive_destruct(); } }
91+
FNode<T>* min;
92+
size_t n;
93+
};
94+
#endif
95+
96+
#ifdef MAIN_FibHeap
97+
int main(int argc, char *argv[]) {
98+
std::vector<FibHeap<int>> FH_list(1);
99+
FibHeap<int>* FH = &(FH_list[0]);
100+
std::cout << "s: select" << std::endl;
101+
std::cout << "i: insert" << std::endl;
102+
std::cout << "m: minimum" << std::endl;
103+
std::cout << "u: union" << std::endl;
104+
// std::cout << "p: print tree" << std::endl;
105+
std::cout << "q: quit" << std::endl;
106+
FNode<int>* ptr = nullptr;
107+
size_t n = 0;
108+
while (true) {
109+
char c; int x; size_t n2;
110+
std::cout << "n = " << n << std::endl;
111+
std::cout << "ptr = " << pptr(ptr) << std::endl;
112+
std::cout << ">> ";
113+
if (!(std::cin >> c)) {
114+
std::cout << std::endl;
115+
break;
116+
}
117+
switch (c) {
118+
case 's':
119+
std::cout << "n = ";
120+
std::cin >> n;
121+
if (FH_list.size() <= n)
122+
FH_list.resize(n + 1);
123+
FH = &(FH_list[n]);
124+
ptr = FH->min;
125+
break;
126+
case 'i':
127+
std::cout << "x = ";
128+
std::cin >> x;
129+
ptr = FH->FibHeapInsert(x);
130+
break;
131+
case 'm':
132+
ptr = FH->FibHeapMinimum();
133+
break;
134+
case 'u':
135+
std::cout << "n = ";
136+
std::cin >> n2;
137+
*FH = FH->FibHeapUnion(FH_list[n2]);
138+
break;
139+
case 'q':
140+
return 0;
141+
}
142+
}
143+
return 0;
144+
}
145+
#endif
146+

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@
114114
| 18 | BTree.cpp | B Tree Insert | 495 |
115115
| 18 | BTree.cpp | B Tree Insert Nonfull | 495 |
116116
| 18 | BTree.cpp | B Tree Insert Delete | 502 |
117+
| 19 | FibHeap.cpp | Make Fib Heap | 510 |
118+
| 19 | FibHeap.cpp | Fib Heap Insert | 510 |
119+
| 19 | FibHeap.cpp | Fib Heap Minimum | 511 |
120+
| 19 | FibHeap.cpp | Fib Heap Union | 512 |

0 commit comments

Comments
 (0)