-
Notifications
You must be signed in to change notification settings - Fork 5
/
bintree.c
398 lines (347 loc) · 10.6 KB
/
bintree.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* $Id: bintree.c,v 1.9 2008/07/15 07:40:25 bnv Exp $
* $Log: bintree.c,v $
* Revision 1.9 2008/07/15 07:40:25 bnv
* #include changed from <> to ""
*
* Revision 1.8 2008/07/14 13:08:42 bnv
* MVS,CMS support
*
* Revision 1.7 2004/04/30 15:24:38 bnv
* Added: include file os.h
*
* Revision 1.6 2003/10/30 13:15:12 bnv
* default removed
*
* Revision 1.5 2002/06/11 12:37:38 bnv
* Added: CDECL
*
* Revision 1.4 2001/06/25 18:51:48 bnv
* Header -> Id
*
* Revision 1.3 1999/11/26 13:13:47 bnv
* Changed: To use the new macros.
*
* Revision 1.2 1999/03/10 16:53:32 bnv
* *** empty log message ***
*
* Revision 1.1 1998/07/02 17:34:50 bnv
* Initial revision
*
*/
/*
* Binary Tree
* ~~~~~~ ~~~~
* Very general purpose routines for binary tree implemetation.
* Each leaf contains a (PLstr)key with the name of the leaf
* and a (void*)value which contains the value of the leaf.
*
* The searching is done with the key's checked with _Lstrcmp
* that means that an INTEGER or a REAL is stored according
* to its binary representation in memory.
*
* When adding a leaf no memory allocation is done for the key
* and the value.
*/
#include "os.h"
#include "bmem.h"
#ifndef WCE
# include <stdio.h>
#endif
#include <string.h>
#include "bintree.h"
#ifdef __DEBUG__
static int scandepth( BinLeaf *leaf, int depth );
#endif
/* ------------------ BinAdd ------------------ */
BinLeaf *__CDECL
BinAdd(BinTree *tree, PLstr name, void *dat) {
BinLeaf *thisEntry;
BinLeaf *lastEntry;
BinLeaf *leaf;
bool leftTaken = FALSE;
int cmp, dep = 0;
/* If tree is NULL then it will produce an error */
thisEntry = tree->parent;
while (thisEntry != NULL) {
lastEntry = thisEntry;
cmp = _Lstrcmp(name, &(thisEntry->key));
if (cmp < 0) {
thisEntry = thisEntry->left;
leftTaken = TRUE;
} else if (cmp > 0) {
thisEntry = thisEntry->right;
leftTaken = FALSE;
} else
return thisEntry;
dep++;
}
/* Create a new entry */
leaf = (BinLeaf *) MALLOC(sizeof(BinLeaf), "BinLeaf");
/* just move the data to the new Lstring */
/* and initialise the name LSTR(*name)=NULL */
LMOVESTR(leaf->key, *name);
leaf->value = dat;
leaf->left = NULL;
leaf->right = NULL;
if (tree->parent == NULL)
tree->parent = leaf;
else {
if (leftTaken)
lastEntry->left = leaf;
else
lastEntry->right = leaf;
}
tree->items++;
if (dep > tree->maxdepth) {
tree->maxdepth = dep;
if (tree->maxdepth > tree->balancedepth)
BinBalance(tree);
}
return leaf;
} /* BinAdd */
/* ------------------ BinFind ----------------- */
BinLeaf *__CDECL
BinFind(BinTree *tree, PLstr name) {
BinLeaf *leaf;
int cmp;
leaf = tree->parent;
while (leaf != NULL) {
cmp = _Lstrcmp(name, &(leaf->key));
if (cmp < 0)
leaf = leaf->left;
else if (cmp > 0)
leaf = leaf->right;
else
return leaf;
}
return NULL;
} /* BinFind */
/* ----------------- BinDisposeLeaf -------------------- */
void __CDECL
BinDisposeLeaf(BinTree *tree, BinLeaf *leaf,
void (__CDECL *BinFreeData)(void *)) {
if (!leaf) return;
if (leaf->left)
BinDisposeLeaf(tree, leaf->left, BinFreeData);
if (leaf->right)
BinDisposeLeaf(tree, leaf->right, BinFreeData);
LFREESTR(leaf->key);
if (leaf->value && BinFreeData)
BinFreeData(leaf->value);
if (leaf == tree->parent)
tree->parent = NULL;
FREE(leaf);
tree->items--;
} /* BinDisposeLeaf */
/* ----------------- BinDisposeTree -------------------- */
void __CDECL
BinDisposeTree(BinTree *tree, void (__CDECL *BinFreeData)(void *)) {
if (tree != NULL) {
BinDisposeLeaf(tree, tree->parent, BinFreeData);
FREE(tree);
}
} /* BinDisposeTree */
/* -------------------------------------------------------------- */
/* To correctly delete a pointer from a Binary tree we must not */
/* change the tree structure, that is the smaller values are the */
/* left most. In order to satisfy this with few steps we must */
/* replace the pointer that is to be erased with the one which is */
/* closest with a smaller value (the right most from the left */
/* branch, as you can see below */
/* ... */
/* / */
/* (name) <-- to be dropped */
/* / \ */
/* (a) (d) (c)= newid from left branch */
/* / \ ... where c->right=NULL */
/* ... (c) (a)= par_newidt parent of newid*/
/* / \ */
/* (b) NIL newid will become the new */
/* ... sub-head when (name) is dropped */
/* | */
/* | */
/* \|/ */
/* V ... */
/* / */
/* (c) but in the case that */
/* / \ (a)=(c) when a->right = NULL */
/* (a) (d) then the tree is very simple */
/* / \ .... we simply replace a->right=d */
/* ... (b) */
/* .... */
/* */
/* -------------------------------------------------------------- */
void __CDECL
BinDel(BinTree *tree, PLstr name, void (__CDECL *BinFreeData)(void *)) {
BinLeaf *thisid, *previous = NULL, *par_newid, *newid;
bool lefttaken = FALSE;
int cmp;
thisid = tree->parent;
while (thisid != NULL) {
cmp = _Lstrcmp(name, &(thisid->key));
if (cmp < 0) {
previous = thisid;
thisid = thisid->left;
lefttaken = TRUE;
} else if (cmp > 0) {
previous = thisid;
thisid = thisid->right;
lefttaken = FALSE;
} else
break;
}
if (thisid == NULL) return; /* Not Found */
if (thisid->right == NULL)
newid = thisid->left;
else if (thisid->left == NULL)
newid = thisid->right;
else { /* when no node is empty */
/* find the right most id of the */
/* left branch of thisid */
par_newid = thisid;
newid = thisid->left;
while (newid->right != NULL) {
par_newid = newid;
newid = newid->right;
}
/* newid must now replace thisid */
newid->right = thisid->right;
if (par_newid != thisid) {
par_newid->right = newid->left;
newid->left = thisid->left;
}
}
if (thisid == tree->parent)
tree->parent = newid;
else {
if (lefttaken)
previous->left = newid;
else
previous->right = newid;
}
thisid->left = NULL;
thisid->right = NULL;
BinDisposeLeaf(tree, thisid, BinFreeData);
} /* BinDel */
#ifdef __DEBUG__
/* -------------------- BinPrint ---------------------- */
void __CDECL
BinPrint(BinLeaf *leaf)
{
long i;
static int depth = 0;
if (!leaf) return;
depth += 3;
BinPrint(leaf->left);
for (i=0; i<depth-3; i++) PUTCHAR(' ');
PUTCHAR('\"');
Lprint(STDOUT,&(leaf->key));
switch (LTYPE(leaf->key)) {
case LINTEGER_TY:
PUTS("\"d = ");
break;
case LREAL_TY:
PUTS("\"r = ");
break;
case LSTRING_TY:
PUTS("\"s = ");
break;
}
//Lprint(STDOUT,leaf->value);
if (leaf->value)
printf("%p\n",leaf->value);
else
PUTS("NULL\n");
BinPrint(leaf->right);
depth -= 3;
} /* BinPrint */
#endif
/* -------------------- LeafBalance --------------------- */
static void
LeafBalance(BinLeaf *leaf, BinLeaf **head, BinLeaf **tail) {
BinLeaf *Lhead, *Ltail;
BinLeaf *Rhead, *Rtail;
if (leaf == NULL) {
*head = NULL;
*tail = NULL;
return;
}
LeafBalance(leaf->left, &Lhead, &Ltail);
LeafBalance(leaf->right, &Rhead, &Rtail);
/* connect nodes */
/* head - left - middle - right - tail */
if (Ltail) Ltail->right = leaf;
leaf->left = Ltail;
leaf->right = Rhead;
if (Rhead) Rhead->left = leaf;
if (Lhead)
*head = Lhead;
else
*head = leaf;
if (Rtail)
*tail = Rtail;
else
*tail = leaf;
} /* LeafBalance */
/* -------------------- LeafConstruct ------------------- */
static BinLeaf *
LeafConstruct(BinLeaf *head, BinLeaf *tail, int n, int *maxdepth) {
int Lmaxd, Rmaxd, i, mid;
BinLeaf *Lleaf, *Rleaf, *LMidleaf, *Midleaf, *RMidleaf;
if (n == 0) return NULL;
if (n == 1) {
/* then head must be equal to tail */
head->left = NULL;
head->right = NULL;
return head;
}
if (n == 2) {
(*maxdepth)++;
head->left = NULL;
head->right = tail;
tail->left = NULL;
tail->right = NULL;
return head;
}
/* --- find middle --- */
mid = n / 2;
LMidleaf = head;
for (i = 0; i < mid - 1; i++)
LMidleaf = LMidleaf->right;
Midleaf = LMidleaf->right;
RMidleaf = Midleaf->right;
/* --- do the same for left and right branch --- */
Lmaxd = Rmaxd = *maxdepth + 1;
Lleaf = LeafConstruct(head, LMidleaf, mid, &Lmaxd);
Rleaf = LeafConstruct(RMidleaf, tail, n - mid - 1, &Rmaxd);
*maxdepth = MAX(Lmaxd, Rmaxd);
Midleaf->left = Lleaf;
Midleaf->right = Rleaf;
return Midleaf;
} /* LeafConstruct */
/* -------------------- BinBalance ---------------------- */
void __CDECL
BinBalance(BinTree *tree) {
BinLeaf *head, *tail;
int maxdepth = 1;
/* first we will make tree a double queue */
LeafBalance(tree->parent, &head, &tail);
/* now we must reconstruct the tree */
tree->parent =
LeafConstruct(head, tail, tree->items, &maxdepth);
tree->maxdepth = maxdepth;
tree->balancedepth = maxdepth + BALANCE_INC;
} /* BinBalance */
#ifdef __DEBUG__
static int
scandepth( BinLeaf *leaf, int depth )
{
int left, right;
if (leaf==NULL)
return depth;
left = scandepth(leaf->left,depth+1);
right = scandepth(leaf->right,depth+1);
return MAX(left,right);
} /* scandepth */
#endif