-
Notifications
You must be signed in to change notification settings - Fork 0
/
binomialheap.c
254 lines (238 loc) · 6 KB
/
binomialheap.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Created by Once on 2019/9/1.
//
#include "binomialheap.h"
#include <stdio.h>
#include <stdlib.h>
BinomialHeap *bheap_init(){
BinomialHeap *heap = (BinomialHeap*)malloc(sizeof(BinomialHeap));
if(!heap){
perror("alloc memory for heap error");
return NULL;
}
heap->list = NULL;
heap->size = 0;
return heap;
}
int bheap_is_full(BinomialHeap *bheap){
ANode *node = (ANode*)malloc(sizeof(ANode));
if(!node)
return 1;
free(node);
return 0;
}
int bheap_is_empty(BinomialHeap *bheap){
if(bheap == NULL)
return 1;
return bheap->size == 0;
}
// 合并两个阶数相同的二项树
static ltnode *merge_tree(ltnode *n1, ltnode *n2){
if(n1 == NULL || n2 == NULL)
return NULL;
if(n1->value->key > n2->value->key){
ANode *temp = n1->value;
n1->value = n2->value;
n2->value = temp;
}
ANode *t1 = n1->value, *t2 = n2->value;
t2->parent = t1;
t2->sibling = t1->child;
t1->child = t2;
t1->degree++;
return n1;
}
// 初步合并两个堆
static list *union_heap(list *h1, list *h2){
if(h1 == NULL)
return h2;
if(h2 == NULL)
return h1;
list *h = list_new();
if(!h)
return NULL;
ltnode *n1 = h1->head, *n2 = h2->head, *t;
while(!list_is_empty(h1) && !list_is_empty(h2)){
if(n1->value->degree <= n2->value->degree){
t = n1->next;
list_push_back(h, n1->value);
list_delete(h1, n1->value);
n1 = t;
}
else{
t = n2->next;
list_push_back(h, n2->value);
list_delete(h2, n2->value);
n2 = t;
}
}
while(!list_is_empty(h1)){
t = n1->next;
list_push_back(h, n1->value);
list_delete(h1, n1->value);
n1 = t;
}
while(!list_is_empty(h2)){
t = n2->next;
list_push_back(h, n2->value);
list_delete(h2, n2->value);
n2 = t;
}
list_clear(h1);
list_clear(h2);
return h;
}
// 调整堆以符合二项堆性质
static int just_heap(list *h){
if(h == NULL)
return 0;
if(h->size <= 1)
return 1;
ltnode *n1, *n2, *n3;
if(h->size == 2){
n1 = list_begin(h);
n2 = n1->next;
n3 = list_begin(h);
}
else{
n1 = list_begin(h);
n2 = n1->next;
n3 = n2->next;
}
do{
if(n2 == list_begin(h))
n1 = n1->next;
else if(n1->value->degree < n2->value->degree){
n1 = n1->next;
n2 = n2->next;
if(n3 != list_begin(h))
n3 = n3->next;
}
else if(n3 != list_begin(h) && n1->value->degree == n2->value->degree &&
n2->value->degree == n3->value->degree){
n1 = n1->next;
n2 = n2->next;
n3 = n3->next;
}
else if(n1->value->degree == n2->value->degree){
ANode *value = NULL;
ltnode *t = merge_tree(n1, n2);
if(t == n2)
value = n1->value;
else
value = n2->value;
ltnode *temp = n2->next;
list_delete(h, value);
n2 = temp;
if(n3 != list_begin(h))
n3 = n3->next;
}
}
while(n1 != list_end(h));
return 1;
}
// 将一个结点和一个二项堆合并
static int merge_with_node(BinomialHeap *bheap, list *h, ANode *node){
if(bheap == NULL || node == NULL)
return 0;
list *heap = list_new();
if(!heap)
return 0;
list_push_back(heap, node);
h = union_heap(h, heap);
bheap->list = h;
bheap->size++;
return just_heap(h);
}
int bheap_push(BinomialHeap *bheap, int key, Area *value){
if(bheap == NULL || value == NULL)
return 0;
ANode *node = (ANode*)malloc(sizeof(ANode));
if(!node){
perror("alloc memory for node error");
return 0;
}
node->key = key;
node->value.width = value->width;
node->value.height = value->height;
node->degree = 0;
node->parent = node->child = node->sibling = NULL;
return merge_with_node(bheap, bheap->list, node);
}
ANode *bheap_top(BinomialHeap *bheap){
if(bheap == NULL || bheap->size == 0)
return NULL;
list *lt = bheap->list;
ltnode *temp = lt->head, *min = lt->head;
do{
if(temp->value->key < min->value->key)
min = temp;
temp = temp->next;
}
while(temp != list_begin(lt));
return min->value;
}
static list *heap_from_tree(ANode *tree){
if(tree == NULL)
return NULL;
list *h = list_new();
if(!h)
return NULL;
ANode *temp = tree->child, *t;
while(temp){
t = temp;
temp = temp->sibling;
t->sibling = NULL;
list_push_front(h, t);
}
return h;
}
void bheap_pop(BinomialHeap *bheap){
if(bheap == NULL || bheap->size == 0)
return;
ANode *min = bheap_top(bheap);
list *new_heap = heap_from_tree(min);
list_delete(bheap->list, min);
bheap->list = union_heap(bheap->list, new_heap);
just_heap(bheap->list);
bheap->size--;
}
static void traverse_node(ANode *node){
if(!node)
return;
printf("%d ", node->key);
traverse_node(node->sibling);
traverse_node(node->child);
}
void bheap_traverse(BinomialHeap *bheap){
if(bheap == NULL || bheap->size == 0)
return;
ltnode *node = bheap->list->head;
do{
traverse_node(node->value);
node = node->next;
printf("--- ");
}
while(node != list_begin(bheap->list));
printf("\n");
}
static void clear_node(ANode *node){
if(!node)
return;
clear_node(node->child);
clear_node(node->sibling);
free(node);
}
int bheap_clear(BinomialHeap *bheap){
if(bheap == NULL || bheap->size == 0)
return 0;
ltnode *node = bheap->list->head;
do{
clear_node(node->value);
node = node->next;
}
while(node != list_begin(bheap->list));
list_clear(bheap->list);
free(bheap);
return 1;
}