AVLTree.h 28 KB

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