-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftistTree.c
More file actions
218 lines (168 loc) · 4.06 KB
/
Copy pathLeftistTree.c
File metadata and controls
218 lines (168 loc) · 4.06 KB
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
#include "LeftistTree.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define SWAP(a, b, t) do{ \
t = a; a = b; b = t;\
}while(0)
typedef struct node{
Element data;
struct node *lc, *rc;
int balance; //record height in HBLT or weight in WBLT
}Node;
struct leftist{
LeftistType type;
Node *root;
void (*merge)(Leftist *treea, Leftist *treeb);
};
//--function declaration
Node *newNode(Element data);
Leftist *initLeftist(LeftistType type);
void insertLeftist(Leftist *tree, Element data);
Element getMin(Leftist *tree);
void printLeftist(Leftist *tree);
void destroyLeftist(Leftist *tree);
Node *HBLTUnion(Node *a, Node *b);
void mergeHBLT(Leftist *treea, Leftist *treeb);
Node *WBLTUnion(Node *a, Node *b);
void mergeWBLT(Leftist *treea, Leftist *treeb);
//--end
//--helper
Node *newNode(Element data){
Node *new = (Node *)malloc(sizeof(Node));
new->balance = 0;
new->data = data;
new->lc = new->rc = NULL;
return new;
}
//--end
//--function
Leftist *initLeftist(LeftistType type){
Leftist *tree = (Leftist *)malloc(sizeof(Leftist));
tree->root = NULL;
tree->type = type;
if(type == HBLT)
tree->merge = mergeHBLT;
else{
assert(type == WBLT);
tree->merge = mergeWBLT;
}
return tree;
}
void insertLeftist(Leftist *tree, Element data){
Leftist *temp = initLeftist(tree->type);
Node *new = newNode(data);
new->balance = 1;
temp->root = new;
temp->merge = tree->merge;
temp->type = tree->type;
tree->merge(tree, temp);
return;
}
void printHelper(Node *now){
if(now == NULL)
return;
printHelper(now->lc);
printf("%d ", now->data.key);
printHelper(now->rc);
return;
}
void printLeftist(Leftist *tree){
printHelper(tree->root);
printf("\n");
return;
}
void destroyHelper(Node *now){
if(now == NULL)
return;
destroyHelper(now->lc);
destroyHelper(now->rc);
free(now);
return;
}
void destroyLeftist(Leftist *tree){
if(tree == NULL)
return;
destroyHelper(tree->root);
free(tree);
return;
}
Element getMin(Leftist *tree){
Element result = tree->root->data;
Leftist *temp1 = initLeftist(tree->type);
temp1->root = tree->root->lc;
Leftist *temp2 = initLeftist(tree->type);
temp2->root = tree->root->rc;
temp1->merge = temp2->merge;
free(tree->root);
tree->merge(temp1, temp2);
tree->root = temp1->root;
return result;
}
//--end
//--HBLT
Node *HBLTUnion(Node *a, Node *b){
if(a->data.key > b->data.key){
//swap
Node *temp;
SWAP(a, b, temp);
}
if(a->rc == NULL)
a->rc = b;
else
a->rc = HBLTUnion(a->rc, b);
//maintain property
if(a->lc == NULL){
a->lc = a->rc;
a->rc = NULL;
}
else if(a->lc->balance < a->rc->balance){
Node *temp;
SWAP(a->rc, a->lc, temp);
}
a->balance = (a->rc == NULL ? 1 : a->rc->balance + 1);
return a;
}
void mergeHBLT(Leftist *treea, Leftist *treeb){
if(treea->root == NULL){
treea->root = treeb->root;
}
else if(treeb->root != NULL)
treea->root = HBLTUnion(treea->root, treeb->root);
free(treeb);
return;
}
//--end
//--WBLT
Node *WBLTUnion(Node *a, Node *b){
if(a == NULL)
return b;
if(a->data.key > b->data.key){
//swap
Node *temp;
SWAP(a, b, temp);
}
int rb = (a->rc == NULL ? 0 : a->rc->balance) + b->balance;
int lb = (a->lc == NULL ? 0 : a->lc->balance);
if(rb > lb){
Node *temp = a->lc;
a->lc = WBLTUnion(a->rc, b);
a->rc = temp;
}
else
a->rc = WBLTUnion(a->rc, b);
//check
assert(a->rc == NULL || a->lc->balance >= a->rc->balance);
a->balance = lb + rb + 1;
return a;
}
void mergeWBLT(Leftist *treea, Leftist *treeb){
if(treea->root == NULL){
treea->root = treeb->root;
}
else if(treeb->root != NULL)
treea->root = WBLTUnion(treea->root, treeb->root);
free(treeb);
return;
}
//--end