forked from lamproae/ncc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdbstree.h
297 lines (256 loc) · 5.86 KB
/
dbstree.h
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
/******************************************************************************
Dynamic Binary Search Tree.
Check dbstree.tex for info.
******************************************************************************/
#include <alloca.h>
#ifndef HAVE_DBSTREE
#define HAVE_DBSTREE
#define DBS_MAGIC 32
extern char *StrDup (char*);
#define TX template<class dbsNode>
template<class dbsNode> class dbsTree
{
dbsNode **FoundSlot;
int FoundDepth;
void tree_to_array (dbsNode*);
void walk_tree (dbsNode*, void (*)(dbsNode*));
void walk_tree_io (dbsNode*, void (*)(dbsNode*));
void walk_tree_d (dbsNode*, void (*)(dbsNode*));
public:
dbsNode *root;
int nnodes;
dbsTree ();
void dbsBalance ();
dbsNode* dbsFind ();
void dbsRemove (dbsNode*);
void copytree (void (*)(dbsNode*));
void foreach (void (*)(dbsNode*));
void deltree (void (*)(dbsNode*));
dbsNode* parentOf (dbsNode*);
void addself (dbsNode*);
};
#define DBS_STRQUERY dbsNodeStr::Query
class dbsNodeStr
{
public:
int compare (dbsNodeStr*);
int compare ();
dbsNodeStr ();
char *Name;
~dbsNodeStr ();
static char *Query;
};
TX dbsTree<dbsNode>::dbsTree ()
{
nnodes = 0;
root = NULL;
FoundSlot = NULL;
}
/*
* Balancing a binary tree. This happens whenever the height reaches 32.
*/
TX void dbsTree<dbsNode>::tree_to_array (dbsNode *n)
{
if (n->less) tree_to_array (n->less);
*FoundSlot++ = n;
if (n->more) tree_to_array (n->more);
}
TX void dbsTree<dbsNode>::dbsBalance () // O(n)
{
dbsNode **npp;
unsigned long long i, j, k, D, Y, k2, k3;
if (!root) return;
npp = FoundSlot = (dbsNode**) alloca (sizeof (dbsNode*) * nnodes);
tree_to_array (root);
root = npp [nnodes / 2];
for (D = nnodes + 1, i = 4; i <= D; i *= 2)
for (j = 2; j < i; j += 4)
{
k3 = nnodes * j / i;
npp [k3]->less = npp [nnodes * (j - 1) / i],
npp [k3]->more = npp [nnodes * (j + 1) / i];
}
k = nnodes + 1 - (Y = i / 2);
if (k == 0)
{
for (i /=2, j = 1; j < i; j += 2)
k3 = nnodes * j / i,
npp [k3]->less = npp [k3]->more = NULL;
return;
}
for (j = 2; j < i; j += 4)
{
k3 = nnodes * j / i;
D = (k2 = (j - 1) * nnodes / i) * Y % nnodes;
if (D >= k || D == 0)
npp [k3]->less = NULL;
else
{
npp [k3]->less = npp [k2];
npp [k2]->less = npp [k2]->more = NULL;
}
D = (k2 = (j + 1) * nnodes / i) * Y % nnodes;
if (D >= k || D == 0)
npp [k3]->more = NULL;
else
{
npp [k3]->more = npp [k2];
npp [k2]->less = npp [k2]->more = NULL;
}
}
dbsNode *np;
for (np = root; np->less; np = np->less);
np->less = npp [0];
npp [0]->less = npp [0]->more = NULL;
}
TX void dbsTree<dbsNode>::addself (dbsNode *t)
{
t->less = t->more = 0;
*FoundSlot = t;
++nnodes;
if (FoundDepth >= DBS_MAGIC)
dbsBalance ();
FoundSlot = NULL; // Bug traper
}
/*
*/
TX void dbsTree<dbsNode>::dbsRemove (dbsNode *t) // O(log n)
{
dbsNode *np, *nl, *nr, *nlp, *nrp;
int isroot;
unsigned int i, j;
isroot = (np = parentOf (t)) == NULL;
--nnodes;
if (!(t->less && t->more))
{
if (isroot)
root = (t->less) ? t->less : t->more;
else
if (np->less == t)
np->less = (t->less) ? t->less : t->more;
else
np->more = (t->less) ? t->less : t->more;
return;
}
for (i = 0, nlp = NULL, nl = t->less; nl->more; i++)
nlp = nl, nl = nl->more;
for (j = 0, nrp = NULL, nr = t->more; nr->less; j++)
nrp = nr, nr = nr->less;
if (i >= j) // the smallest from bigger ones
{
if (isroot) root = nl;
else
if (np->less == t) np->less = nl;
else np->more = nl;
if (nlp)
{
nlp->more = nl->less;
nl->less = t->less;
}
nl->more = t->more;
}
else // Mirror situation
{
if (isroot) root = nr;
else
if (np->less == t) np->less = nr;
else np->more = nr;
if (nrp)
{
nrp->less = nr->more;
nr->more = t->more;
}
nr->less = t->less;
}
}
TX dbsNode *dbsTree<dbsNode>::parentOf (dbsNode *t) // O(log n)
{
dbsNode *np;
if ((np = root) == t)
return NULL;
while (np)
if (t->compare (np) < 0)
if (np->less == t) break;
else np = np->less;
else
if (np->more == t) break;
else np = np->more;
assert (np);
return np;
}
TX dbsNode *dbsTree<dbsNode>::dbsFind () // O (log n)
{
dbsNode *d;
int i;
FoundDepth = 0;
if (!(d = root)) {
FoundSlot = &root;
return NULL;
}
++FoundDepth;
for (;; ++FoundDepth) {
if ((i = d->compare ()) == 0) {
FoundSlot = NULL;
return d;
}
if (i < 0)
if (d->more) d = d->more;
else {
FoundSlot = &d->more;
return NULL;
}
else
if (d->less) d = d->less;
else {
FoundSlot = &d->less;
return NULL;
}
}
}
/*
* Moving in the tree.
* If we want for every node of the tree: foreach () O(n)
* To copy the tree - preorder: copytree () O(n)
* Safe to node deletion (but no dbsRemove, just
* root=NULL, cnt=0) - postorder: deltree () O(n)
* To a specific index: operator [i] O(n) ...(n/16)
* But for every node with operator[] total is ... n^2 CAREFUL!
* Inorder next/prev dbsNext, dbsPrev: O(log n)
* For a scroller area Use dbsNext, dbsPrev and keep a sample node
* if the tree is modified, reget the sample node from operator [].
*
*/
TX void dbsTree<dbsNode>::walk_tree (dbsNode *n, void (*foo)(dbsNode*))
{
foo (n);
if (n->less) walk_tree (n->less, foo);
if (n->more) walk_tree (n->more, foo);
}
TX void dbsTree<dbsNode>::walk_tree_io (dbsNode *n, void (*foo)(dbsNode*))
{
if (n->less) walk_tree_io (n->less, foo);
foo (n);
if (n->more) walk_tree_io (n->more, foo);
}
TX void dbsTree<dbsNode>::walk_tree_d (dbsNode *n, void (*foo)(dbsNode*))
{
if (n->less) walk_tree_d (n->less, foo);
if (n->more) walk_tree_d (n->more, foo);
foo (n);
}
TX void dbsTree<dbsNode>::copytree (void (*f)(dbsNode*))
{
if (root) walk_tree (root, f);
}
TX void dbsTree<dbsNode>::foreach (void (*f)(dbsNode*))
{
if (root) walk_tree_io (root, f);
}
TX void dbsTree<dbsNode>::deltree (void (*f)(dbsNode*))
{
if (root) walk_tree_d (root, f);
}
/*
* Indexed by inorder access
*/
#endif