斐波那契堆(简单版)

这个只有搞priority_queue的插入和删除,别的没有

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
#ifndef __Priority_Queue__
#define __Priority_Queue__

#include <cmath>

typedef size_t size_type;

template<class value_type>
class PriorityQueue {
private:
class Node {
public:
Node *parent, *left, *right, *child;
int degree;
value_type key;
Node(value_type Key) {
key = Key;
left = this;
right = this;
child = nullptr;
parent = nullptr;
degree = 0;
}
};
Node *minNode;
size_type qSize;
void insert(const value_type & value);
void insertToRight(Node *a, Node *h);
Node *extractMin();
void extractNode(Node *h);
void deleteMin();
void consolidate();
public:
PriorityQueue() {
minNode = nullptr;
qSize = 0;
}
const value_type & top() const;
bool empty() const;
size_type size() const;
void push(const value_type& value);
void pop();
};

template<class value_type>
void PriorityQueue<value_type>::extractNode(Node *h) {
h->left->right = h->right;
h->right->left = h->left;
}

template<class value_type>
void PriorityQueue<value_type>::insertToRight(Node *a, Node *h) {
if(a!=nullptr) {
h->left = a;
h->right = a->right;
h->right->left = h;
a->right = h;
h->parent = a->parent;
}
}

template<class value_type>
void PriorityQueue<value_type>::insert(const value_type & value) {
Node *newNode=new Node(value);
if(minNode==nullptr)
minNode = newNode;
else {
insertToRight(minNode, newNode);
if(value<minNode->key)
minNode = newNode;
}
qSize++;
}

template<class value_type>
typename PriorityQueue<value_type>::Node * PriorityQueue<value_type>::extractMin() {
Node *h=minNode;
if(h==h->right)
minNode = nullptr;
else {
extractNode(h);
minNode = h->right;
}
h->left = h->right = h;
return h;
}

template<class value_type>
void PriorityQueue<value_type>::consolidate() {
if(minNode==nullptr)
return ;
int maxDeg=1.0*std::log(qSize)/std::log(2)+1;
Node **cons=new Node *[maxDeg+1];
for(int i=0; i<=maxDeg; i++)
cons[i] = nullptr;
while(minNode!=nullptr) {
Node *x=extractMin();
int deg=x->degree;
while(cons[deg]!=nullptr) {
if(x->key>cons[deg]->key)
std::swap(x, cons[deg]);
extractNode(cons[deg]);
if (x->child == NULL)
x->child = cons[deg];
else
insertToRight(x->child, cons[deg]);
cons[deg]->parent = x;
x->degree++;
cons[deg] = nullptr;
deg++;
}
cons[deg] = x;
}
minNode = nullptr;
for(int i=0; i<=maxDeg; i++) {
if(cons[i]!=nullptr) {
if(minNode==nullptr)
minNode = cons[i];
else {
insertToRight(minNode, cons[i]);
if(cons[i]->key<minNode->key)
minNode = cons[i];
}
}
}
delete cons;
}

template<class value_type>
void PriorityQueue<value_type>::deleteMin() {
if(minNode==nullptr) return ;
Node *child=nullptr;
Node *delNode=minNode;
while(delNode->child!=nullptr) {
child = delNode->child;
extractNode(child);
if(child->right==child)
delNode->child = nullptr;
else
delNode->child = child->right;
insertToRight(minNode, child);
child->parent = nullptr;
}
extractNode(delNode);
if(delNode->right==delNode) minNode = nullptr;
else {
minNode = delNode->right;
consolidate();
}
qSize--;
delete delNode;
}

template<class value_type>
const value_type & PriorityQueue<value_type>::top() const {
return minNode->key;
}

template<class value_type>
bool PriorityQueue<value_type>::empty() const {
return qSize==0;
}

template<class value_type>
size_type PriorityQueue<value_type>::size() const {
return qSize;
}

template<class value_type>
void PriorityQueue<value_type>::push(const value_type& value) {
insert(value);
}

template<class value_type>
void PriorityQueue<value_type>::pop() {
deleteMin();
}

#endif
打赏
  • © 2018-2020 poorpool
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

请我喝杯草莓芝士奶盖吧~

支付宝
微信