This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binomial_Heap.cpp
164 lines (164 loc) · 4.86 KB
/
Binomial_Heap.cpp
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
#include <iostream>
#include <vector>
class BinomialNode {
public:
int key;
int degree;
BinomialNode* parent;
BinomialNode* child;
BinomialNode* sibling;
BinomialNode(int k) : key(k), degree(0), parent(nullptr), child(nullptr), sibling(nullptr) {}
};
class BinomialHeap {
private:
std::vector<BinomialNode*> trees;
public:
BinomialHeap() {}
bool isEmpty() const {
return trees.empty();
}
void insert(int key) {
BinomialNode* newNode = new BinomialNode(key);
BinomialHeap tempHeap;
tempHeap.trees.push_back(newNode);
unionHeaps(*this, tempHeap);
}
int getMin() const {
if (isEmpty()) {
std::cerr << "Error: Heap is empty\n";
return -1;
}
BinomialNode* minNode = findMinNode();
return minNode->key;
}
int extractMin() {
if (isEmpty()) {
std::cerr << "Error: Heap is empty\n";
return -1;
}
BinomialNode* minNode = findMinNode();
removeTree(minNode);
BinomialHeap tempHeap;
while (minNode->child) {
BinomialNode* child = minNode->child;
minNode->child = child->sibling;
child->parent = nullptr;
child->sibling = nullptr;
tempHeap.trees.push_back(reverseTree(child));
}
unionHeaps(*this, tempHeap);
int minValue = minNode->key;
delete minNode;
return minValue;
}
void decreaseKey(BinomialNode* node, int newKey) {
if (newKey > node->key) {
std::cerr << "Error: New key is greater than current key\n";
return;
}
node->key = newKey;
while (node->parent && node->key < node->parent->key) {
std::swap(node->key, node->parent->key);
node = node->parent;
}
}
void deleteNode(BinomialNode* node) {
decreaseKey(node, std::numeric_limits<int>::min());
extractMin();
}
void printHeap() {
if (isEmpty()) {
std::cout << "Heap is empty\n";
return;
}
for (BinomialNode* tree : trees) {
printTree(tree);
}
}
private:
BinomialNode* findMinNode() const {
BinomialNode* minNode = nullptr;
for (BinomialNode* tree : trees) {
if (!minNode || (tree && tree->key < minNode->key)) {
minNode = tree;
}
}
return minNode;
}
void removeTree(BinomialNode* target) {
trees.erase(std::remove(trees.begin(), trees.end(), target), trees.end());
}
void unionHeaps(BinomialHeap& heap1, BinomialHeap& heap2) {
heap1.trees.insert(heap1.trees.end(), heap2.trees.begin(), heap2.trees.end());
heap2.trees.clear();
consolidate(heap1);
}
void consolidate(BinomialHeap& heap) {
if (heap.trees.size() <= 1) {
return;
}
std::vector<BinomialNode*> degreeTable(2 * heap.trees.size(), nullptr);
for (BinomialNode* tree : heap.trees) {
int degree = tree->degree;
while (degreeTable[degree] != nullptr) {
BinomialNode* otherTree = degreeTable[degree];
if (tree->key > otherTree->key) {
std::swap(tree, otherTree);
}
link(tree, otherTree);
degreeTable[degree] = nullptr;
degree++;
}
degreeTable[degree] = tree;
}
heap.trees.clear();
for (BinomialNode* tree : degreeTable) {
if (tree != nullptr) {
heap.trees.push_back(tree);
}
}
}
void link(BinomialNode* tree1, BinomialNode* tree2) {
tree1->parent = tree2;
tree1->sibling = tree2->child;
tree2->child = tree1;
tree2->degree++;
}
BinomialNode* reverseTree(BinomialNode* root) {
BinomialNode* prev = nullptr;
BinomialNode* current = root;
while (current) {
BinomialNode* next = current->sibling;
current->sibling = prev;
current->parent = nullptr;
prev = current;
current = next;
}
return prev;
}
void printTree(BinomialNode* root) {
if (root == nullptr) {
return;
}
BinomialNode* current = root;
while (current) {
std::cout << current->key << " ";
printTree(current->child);
current = current->sibling;
}
}
};
int main() {
BinomialHeap binHeap;
binHeap.insert(3);
binHeap.insert(8);
binHeap.insert(2);
binHeap.insert(5);
binHeap.printHeap();
std::cout << "Min: " << binHeap.getMin() << "\n";
binHeap.decreaseKey(binHeap.trees[0]->child, 1);
binHeap.printHeap();
binHeap.deleteNode(binHeap.trees[0]->child);
binHeap.printHeap();
return 0;
}