1 | /*
|
---|
2 | * Copyright (C) 2008 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Based on Abstract AVL Tree Template v1.5 by Walt Karas
|
---|
5 | * <http://geocities.com/wkaras/gen_cpp/avl_tree.html>.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * 1. Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
|
---|
17 | * its contributors may be used to endorse or promote products derived
|
---|
18 | * from this software without specific prior written permission.
|
---|
19 | *
|
---|
20 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
|
---|
21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
23 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
|
---|
24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | */
|
---|
31 |
|
---|
32 | #ifndef KJS_AVL_TREE_H_
|
---|
33 | #define KJS_AVL_TREE_H_
|
---|
34 |
|
---|
35 | #include "Assertions.h"
|
---|
36 |
|
---|
37 | namespace JSC {
|
---|
38 |
|
---|
39 | // Here is the reference class for BSet.
|
---|
40 | //
|
---|
41 | // class BSet
|
---|
42 | // {
|
---|
43 | // public:
|
---|
44 | //
|
---|
45 | // class ANY_bitref
|
---|
46 | // {
|
---|
47 | // public:
|
---|
48 | // operator bool ();
|
---|
49 | // void operator = (bool b);
|
---|
50 | // };
|
---|
51 | //
|
---|
52 | // // Does not have to initialize bits.
|
---|
53 | // BSet();
|
---|
54 | //
|
---|
55 | // // Must return a valid value for index when 0 <= index < maxDepth
|
---|
56 | // ANY_bitref operator [] (unsigned index);
|
---|
57 | //
|
---|
58 | // // Set all bits to 1.
|
---|
59 | // void set();
|
---|
60 | //
|
---|
61 | // // Set all bits to 0.
|
---|
62 | // void reset();
|
---|
63 | // };
|
---|
64 |
|
---|
65 | template<unsigned maxDepth>
|
---|
66 | class AVLTreeDefaultBSet {
|
---|
67 | public:
|
---|
68 | bool& operator[](unsigned i) { ASSERT(i < maxDepth); return m_data[i]; }
|
---|
69 | void set() { for (unsigned i = 0; i < maxDepth; ++i) m_data[i] = true; }
|
---|
70 | void reset() { for (unsigned i = 0; i < maxDepth; ++i) m_data[i] = false; }
|
---|
71 |
|
---|
72 | private:
|
---|
73 | bool m_data[maxDepth];
|
---|
74 | };
|
---|
75 |
|
---|
76 | // How to determine maxDepth:
|
---|
77 | // d Minimum number of nodes
|
---|
78 | // 2 2
|
---|
79 | // 3 4
|
---|
80 | // 4 7
|
---|
81 | // 5 12
|
---|
82 | // 6 20
|
---|
83 | // 7 33
|
---|
84 | // 8 54
|
---|
85 | // 9 88
|
---|
86 | // 10 143
|
---|
87 | // 11 232
|
---|
88 | // 12 376
|
---|
89 | // 13 609
|
---|
90 | // 14 986
|
---|
91 | // 15 1,596
|
---|
92 | // 16 2,583
|
---|
93 | // 17 4,180
|
---|
94 | // 18 6,764
|
---|
95 | // 19 10,945
|
---|
96 | // 20 17,710
|
---|
97 | // 21 28,656
|
---|
98 | // 22 46,367
|
---|
99 | // 23 75,024
|
---|
100 | // 24 121,392
|
---|
101 | // 25 196,417
|
---|
102 | // 26 317,810
|
---|
103 | // 27 514,228
|
---|
104 | // 28 832,039
|
---|
105 | // 29 1,346,268
|
---|
106 | // 30 2,178,308
|
---|
107 | // 31 3,524,577
|
---|
108 | // 32 5,702,886
|
---|
109 | // 33 9,227,464
|
---|
110 | // 34 14,930,351
|
---|
111 | // 35 24,157,816
|
---|
112 | // 36 39,088,168
|
---|
113 | // 37 63,245,985
|
---|
114 | // 38 102,334,154
|
---|
115 | // 39 165,580,140
|
---|
116 | // 40 267,914,295
|
---|
117 | // 41 433,494,436
|
---|
118 | // 42 701,408,732
|
---|
119 | // 43 1,134,903,169
|
---|
120 | // 44 1,836,311,902
|
---|
121 | // 45 2,971,215,072
|
---|
122 | //
|
---|
123 | // E.g., if, in a particular instantiation, the maximum number of nodes in a tree instance is 1,000,000, the maximum depth should be 28.
|
---|
124 | // You pick 28 because MN(28) is 832,039, which is less than or equal to 1,000,000, and MN(29) is 1,346,268, which is strictly greater than 1,000,000.
|
---|
125 |
|
---|
126 | template <class Abstractor, unsigned maxDepth = 32, class BSet = AVLTreeDefaultBSet<maxDepth> >
|
---|
127 | class AVLTree {
|
---|
128 | public:
|
---|
129 |
|
---|
130 | typedef typename Abstractor::key key;
|
---|
131 | typedef typename Abstractor::handle handle;
|
---|
132 | typedef typename Abstractor::size size;
|
---|
133 |
|
---|
134 | enum SearchType {
|
---|
135 | EQUAL = 1,
|
---|
136 | LESS = 2,
|
---|
137 | GREATER = 4,
|
---|
138 | LESS_EQUAL = EQUAL | LESS,
|
---|
139 | GREATER_EQUAL = EQUAL | GREATER
|
---|
140 | };
|
---|
141 |
|
---|
142 |
|
---|
143 | Abstractor& abstractor() { return abs; }
|
---|
144 |
|
---|
145 | inline handle insert(handle h);
|
---|
146 |
|
---|
147 | inline handle search(key k, SearchType st = EQUAL);
|
---|
148 | inline handle search_least();
|
---|
149 | inline handle search_greatest();
|
---|
150 |
|
---|
151 | inline handle remove(key k);
|
---|
152 |
|
---|
153 | inline handle subst(handle new_node);
|
---|
154 |
|
---|
155 | void purge() { abs.root = null(); }
|
---|
156 |
|
---|
157 | bool is_empty() { return abs.root == null(); }
|
---|
158 |
|
---|
159 | AVLTree() { abs.root = null(); }
|
---|
160 |
|
---|
161 | class Iterator {
|
---|
162 | public:
|
---|
163 |
|
---|
164 | // Initialize depth to invalid value, to indicate iterator is
|
---|
165 | // invalid. (Depth is zero-base.)
|
---|
166 | Iterator() { depth = ~0U; }
|
---|
167 |
|
---|
168 | void start_iter(AVLTree &tree, key k, SearchType st = EQUAL)
|
---|
169 | {
|
---|
170 | // Mask of high bit in an int.
|
---|
171 | const int MASK_HIGH_BIT = (int) ~ ((~ (unsigned) 0) >> 1);
|
---|
172 |
|
---|
173 | // Save the tree that we're going to iterate through in a
|
---|
174 | // member variable.
|
---|
175 | tree_ = &tree;
|
---|
176 |
|
---|
177 | int cmp, target_cmp;
|
---|
178 | handle h = tree_->abs.root;
|
---|
179 | unsigned d = 0;
|
---|
180 |
|
---|
181 | depth = ~0U;
|
---|
182 |
|
---|
183 | if (h == null())
|
---|
184 | // Tree is empty.
|
---|
185 | return;
|
---|
186 |
|
---|
187 | if (st & LESS)
|
---|
188 | // Key can be greater than key of starting node.
|
---|
189 | target_cmp = 1;
|
---|
190 | else if (st & GREATER)
|
---|
191 | // Key can be less than key of starting node.
|
---|
192 | target_cmp = -1;
|
---|
193 | else
|
---|
194 | // Key must be same as key of starting node.
|
---|
195 | target_cmp = 0;
|
---|
196 |
|
---|
197 | for (;;) {
|
---|
198 | cmp = cmp_k_n(k, h);
|
---|
199 | if (cmp == 0) {
|
---|
200 | if (st & EQUAL) {
|
---|
201 | // Equal node was sought and found as starting node.
|
---|
202 | depth = d;
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | cmp = -target_cmp;
|
---|
206 | } else if (target_cmp != 0)
|
---|
207 | if (!((cmp ^ target_cmp) & MASK_HIGH_BIT))
|
---|
208 | // cmp and target_cmp are both negative or both positive.
|
---|
209 | depth = d;
|
---|
210 | h = cmp < 0 ? get_lt(h) : get_gt(h);
|
---|
211 | if (h == null())
|
---|
212 | break;
|
---|
213 | branch[d] = cmp > 0;
|
---|
214 | path_h[d++] = h;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | void start_iter_least(AVLTree &tree)
|
---|
219 | {
|
---|
220 | tree_ = &tree;
|
---|
221 |
|
---|
222 | handle h = tree_->abs.root;
|
---|
223 |
|
---|
224 | depth = ~0U;
|
---|
225 |
|
---|
226 | branch.reset();
|
---|
227 |
|
---|
228 | while (h != null()) {
|
---|
229 | if (depth != ~0U)
|
---|
230 | path_h[depth] = h;
|
---|
231 | depth++;
|
---|
232 | h = get_lt(h);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | void start_iter_greatest(AVLTree &tree)
|
---|
237 | {
|
---|
238 | tree_ = &tree;
|
---|
239 |
|
---|
240 | handle h = tree_->abs.root;
|
---|
241 |
|
---|
242 | depth = ~0U;
|
---|
243 |
|
---|
244 | branch.set();
|
---|
245 |
|
---|
246 | while (h != null()) {
|
---|
247 | if (depth != ~0U)
|
---|
248 | path_h[depth] = h;
|
---|
249 | depth++;
|
---|
250 | h = get_gt(h);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | handle operator*()
|
---|
255 | {
|
---|
256 | if (depth == ~0U)
|
---|
257 | return null();
|
---|
258 |
|
---|
259 | return depth == 0 ? tree_->abs.root : path_h[depth - 1];
|
---|
260 | }
|
---|
261 |
|
---|
262 | void operator++()
|
---|
263 | {
|
---|
264 | if (depth != ~0U) {
|
---|
265 | handle h = get_gt(**this);
|
---|
266 | if (h == null()) {
|
---|
267 | do {
|
---|
268 | if (depth == 0) {
|
---|
269 | depth = ~0U;
|
---|
270 | break;
|
---|
271 | }
|
---|
272 | depth--;
|
---|
273 | } while (branch[depth]);
|
---|
274 | } else {
|
---|
275 | branch[depth] = true;
|
---|
276 | path_h[depth++] = h;
|
---|
277 | for (;;) {
|
---|
278 | h = get_lt(h);
|
---|
279 | if (h == null())
|
---|
280 | break;
|
---|
281 | branch[depth] = false;
|
---|
282 | path_h[depth++] = h;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | void operator--()
|
---|
289 | {
|
---|
290 | if (depth != ~0U) {
|
---|
291 | handle h = get_lt(**this);
|
---|
292 | if (h == null())
|
---|
293 | do {
|
---|
294 | if (depth == 0) {
|
---|
295 | depth = ~0U;
|
---|
296 | break;
|
---|
297 | }
|
---|
298 | depth--;
|
---|
299 | } while (!branch[depth]);
|
---|
300 | else {
|
---|
301 | branch[depth] = false;
|
---|
302 | path_h[depth++] = h;
|
---|
303 | for (;;) {
|
---|
304 | h = get_gt(h);
|
---|
305 | if (h == null())
|
---|
306 | break;
|
---|
307 | branch[depth] = true;
|
---|
308 | path_h[depth++] = h;
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | void operator++(int) { ++(*this); }
|
---|
315 | void operator--(int) { --(*this); }
|
---|
316 |
|
---|
317 | protected:
|
---|
318 |
|
---|
319 | // Tree being iterated over.
|
---|
320 | AVLTree *tree_;
|
---|
321 |
|
---|
322 | // Records a path into the tree. If branch[n] is true, indicates
|
---|
323 | // take greater branch from the nth node in the path, otherwise
|
---|
324 | // take the less branch. branch[0] gives branch from root, and
|
---|
325 | // so on.
|
---|
326 | BSet branch;
|
---|
327 |
|
---|
328 | // Zero-based depth of path into tree.
|
---|
329 | unsigned depth;
|
---|
330 |
|
---|
331 | // Handles of nodes in path from root to current node (returned by *).
|
---|
332 | handle path_h[maxDepth - 1];
|
---|
333 |
|
---|
334 | int cmp_k_n(key k, handle h) { return tree_->abs.compare_key_node(k, h); }
|
---|
335 | int cmp_n_n(handle h1, handle h2) { return tree_->abs.compare_node_node(h1, h2); }
|
---|
336 | handle get_lt(handle h) { return tree_->abs.get_less(h); }
|
---|
337 | handle get_gt(handle h) { return tree_->abs.get_greater(h); }
|
---|
338 | handle null() { return tree_->abs.null(); }
|
---|
339 | };
|
---|
340 |
|
---|
341 | template<typename fwd_iter>
|
---|
342 | bool build(fwd_iter p, size num_nodes)
|
---|
343 | {
|
---|
344 | if (num_nodes == 0) {
|
---|
345 | abs.root = null();
|
---|
346 | return true;
|
---|
347 | }
|
---|
348 |
|
---|
349 | // Gives path to subtree being built. If branch[N] is false, branch
|
---|
350 | // less from the node at depth N, if true branch greater.
|
---|
351 | BSet branch;
|
---|
352 |
|
---|
353 | // If rem[N] is true, then for the current subtree at depth N, it's
|
---|
354 | // greater subtree has one more node than it's less subtree.
|
---|
355 | BSet rem;
|
---|
356 |
|
---|
357 | // Depth of root node of current subtree.
|
---|
358 | unsigned depth = 0;
|
---|
359 |
|
---|
360 | // Number of nodes in current subtree.
|
---|
361 | size num_sub = num_nodes;
|
---|
362 |
|
---|
363 | // The algorithm relies on a stack of nodes whose less subtree has
|
---|
364 | // been built, but whose right subtree has not yet been built. The
|
---|
365 | // stack is implemented as linked list. The nodes are linked
|
---|
366 | // together by having the "greater" handle of a node set to the
|
---|
367 | // next node in the list. "less_parent" is the handle of the first
|
---|
368 | // node in the list.
|
---|
369 | handle less_parent = null();
|
---|
370 |
|
---|
371 | // h is root of current subtree, child is one of its children.
|
---|
372 | handle h, child;
|
---|
373 |
|
---|
374 | for (;;) {
|
---|
375 | while (num_sub > 2) {
|
---|
376 | // Subtract one for root of subtree.
|
---|
377 | num_sub--;
|
---|
378 | rem[depth] = !!(num_sub & 1);
|
---|
379 | branch[depth++] = false;
|
---|
380 | num_sub >>= 1;
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (num_sub == 2) {
|
---|
384 | // Build a subtree with two nodes, slanting to greater.
|
---|
385 | // I arbitrarily chose to always have the extra node in the
|
---|
386 | // greater subtree when there is an odd number of nodes to
|
---|
387 | // split between the two subtrees.
|
---|
388 |
|
---|
389 | h = *p;
|
---|
390 | p++;
|
---|
391 | child = *p;
|
---|
392 | p++;
|
---|
393 | set_lt(child, null());
|
---|
394 | set_gt(child, null());
|
---|
395 | set_bf(child, 0);
|
---|
396 | set_gt(h, child);
|
---|
397 | set_lt(h, null());
|
---|
398 | set_bf(h, 1);
|
---|
399 | } else { // num_sub == 1
|
---|
400 | // Build a subtree with one node.
|
---|
401 |
|
---|
402 | h = *p;
|
---|
403 | p++;
|
---|
404 | set_lt(h, null());
|
---|
405 | set_gt(h, null());
|
---|
406 | set_bf(h, 0);
|
---|
407 | }
|
---|
408 |
|
---|
409 | while (depth) {
|
---|
410 | depth--;
|
---|
411 | if (!branch[depth])
|
---|
412 | // We've completed a less subtree.
|
---|
413 | break;
|
---|
414 |
|
---|
415 | // We've completed a greater subtree, so attach it to
|
---|
416 | // its parent (that is less than it). We pop the parent
|
---|
417 | // off the stack of less parents.
|
---|
418 | child = h;
|
---|
419 | h = less_parent;
|
---|
420 | less_parent = get_gt(h);
|
---|
421 | set_gt(h, child);
|
---|
422 | // num_sub = 2 * (num_sub - rem[depth]) + rem[depth] + 1
|
---|
423 | num_sub <<= 1;
|
---|
424 | num_sub += 1 - rem[depth];
|
---|
425 | if (num_sub & (num_sub - 1))
|
---|
426 | // num_sub is not a power of 2
|
---|
427 | set_bf(h, 0);
|
---|
428 | else
|
---|
429 | // num_sub is a power of 2
|
---|
430 | set_bf(h, 1);
|
---|
431 | }
|
---|
432 |
|
---|
433 | if (num_sub == num_nodes)
|
---|
434 | // We've completed the full tree.
|
---|
435 | break;
|
---|
436 |
|
---|
437 | // The subtree we've completed is the less subtree of the
|
---|
438 | // next node in the sequence.
|
---|
439 |
|
---|
440 | child = h;
|
---|
441 | h = *p;
|
---|
442 | p++;
|
---|
443 | set_lt(h, child);
|
---|
444 |
|
---|
445 | // Put h into stack of less parents.
|
---|
446 | set_gt(h, less_parent);
|
---|
447 | less_parent = h;
|
---|
448 |
|
---|
449 | // Proceed to creating greater than subtree of h.
|
---|
450 | branch[depth] = true;
|
---|
451 | num_sub += rem[depth++];
|
---|
452 |
|
---|
453 | } // end for (;;)
|
---|
454 |
|
---|
455 | abs.root = h;
|
---|
456 |
|
---|
457 | return true;
|
---|
458 | }
|
---|
459 |
|
---|
460 | protected:
|
---|
461 |
|
---|
462 | friend class Iterator;
|
---|
463 |
|
---|
464 | // Create a class whose sole purpose is to take advantage of
|
---|
465 | // the "empty member" optimization.
|
---|
466 | struct abs_plus_root : public Abstractor {
|
---|
467 | // The handle of the root element in the AVL tree.
|
---|
468 | handle root;
|
---|
469 | };
|
---|
470 |
|
---|
471 | abs_plus_root abs;
|
---|
472 |
|
---|
473 |
|
---|
474 | handle get_lt(handle h) { return abs.get_less(h); }
|
---|
475 | void set_lt(handle h, handle lh) { abs.set_less(h, lh); }
|
---|
476 |
|
---|
477 | handle get_gt(handle h) { return abs.get_greater(h); }
|
---|
478 | void set_gt(handle h, handle gh) { abs.set_greater(h, gh); }
|
---|
479 |
|
---|
480 | int get_bf(handle h) { return abs.get_balance_factor(h); }
|
---|
481 | void set_bf(handle h, int bf) { abs.set_balance_factor(h, bf); }
|
---|
482 |
|
---|
483 | int cmp_k_n(key k, handle h) { return abs.compare_key_node(k, h); }
|
---|
484 | int cmp_n_n(handle h1, handle h2) { return abs.compare_node_node(h1, h2); }
|
---|
485 |
|
---|
486 | handle null() { return abs.null(); }
|
---|
487 |
|
---|
488 | private:
|
---|
489 |
|
---|
490 | // Balances subtree, returns handle of root node of subtree
|
---|
491 | // after balancing.
|
---|
492 | handle balance(handle bal_h)
|
---|
493 | {
|
---|
494 | handle deep_h;
|
---|
495 |
|
---|
496 | // Either the "greater than" or the "less than" subtree of
|
---|
497 | // this node has to be 2 levels deeper (or else it wouldn't
|
---|
498 | // need balancing).
|
---|
499 |
|
---|
500 | if (get_bf(bal_h) > 0) {
|
---|
501 | // "Greater than" subtree is deeper.
|
---|
502 |
|
---|
503 | deep_h = get_gt(bal_h);
|
---|
504 |
|
---|
505 | if (get_bf(deep_h) < 0) {
|
---|
506 | handle old_h = bal_h;
|
---|
507 | bal_h = get_lt(deep_h);
|
---|
508 |
|
---|
509 | set_gt(old_h, get_lt(bal_h));
|
---|
510 | set_lt(deep_h, get_gt(bal_h));
|
---|
511 | set_lt(bal_h, old_h);
|
---|
512 | set_gt(bal_h, deep_h);
|
---|
513 |
|
---|
514 | int bf = get_bf(bal_h);
|
---|
515 | if (bf != 0) {
|
---|
516 | if (bf > 0) {
|
---|
517 | set_bf(old_h, -1);
|
---|
518 | set_bf(deep_h, 0);
|
---|
519 | } else {
|
---|
520 | set_bf(deep_h, 1);
|
---|
521 | set_bf(old_h, 0);
|
---|
522 | }
|
---|
523 | set_bf(bal_h, 0);
|
---|
524 | } else {
|
---|
525 | set_bf(old_h, 0);
|
---|
526 | set_bf(deep_h, 0);
|
---|
527 | }
|
---|
528 | } else {
|
---|
529 | set_gt(bal_h, get_lt(deep_h));
|
---|
530 | set_lt(deep_h, bal_h);
|
---|
531 | if (get_bf(deep_h) == 0) {
|
---|
532 | set_bf(deep_h, -1);
|
---|
533 | set_bf(bal_h, 1);
|
---|
534 | } else {
|
---|
535 | set_bf(deep_h, 0);
|
---|
536 | set_bf(bal_h, 0);
|
---|
537 | }
|
---|
538 | bal_h = deep_h;
|
---|
539 | }
|
---|
540 | } else {
|
---|
541 | // "Less than" subtree is deeper.
|
---|
542 |
|
---|
543 | deep_h = get_lt(bal_h);
|
---|
544 |
|
---|
545 | if (get_bf(deep_h) > 0) {
|
---|
546 | handle old_h = bal_h;
|
---|
547 | bal_h = get_gt(deep_h);
|
---|
548 | set_lt(old_h, get_gt(bal_h));
|
---|
549 | set_gt(deep_h, get_lt(bal_h));
|
---|
550 | set_gt(bal_h, old_h);
|
---|
551 | set_lt(bal_h, deep_h);
|
---|
552 |
|
---|
553 | int bf = get_bf(bal_h);
|
---|
554 | if (bf != 0) {
|
---|
555 | if (bf < 0) {
|
---|
556 | set_bf(old_h, 1);
|
---|
557 | set_bf(deep_h, 0);
|
---|
558 | } else {
|
---|
559 | set_bf(deep_h, -1);
|
---|
560 | set_bf(old_h, 0);
|
---|
561 | }
|
---|
562 | set_bf(bal_h, 0);
|
---|
563 | } else {
|
---|
564 | set_bf(old_h, 0);
|
---|
565 | set_bf(deep_h, 0);
|
---|
566 | }
|
---|
567 | } else {
|
---|
568 | set_lt(bal_h, get_gt(deep_h));
|
---|
569 | set_gt(deep_h, bal_h);
|
---|
570 | if (get_bf(deep_h) == 0) {
|
---|
571 | set_bf(deep_h, 1);
|
---|
572 | set_bf(bal_h, -1);
|
---|
573 | } else {
|
---|
574 | set_bf(deep_h, 0);
|
---|
575 | set_bf(bal_h, 0);
|
---|
576 | }
|
---|
577 | bal_h = deep_h;
|
---|
578 | }
|
---|
579 | }
|
---|
580 |
|
---|
581 | return bal_h;
|
---|
582 | }
|
---|
583 |
|
---|
584 | };
|
---|
585 |
|
---|
586 | template <class Abstractor, unsigned maxDepth, class BSet>
|
---|
587 | inline typename AVLTree<Abstractor, maxDepth, BSet>::handle
|
---|
588 | AVLTree<Abstractor, maxDepth, BSet>::insert(handle h)
|
---|
589 | {
|
---|
590 | set_lt(h, null());
|
---|
591 | set_gt(h, null());
|
---|
592 | set_bf(h, 0);
|
---|
593 |
|
---|
594 | if (abs.root == null())
|
---|
595 | abs.root = h;
|
---|
596 | else {
|
---|
597 | // Last unbalanced node encountered in search for insertion point.
|
---|
598 | handle unbal = null();
|
---|
599 | // Parent of last unbalanced node.
|
---|
600 | handle parent_unbal = null();
|
---|
601 | // Balance factor of last unbalanced node.
|
---|
602 | int unbal_bf;
|
---|
603 |
|
---|
604 | // Zero-based depth in tree.
|
---|
605 | unsigned depth = 0, unbal_depth = 0;
|
---|
606 |
|
---|
607 | // Records a path into the tree. If branch[n] is true, indicates
|
---|
608 | // take greater branch from the nth node in the path, otherwise
|
---|
609 | // take the less branch. branch[0] gives branch from root, and
|
---|
610 | // so on.
|
---|
611 | BSet branch;
|
---|
612 |
|
---|
613 | handle hh = abs.root;
|
---|
614 | handle parent = null();
|
---|
615 | int cmp;
|
---|
616 |
|
---|
617 | do {
|
---|
618 | if (get_bf(hh) != 0) {
|
---|
619 | unbal = hh;
|
---|
620 | parent_unbal = parent;
|
---|
621 | unbal_depth = depth;
|
---|
622 | }
|
---|
623 | cmp = cmp_n_n(h, hh);
|
---|
624 | if (cmp == 0)
|
---|
625 | // Duplicate key.
|
---|
626 | return hh;
|
---|
627 | parent = hh;
|
---|
628 | hh = cmp < 0 ? get_lt(hh) : get_gt(hh);
|
---|
629 | branch[depth++] = cmp > 0;
|
---|
630 | } while (hh != null());
|
---|
631 |
|
---|
632 | // Add node to insert as leaf of tree.
|
---|
633 | if (cmp < 0)
|
---|
634 | set_lt(parent, h);
|
---|
635 | else
|
---|
636 | set_gt(parent, h);
|
---|
637 |
|
---|
638 | depth = unbal_depth;
|
---|
639 |
|
---|
640 | if (unbal == null())
|
---|
641 | hh = abs.root;
|
---|
642 | else {
|
---|
643 | cmp = branch[depth++] ? 1 : -1;
|
---|
644 | unbal_bf = get_bf(unbal);
|
---|
645 | if (cmp < 0)
|
---|
646 | unbal_bf--;
|
---|
647 | else // cmp > 0
|
---|
648 | unbal_bf++;
|
---|
649 | hh = cmp < 0 ? get_lt(unbal) : get_gt(unbal);
|
---|
650 | if ((unbal_bf != -2) && (unbal_bf != 2)) {
|
---|
651 | // No rebalancing of tree is necessary.
|
---|
652 | set_bf(unbal, unbal_bf);
|
---|
653 | unbal = null();
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | if (hh != null())
|
---|
658 | while (h != hh) {
|
---|
659 | cmp = branch[depth++] ? 1 : -1;
|
---|
660 | if (cmp < 0) {
|
---|
661 | set_bf(hh, -1);
|
---|
662 | hh = get_lt(hh);
|
---|
663 | } else { // cmp > 0
|
---|
664 | set_bf(hh, 1);
|
---|
665 | hh = get_gt(hh);
|
---|
666 | }
|
---|
667 | }
|
---|
668 |
|
---|
669 | if (unbal != null()) {
|
---|
670 | unbal = balance(unbal);
|
---|
671 | if (parent_unbal == null())
|
---|
672 | abs.root = unbal;
|
---|
673 | else {
|
---|
674 | depth = unbal_depth - 1;
|
---|
675 | cmp = branch[depth] ? 1 : -1;
|
---|
676 | if (cmp < 0)
|
---|
677 | set_lt(parent_unbal, unbal);
|
---|
678 | else // cmp > 0
|
---|
679 | set_gt(parent_unbal, unbal);
|
---|
680 | }
|
---|
681 | }
|
---|
682 | }
|
---|
683 |
|
---|
684 | return h;
|
---|
685 | }
|
---|
686 |
|
---|
687 | template <class Abstractor, unsigned maxDepth, class BSet>
|
---|
688 | inline typename AVLTree<Abstractor, maxDepth, BSet>::handle
|
---|
689 | AVLTree<Abstractor, maxDepth, BSet>::search(key k, typename AVLTree<Abstractor, maxDepth, BSet>::SearchType st)
|
---|
690 | {
|
---|
691 | const int MASK_HIGH_BIT = (int) ~ ((~ (unsigned) 0) >> 1);
|
---|
692 |
|
---|
693 | int cmp, target_cmp;
|
---|
694 | handle match_h = null();
|
---|
695 | handle h = abs.root;
|
---|
696 |
|
---|
697 | if (st & LESS)
|
---|
698 | target_cmp = 1;
|
---|
699 | else if (st & GREATER)
|
---|
700 | target_cmp = -1;
|
---|
701 | else
|
---|
702 | target_cmp = 0;
|
---|
703 |
|
---|
704 | while (h != null()) {
|
---|
705 | cmp = cmp_k_n(k, h);
|
---|
706 | if (cmp == 0) {
|
---|
707 | if (st & EQUAL) {
|
---|
708 | match_h = h;
|
---|
709 | break;
|
---|
710 | }
|
---|
711 | cmp = -target_cmp;
|
---|
712 | } else if (target_cmp != 0)
|
---|
713 | if (!((cmp ^ target_cmp) & MASK_HIGH_BIT))
|
---|
714 | // cmp and target_cmp are both positive or both negative.
|
---|
715 | match_h = h;
|
---|
716 | h = cmp < 0 ? get_lt(h) : get_gt(h);
|
---|
717 | }
|
---|
718 |
|
---|
719 | return match_h;
|
---|
720 | }
|
---|
721 |
|
---|
722 | template <class Abstractor, unsigned maxDepth, class BSet>
|
---|
723 | inline typename AVLTree<Abstractor, maxDepth, BSet>::handle
|
---|
724 | AVLTree<Abstractor, maxDepth, BSet>::search_least()
|
---|
725 | {
|
---|
726 | handle h = abs.root, parent = null();
|
---|
727 |
|
---|
728 | while (h != null()) {
|
---|
729 | parent = h;
|
---|
730 | h = get_lt(h);
|
---|
731 | }
|
---|
732 |
|
---|
733 | return parent;
|
---|
734 | }
|
---|
735 |
|
---|
736 | template <class Abstractor, unsigned maxDepth, class BSet>
|
---|
737 | inline typename AVLTree<Abstractor, maxDepth, BSet>::handle
|
---|
738 | AVLTree<Abstractor, maxDepth, BSet>::search_greatest()
|
---|
739 | {
|
---|
740 | handle h = abs.root, parent = null();
|
---|
741 |
|
---|
742 | while (h != null()) {
|
---|
743 | parent = h;
|
---|
744 | h = get_gt(h);
|
---|
745 | }
|
---|
746 |
|
---|
747 | return parent;
|
---|
748 | }
|
---|
749 |
|
---|
750 | template <class Abstractor, unsigned maxDepth, class BSet>
|
---|
751 | inline typename AVLTree<Abstractor, maxDepth, BSet>::handle
|
---|
752 | AVLTree<Abstractor, maxDepth, BSet>::remove(key k)
|
---|
753 | {
|
---|
754 | // Zero-based depth in tree.
|
---|
755 | unsigned depth = 0, rm_depth;
|
---|
756 |
|
---|
757 | // Records a path into the tree. If branch[n] is true, indicates
|
---|
758 | // take greater branch from the nth node in the path, otherwise
|
---|
759 | // take the less branch. branch[0] gives branch from root, and
|
---|
760 | // so on.
|
---|
761 | BSet branch;
|
---|
762 |
|
---|
763 | handle h = abs.root;
|
---|
764 | handle parent = null(), child;
|
---|
765 | int cmp, cmp_shortened_sub_with_path;
|
---|
766 |
|
---|
767 | for (;;) {
|
---|
768 | if (h == null())
|
---|
769 | // No node in tree with given key.
|
---|
770 | return null();
|
---|
771 | cmp = cmp_k_n(k, h);
|
---|
772 | if (cmp == 0)
|
---|
773 | // Found node to remove.
|
---|
774 | break;
|
---|
775 | parent = h;
|
---|
776 | h = cmp < 0 ? get_lt(h) : get_gt(h);
|
---|
777 | branch[depth++] = cmp > 0;
|
---|
778 | cmp_shortened_sub_with_path = cmp;
|
---|
779 | }
|
---|
780 | handle rm = h;
|
---|
781 | handle parent_rm = parent;
|
---|
782 | rm_depth = depth;
|
---|
783 |
|
---|
784 | // If the node to remove is not a leaf node, we need to get a
|
---|
785 | // leaf node, or a node with a single leaf as its child, to put
|
---|
786 | // in the place of the node to remove. We will get the greatest
|
---|
787 | // node in the less subtree (of the node to remove), or the least
|
---|
788 | // node in the greater subtree. We take the leaf node from the
|
---|
789 | // deeper subtree, if there is one.
|
---|
790 |
|
---|
791 | if (get_bf(h) < 0) {
|
---|
792 | child = get_lt(h);
|
---|
793 | branch[depth] = false;
|
---|
794 | cmp = -1;
|
---|
795 | } else {
|
---|
796 | child = get_gt(h);
|
---|
797 | branch[depth] = true;
|
---|
798 | cmp = 1;
|
---|
799 | }
|
---|
800 | depth++;
|
---|
801 |
|
---|
802 | if (child != null()) {
|
---|
803 | cmp = -cmp;
|
---|
804 | do {
|
---|
805 | parent = h;
|
---|
806 | h = child;
|
---|
807 | if (cmp < 0) {
|
---|
808 | child = get_lt(h);
|
---|
809 | branch[depth] = false;
|
---|
810 | } else {
|
---|
811 | child = get_gt(h);
|
---|
812 | branch[depth] = true;
|
---|
813 | }
|
---|
814 | depth++;
|
---|
815 | } while (child != null());
|
---|
816 |
|
---|
817 | if (parent == rm)
|
---|
818 | // Only went through do loop once. Deleted node will be replaced
|
---|
819 | // in the tree structure by one of its immediate children.
|
---|
820 | cmp_shortened_sub_with_path = -cmp;
|
---|
821 | else
|
---|
822 | cmp_shortened_sub_with_path = cmp;
|
---|
823 |
|
---|
824 | // Get the handle of the opposite child, which may not be null.
|
---|
825 | child = cmp > 0 ? get_lt(h) : get_gt(h);
|
---|
826 | }
|
---|
827 |
|
---|
828 | if (parent == null())
|
---|
829 | // There were only 1 or 2 nodes in this tree.
|
---|
830 | abs.root = child;
|
---|
831 | else if (cmp_shortened_sub_with_path < 0)
|
---|
832 | set_lt(parent, child);
|
---|
833 | else
|
---|
834 | set_gt(parent, child);
|
---|
835 |
|
---|
836 | // "path" is the parent of the subtree being eliminated or reduced
|
---|
837 | // from a depth of 2 to 1. If "path" is the node to be removed, we
|
---|
838 | // set path to the node we're about to poke into the position of the
|
---|
839 | // node to be removed.
|
---|
840 | handle path = parent == rm ? h : parent;
|
---|
841 |
|
---|
842 | if (h != rm) {
|
---|
843 | // Poke in the replacement for the node to be removed.
|
---|
844 | set_lt(h, get_lt(rm));
|
---|
845 | set_gt(h, get_gt(rm));
|
---|
846 | set_bf(h, get_bf(rm));
|
---|
847 | if (parent_rm == null())
|
---|
848 | abs.root = h;
|
---|
849 | else {
|
---|
850 | depth = rm_depth - 1;
|
---|
851 | if (branch[depth])
|
---|
852 | set_gt(parent_rm, h);
|
---|
853 | else
|
---|
854 | set_lt(parent_rm, h);
|
---|
855 | }
|
---|
856 | }
|
---|
857 |
|
---|
858 | if (path != null()) {
|
---|
859 | // Create a temporary linked list from the parent of the path node
|
---|
860 | // to the root node.
|
---|
861 | h = abs.root;
|
---|
862 | parent = null();
|
---|
863 | depth = 0;
|
---|
864 | while (h != path) {
|
---|
865 | if (branch[depth++]) {
|
---|
866 | child = get_gt(h);
|
---|
867 | set_gt(h, parent);
|
---|
868 | } else {
|
---|
869 | child = get_lt(h);
|
---|
870 | set_lt(h, parent);
|
---|
871 | }
|
---|
872 | parent = h;
|
---|
873 | h = child;
|
---|
874 | }
|
---|
875 |
|
---|
876 | // Climb from the path node to the root node using the linked
|
---|
877 | // list, restoring the tree structure and rebalancing as necessary.
|
---|
878 | bool reduced_depth = true;
|
---|
879 | int bf;
|
---|
880 | cmp = cmp_shortened_sub_with_path;
|
---|
881 | for (;;) {
|
---|
882 | if (reduced_depth) {
|
---|
883 | bf = get_bf(h);
|
---|
884 | if (cmp < 0)
|
---|
885 | bf++;
|
---|
886 | else // cmp > 0
|
---|
887 | bf--;
|
---|
888 | if ((bf == -2) || (bf == 2)) {
|
---|
889 | h = balance(h);
|
---|
890 | bf = get_bf(h);
|
---|
891 | } else
|
---|
892 | set_bf(h, bf);
|
---|
893 | reduced_depth = (bf == 0);
|
---|
894 | }
|
---|
895 | if (parent == null())
|
---|
896 | break;
|
---|
897 | child = h;
|
---|
898 | h = parent;
|
---|
899 | cmp = branch[--depth] ? 1 : -1;
|
---|
900 | if (cmp < 0) {
|
---|
901 | parent = get_lt(h);
|
---|
902 | set_lt(h, child);
|
---|
903 | } else {
|
---|
904 | parent = get_gt(h);
|
---|
905 | set_gt(h, child);
|
---|
906 | }
|
---|
907 | }
|
---|
908 | abs.root = h;
|
---|
909 | }
|
---|
910 |
|
---|
911 | return rm;
|
---|
912 | }
|
---|
913 |
|
---|
914 | template <class Abstractor, unsigned maxDepth, class BSet>
|
---|
915 | inline typename AVLTree<Abstractor, maxDepth, BSet>::handle
|
---|
916 | AVLTree<Abstractor, maxDepth, BSet>::subst(handle new_node)
|
---|
917 | {
|
---|
918 | handle h = abs.root;
|
---|
919 | handle parent = null();
|
---|
920 | int cmp, last_cmp;
|
---|
921 |
|
---|
922 | /* Search for node already in tree with same key. */
|
---|
923 | for (;;) {
|
---|
924 | if (h == null())
|
---|
925 | /* No node in tree with same key as new node. */
|
---|
926 | return null();
|
---|
927 | cmp = cmp_n_n(new_node, h);
|
---|
928 | if (cmp == 0)
|
---|
929 | /* Found the node to substitute new one for. */
|
---|
930 | break;
|
---|
931 | last_cmp = cmp;
|
---|
932 | parent = h;
|
---|
933 | h = cmp < 0 ? get_lt(h) : get_gt(h);
|
---|
934 | }
|
---|
935 |
|
---|
936 | /* Copy tree housekeeping fields from node in tree to new node. */
|
---|
937 | set_lt(new_node, get_lt(h));
|
---|
938 | set_gt(new_node, get_gt(h));
|
---|
939 | set_bf(new_node, get_bf(h));
|
---|
940 |
|
---|
941 | if (parent == null())
|
---|
942 | /* New node is also new root. */
|
---|
943 | abs.root = new_node;
|
---|
944 | else {
|
---|
945 | /* Make parent point to new node. */
|
---|
946 | if (last_cmp < 0)
|
---|
947 | set_lt(parent, new_node);
|
---|
948 | else
|
---|
949 | set_gt(parent, new_node);
|
---|
950 | }
|
---|
951 |
|
---|
952 | return h;
|
---|
953 | }
|
---|
954 |
|
---|
955 | }
|
---|
956 |
|
---|
957 | #endif
|
---|