Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1146 lines
32 KiB

  1. /*++
  2. Copyright (c) 1993-1999 Microsoft Corporation
  3. Module Name:
  4. avl.h
  5. Abstract:
  6. AVL tree template class implementation
  7. Author:
  8. Bill Bolosky [bolosky] 1993
  9. Revision History:
  10. --*/
  11. enum AVLBalance {
  12. AVLNew, // Not yet inserted in a tree
  13. AVLLeft, // Left side is one deeper than the right
  14. AVLBalanced, // Left and right sides are evenly balanced
  15. AVLRight, // Right side is one deeper than left
  16. };
  17. template<class elementClass> class AVLElement;
  18. template<class elementClass> class AVLTree {
  19. public:
  20. AVLTree(
  21. unsigned preallocateSize = 0);
  22. ~AVLTree(void);
  23. elementClass *findFirstLessThanOrEqualTo(
  24. elementClass *element);
  25. elementClass *findFirstGreaterThan(
  26. elementClass *element);
  27. elementClass *findFirstGreaterThanOrEqualTo(
  28. elementClass *element);
  29. elementClass *findMin(void);
  30. elementClass *findMax(void);
  31. int empty(void);
  32. unsigned size(void);
  33. void check(void);
  34. BOOLEAN insert(
  35. elementClass *element);
  36. void remove(
  37. elementClass *element);
  38. void dumpPoolStats(void);
  39. private:
  40. AVLElement<elementClass> *tree;
  41. Pool *avlElementPool;
  42. unsigned insertions;
  43. unsigned deletions;
  44. unsigned singleRotations;
  45. unsigned doubleRotations;
  46. friend class AVLElement<elementClass>;
  47. };
  48. // The AVLElement class would normally be declared in the avl.cpp file, except that because it's
  49. // a template, it needs to be in the header file. It can only be accessed (including creation and
  50. // destruction) by the AVLTree friend class.
  51. template<class elementClass> class AVLElement {
  52. private:
  53. AVLElement(void);
  54. ~AVLElement(void);
  55. void initialize(void);
  56. void insert(
  57. AVLTree<elementClass> *intoTree,
  58. elementClass *element);
  59. void remove(
  60. AVLTree<elementClass> *fromTree);
  61. unsigned checkAndReturnDepth(
  62. unsigned *countedElements);
  63. int inTree(void);
  64. int operator<=(
  65. AVLElement<elementClass> *peer);
  66. int operator<(
  67. AVLElement<elementClass> *peer);
  68. int operator==(
  69. AVLElement<elementClass> *peer);
  70. int operator>=(
  71. AVLElement<elementClass> *peer);
  72. int operator>(
  73. AVLElement<elementClass> *peer);
  74. AVLElement<elementClass>
  75. *findFirstLessThanOrEqualTo(
  76. elementClass *element);
  77. AVLElement<elementClass>
  78. *findFirstGreaterThan(
  79. elementClass *element);
  80. AVLElement<elementClass>
  81. *findFirstGreaterThanOrEqualTo(
  82. elementClass *element);
  83. void rightAdded(
  84. AVLTree<elementClass> *tree);
  85. void leftAdded(
  86. AVLTree<elementClass> *tree);
  87. void singleRotate(
  88. AVLTree<elementClass> *tree,
  89. AVLElement<elementClass> *child,
  90. AVLBalance whichSide);
  91. void doubleRotate(
  92. AVLTree<elementClass> *tree,
  93. AVLElement<elementClass> *child,
  94. AVLElement<elementClass> *grandchild,
  95. AVLBalance whichSide);
  96. void gotOneShorter(
  97. AVLTree<elementClass> *tree,
  98. AVLBalance whichSide);
  99. AVLBalance balance;
  100. AVLElement<elementClass> *left;
  101. AVLElement<elementClass> *right;
  102. AVLElement<elementClass> *parent;
  103. elementClass *element;
  104. friend class AVLTree<elementClass>;
  105. };
  106. template<class elementClass> elementClass *
  107. AVLTree<elementClass>::findFirstLessThanOrEqualTo(
  108. elementClass *element)
  109. {
  110. assert(element);
  111. if (!tree)
  112. return(NULL);
  113. AVLElement<elementClass> *avlElement = tree->findFirstLessThanOrEqualTo(element);
  114. if (avlElement) {
  115. return(avlElement->element);
  116. } else {
  117. return(NULL);
  118. }
  119. }
  120. template<class elementClass>
  121. AVLTree<elementClass>::AVLTree(
  122. unsigned preallocateSize)
  123. {
  124. tree = NULL;
  125. insertions = deletions = singleRotations = doubleRotations = 0;
  126. avlElementPool = new Pool(sizeof(AVLElement<elementClass>));
  127. if (preallocateSize && (NULL != avlElementPool)) {
  128. avlElementPool->preAllocate(preallocateSize);
  129. }
  130. }
  131. template<class elementClass> AVLTree<elementClass>::~AVLTree(void)
  132. {
  133. assert(tree == NULL);
  134. if (NULL != avlElementPool) {
  135. delete avlElementPool;
  136. }
  137. }
  138. //****************************************************************************
  139. //* *
  140. //* Function: findFirstLessThanOrEqualTo *
  141. //* *
  142. //* Syntax: AVLElement * findFirstLessThanOrEqualTo( *
  143. //* elementClass * element) *
  144. //* *
  145. //* Input: elementClass * element: *
  146. //* A pointer to an element to compare against while searching. *
  147. //* *
  148. //* Output: AVLElement *: *
  149. //* The element in the tree that has a value less than or equal *
  150. //* to the one specified, or NULL on failure. *
  151. //* *
  152. //* Synopsis: This function finds the element in the tree that has a value *
  153. //* less than or equal to the one specified. *
  154. //* *
  155. //****************************************************************************
  156. template<class elementClass> AVLElement<elementClass> *
  157. AVLElement<elementClass>::findFirstLessThanOrEqualTo(elementClass * element)
  158. {
  159. AVLElement<elementClass> * retVal = NULL;
  160. if (*this->element == element) {
  161. // We have a direct match (equal to). It takes precidence over the
  162. // "first less than" part.
  163. return this;
  164. }
  165. if (*this->element < element) {
  166. // The current element is smaller than the one specified.
  167. // This might be it, but try to find a bigger one.
  168. if (right != NULL) {
  169. retVal = right->findFirstLessThanOrEqualTo(element);
  170. }
  171. // If nothing below us (to the right) was found, then we are the
  172. // next smallest one.
  173. if (retVal == NULL) {
  174. return this;
  175. }
  176. else {
  177. return retVal;
  178. }
  179. }
  180. else {
  181. // The current element is bigger than the one specified.
  182. // We have to find a smaller one.
  183. if (left != NULL) {
  184. return left->findFirstLessThanOrEqualTo(element);
  185. }
  186. else {
  187. return NULL;
  188. }
  189. }
  190. }
  191. template<class elementClass> elementClass *
  192. AVLTree<elementClass>::findFirstGreaterThan(
  193. elementClass *element)
  194. {
  195. assert(element);
  196. if (!tree)
  197. return(NULL);
  198. AVLElement<elementClass> *avlElement = tree->findFirstGreaterThan(element);
  199. if (avlElement) {
  200. return(avlElement->element);
  201. } else {
  202. return(NULL);
  203. }
  204. }
  205. //****************************************************************************
  206. //* *
  207. //* Function: findFirstGreaterThan *
  208. //* *
  209. //* Syntax: AVLElement * findFirstGreaterThan(elementClass * element) *
  210. //* *
  211. //* Input: elementClass * element: *
  212. //* A pointer to an element to compare against while searching. *
  213. //* *
  214. //* Output: AVLElement *: *
  215. //* The element in the tree that has a vlaue greater than the *
  216. //* one specified, or NULL on failure. *
  217. //* *
  218. //* Synopsis: This function finds the element in the tree that has a value *
  219. //* greater than the one specified. *
  220. //* *
  221. //****************************************************************************
  222. template<class elementClass> AVLElement<elementClass> *
  223. AVLElement<elementClass>::findFirstGreaterThan(elementClass * element)
  224. {
  225. AVLElement<elementClass> * retVal = NULL;
  226. if (*this->element > element) {
  227. // The current element is bigger than the one specified.
  228. // This might be it, but try to find a smaller one.
  229. if (left != NULL) {
  230. retVal = left->findFirstGreaterThan(element);
  231. }
  232. // If nothing below us (to the left) was found, then we are the
  233. // next biggest one.
  234. if (retVal == NULL) {
  235. return this;
  236. }
  237. else {
  238. return retVal;
  239. }
  240. }
  241. else {
  242. // The current element is smaller than (or equal) the one specified.
  243. // We have to find a bigger one.
  244. if (right != NULL) {
  245. return right->findFirstGreaterThan(element);
  246. }
  247. else {
  248. return NULL;
  249. }
  250. }
  251. }
  252. template<class elementClass> elementClass *
  253. AVLTree<elementClass>::findFirstGreaterThanOrEqualTo(
  254. elementClass *element)
  255. {
  256. assert(element);
  257. if (!tree)
  258. return(NULL);
  259. AVLElement<elementClass> *avlElement = tree->findFirstGreaterThanOrEqualTo(element);
  260. if (avlElement) {
  261. return(avlElement->element);
  262. } else {
  263. return(NULL);
  264. }
  265. }
  266. //****************************************************************************
  267. //* *
  268. //* Function: findFirstGreaterThanOrEqualTo *
  269. //* *
  270. //* Syntax: AVLElement * findFirstGreaterThanOrEqualTo(elementClass * element)
  271. //* *
  272. //* Input: elementClass * element: *
  273. //* A pointer to an element to compare against while searching. *
  274. //* *
  275. //* Output: AVLElement *: *
  276. //* The element in the tree that has a vlaue greater than or *
  277. //* equal to the one specified, or NULL on failure. *
  278. //* *
  279. //* Synopsis: This function finds the element in the tree that has a value *
  280. //* greater than or equal to the one specified. *
  281. //* *
  282. //****************************************************************************
  283. template<class elementClass> AVLElement<elementClass> *
  284. AVLElement<elementClass>::findFirstGreaterThanOrEqualTo(elementClass * element)
  285. {
  286. if (*this->element == element) {
  287. // We have a direct match (equal to). It takes precidence over the
  288. // "first less than" part.
  289. return this;
  290. }
  291. AVLElement<elementClass> * retVal = NULL;
  292. if (*this->element > element) {
  293. // The current element is bigger than the one specified.
  294. // This might be it, but try to find a smaller one.
  295. if (left != NULL) {
  296. retVal = left->findFirstGreaterThanOrEqualTo(element);
  297. }
  298. // If nothing below us (to the left) was found, then we are the
  299. // next biggest one.
  300. if (retVal == NULL) {
  301. return this;
  302. } else {
  303. return retVal;
  304. }
  305. } else {
  306. // The current element is strictly smaller than the one specified.
  307. // We have to find a bigger one.
  308. if (right != NULL) {
  309. return right->findFirstGreaterThanOrEqualTo(element);
  310. } else {
  311. return NULL;
  312. }
  313. }
  314. }
  315. template<class elementClass> int
  316. AVLTree<elementClass>::empty(void)
  317. {
  318. assert((tree == NULL) == (insertions == deletions));
  319. return(tree == NULL);
  320. }
  321. template<class elementClass> unsigned
  322. AVLTree<elementClass>::size(void)
  323. {
  324. assert(insertions >= deletions);
  325. assert((tree == NULL) == (insertions == deletions));
  326. return(insertions - deletions);
  327. }
  328. template<class elementClass> elementClass *
  329. AVLTree<elementClass>::findMin(void)
  330. {
  331. if (!tree) {
  332. return(NULL);
  333. }
  334. AVLElement<elementClass> *candidate = tree;
  335. while (candidate->left) {
  336. assert(*candidate->left->element <= candidate->element);
  337. candidate = candidate->left;
  338. }
  339. return(candidate->element);
  340. }
  341. template<class elementClass> elementClass *
  342. AVLTree<elementClass>::findMax(void)
  343. {
  344. if (!tree) {
  345. return(NULL);
  346. }
  347. AVLElement<elementClass> *candidate = tree;
  348. while (candidate->right) {
  349. assert(*candidate->right->element >= candidate->element);
  350. candidate = candidate->right;
  351. }
  352. return(candidate->element);
  353. }
  354. template<class elementClass> void
  355. AVLTree<elementClass>::check(void)
  356. {
  357. AVLElement<elementClass> * currElement = NULL;
  358. AVLElement<elementClass> * nextElement = NULL;
  359. AVLElement<elementClass> * oldElement = NULL;
  360. unsigned countedElements = 0;
  361. if (tree) {
  362. assert(tree->parent == NULL);
  363. unsigned overallDepth = tree->checkAndReturnDepth(&countedElements);
  364. }
  365. assert(insertions-deletions == countedElements);
  366. // Check every element in the tree for consistance by verifying that it is in
  367. // the expected order. If not, it is most likely that the element's operators
  368. // are not behaving as needed.
  369. for(currElement = tree; currElement != NULL; currElement = nextElement) {
  370. // Go left if we can (and have not already been here).
  371. if (currElement->left && oldElement == currElement->parent) {
  372. nextElement = currElement->left;
  373. assert(*nextElement < currElement && "The < operator appears to be broken");
  374. assert(*currElement > nextElement && "The > operator appears to be broken");
  375. assert(!(*nextElement == currElement) && "The == operator appears to be broken");
  376. }
  377. // Otherwise go right if we can (and have not already been here).
  378. else if (currElement->right &&
  379. (oldElement == currElement->left || oldElement == currElement->parent)) {
  380. nextElement = currElement->right;
  381. assert(*nextElement > currElement && "The > operator appears to be broken");
  382. assert(*currElement < nextElement && "The < operator appears to be broken");
  383. assert(!(*nextElement == currElement) && "The == operator appears to be broken");
  384. }
  385. // We are done below us, go up a node.
  386. else {
  387. nextElement = currElement->parent;
  388. }
  389. oldElement = currElement;
  390. assert(*oldElement == currElement && "The == operator appears to be broken");
  391. }
  392. }
  393. template<class elementClass>
  394. AVLElement<elementClass>::AVLElement(void)
  395. {
  396. balance = AVLNew;
  397. left = right = parent = NULL;
  398. }
  399. template<class elementClass>
  400. AVLElement<elementClass>::~AVLElement(void)
  401. {
  402. assert(balance == AVLNew);
  403. assert(left == NULL && right == NULL && parent == NULL);
  404. }
  405. template<class elementClass> unsigned
  406. AVLElement<elementClass>::checkAndReturnDepth(
  407. unsigned *countedElements)
  408. {
  409. // We've been inserted and not deleted
  410. assert(balance != AVLNew);
  411. (*countedElements)++;
  412. // Assert that the links all match up.
  413. assert(!left || left->parent == this);
  414. assert(!right || right->parent == this);
  415. // The basic binary tree ordering property applies
  416. assert(!right || *this <= right);
  417. assert(!left || *this >= left);
  418. // The AVL balance property applies
  419. unsigned leftDepth;
  420. if (left) {
  421. leftDepth = left->checkAndReturnDepth(countedElements);
  422. } else {
  423. leftDepth = 0;
  424. }
  425. unsigned rightDepth;
  426. if (right) {
  427. rightDepth = right->checkAndReturnDepth(countedElements);
  428. } else {
  429. rightDepth = 0;
  430. }
  431. if (leftDepth == rightDepth) {
  432. assert(balance == AVLBalanced);
  433. return(leftDepth + 1);
  434. }
  435. if (leftDepth == rightDepth + 1) {
  436. assert(balance == AVLLeft);
  437. return(leftDepth + 1);
  438. }
  439. if (leftDepth + 1 == rightDepth) {
  440. assert(balance == AVLRight);
  441. return(rightDepth + 1);
  442. }
  443. assert(!"AVL Tree out of balance");
  444. return(0);
  445. }
  446. template<class elementClass> void
  447. AVLElement<elementClass>::insert(
  448. AVLTree<elementClass> *intoTree,
  449. elementClass *element)
  450. {
  451. assert(intoTree);
  452. assert(left == NULL && right == NULL && parent == NULL);
  453. this->element = element;
  454. assert(this->element);
  455. intoTree->insertions++;
  456. // Special case the empty tree case.
  457. if (intoTree->tree == NULL) {
  458. intoTree->tree = this;
  459. balance = AVLBalanced;
  460. // We already know all of the links are NULL, which is correct for this case.
  461. return;
  462. }
  463. // Find the leaf position at which to do this insertion.
  464. AVLElement *currentNode = intoTree->tree;
  465. AVLElement *previousNode;
  466. while (currentNode) {
  467. previousNode = currentNode;
  468. if (*currentNode < this) {
  469. currentNode = currentNode->right;
  470. } else if (*currentNode > this) {
  471. currentNode = currentNode->left;
  472. } else {
  473. // An AVL tree gets all whacky if you try to insert duplicate values.
  474. assert(!"Trying to insert a duplicate item. Use something other than an AVL tree.");
  475. }
  476. }
  477. balance = AVLBalanced;
  478. parent = previousNode;
  479. assert(parent);
  480. if (*previousNode <= this) {
  481. assert(!previousNode->right);
  482. previousNode->right = this;
  483. previousNode->rightAdded(intoTree);
  484. // intoTree->check();
  485. } else {
  486. assert(!previousNode->left);
  487. previousNode->left = this;
  488. previousNode->leftAdded(intoTree);
  489. // intoTree->check();
  490. }
  491. }
  492. template<class elementClass> void
  493. AVLElement<elementClass>::rightAdded(
  494. AVLTree<elementClass> *tree)
  495. {
  496. //We've just gotten one deeper on our right side.
  497. assert(balance != AVLNew);
  498. if (balance == AVLLeft) {
  499. balance = AVLBalanced;
  500. // The depth of the subtree rooted here hasn't changed, we're done
  501. return;
  502. }
  503. if (balance == AVLBalanced) {
  504. // We've just gotten one deeper, but are still balanced. Update and recurse up the
  505. // tree.
  506. balance = AVLRight;
  507. if (parent) {
  508. if (parent->right == this) {
  509. parent->rightAdded(tree);
  510. } else {
  511. assert(parent->left == this);
  512. parent->leftAdded(tree);
  513. }
  514. }
  515. return;
  516. }
  517. assert(balance == AVLRight);
  518. // We've just gone to double right (ie, out of balance).
  519. assert(right);
  520. if (right->balance == AVLRight) {
  521. singleRotate(tree,right,AVLRight);
  522. } else {
  523. assert(right->balance == AVLLeft); // Else we shouldn't have been AVLRight before the call
  524. doubleRotate(tree,right,right->left,AVLRight);
  525. }
  526. }
  527. template<class elementClass> void
  528. AVLElement<elementClass>::leftAdded(
  529. AVLTree<elementClass> *tree)
  530. {
  531. //We've just gotten one deeper on our right side.
  532. assert(balance != AVLNew);
  533. if (balance == AVLRight) {
  534. balance = AVLBalanced;
  535. // The depth of the subtree rooted here hasn't changed, we're done
  536. return;
  537. }
  538. if (balance == AVLBalanced) {
  539. // We've just gotten one deeper, but are still balanced. Update and recurse up the
  540. // tree.
  541. balance = AVLLeft;
  542. if (parent) {
  543. if (parent->right == this) {
  544. parent->rightAdded(tree);
  545. } else {
  546. assert(parent->left == this);
  547. parent->leftAdded(tree);
  548. }
  549. }
  550. return;
  551. }
  552. assert(balance == AVLLeft);
  553. // We've just gone to double left (ie, out of balance).
  554. assert(left);
  555. if (left->balance == AVLLeft) {
  556. singleRotate(tree,left,AVLLeft);
  557. } else {
  558. assert(left->balance == AVLRight); // Else we shouldn't have been AVLLeft before the call
  559. doubleRotate(tree,left,left->right,AVLLeft);
  560. }
  561. }
  562. template<class elementClass> void
  563. AVLElement<elementClass>::singleRotate(
  564. AVLTree<elementClass> *tree,
  565. AVLElement *child,
  566. AVLBalance whichSide)
  567. {
  568. // We're the parent node.
  569. assert(tree);
  570. assert(child);
  571. assert(whichSide == AVLRight || whichSide == AVLLeft);
  572. assert(whichSide != AVLRight || right == child);
  573. assert(whichSide != AVLLeft || left == child);
  574. tree->singleRotations++;
  575. // Promote the child to our position in the tree.
  576. if (parent) {
  577. if (parent->left == this) {
  578. parent->left = child;
  579. child->parent = parent;
  580. } else {
  581. assert(parent->right == this);
  582. parent->right = child;
  583. child->parent = parent;
  584. }
  585. } else {
  586. // We're the root of the tree
  587. assert(tree->tree == this);
  588. tree->tree = child;
  589. child->parent = NULL;
  590. }
  591. // Attach the child's light subtree to our heavy side (ie., where the child is attached now)
  592. // Then, attach us to the child's light subtree
  593. if (whichSide == AVLRight) {
  594. right = child->left;
  595. if (right) {
  596. right->parent = this;
  597. }
  598. child->left = this;
  599. parent = child;
  600. } else {
  601. left = child->right;
  602. if (left) {
  603. left->parent = this;
  604. }
  605. child->right = this;
  606. parent = child;
  607. }
  608. // Finally, now both our and our (former) child's balance is "balanced"
  609. balance = AVLBalanced;
  610. child->balance = AVLBalanced;
  611. // NB. One of the cases in delete will result in the above balance settings being incorrect. That
  612. // case fixes up the settings after we return.
  613. }
  614. template<class elementClass> void
  615. AVLElement<elementClass>::doubleRotate(
  616. AVLTree<elementClass> *tree,
  617. AVLElement *child,
  618. AVLElement *grandchild,
  619. AVLBalance whichSide)
  620. {
  621. assert(tree && child && grandchild);
  622. assert(whichSide == AVLLeft || whichSide == AVLRight);
  623. assert(whichSide != AVLLeft || (left == child && child->balance == AVLRight));
  624. assert(whichSide != AVLRight || (right == child && child->balance == AVLLeft));
  625. assert(child->parent == this);
  626. assert(grandchild->parent == child);
  627. tree->doubleRotations++;
  628. // Write down a copy of all of the subtrees; see Knuth v3 p454 for the picture.
  629. // NOTE: The alpha and delta trees are never moved, so we don't store them.
  630. AVLElement *beta;
  631. AVLElement *gamma;
  632. if (whichSide == AVLRight) {
  633. beta = grandchild->left;
  634. gamma = grandchild->right;
  635. } else {
  636. beta = grandchild->right;
  637. gamma = grandchild->left;
  638. }
  639. // Promote grandchild to our position
  640. if (parent) {
  641. if (parent->left == this) {
  642. parent->left = grandchild;
  643. } else {
  644. assert(parent->right == this);
  645. parent->right = grandchild;
  646. }
  647. } else {
  648. assert(tree->tree == this);
  649. tree->tree = grandchild;
  650. }
  651. grandchild->parent = parent;
  652. // Attach the appropriate children to grandchild
  653. if (whichSide == AVLRight) {
  654. grandchild->right = child;
  655. grandchild->left = this;
  656. } else {
  657. grandchild->right = this;
  658. grandchild->left = child;
  659. }
  660. parent = grandchild;
  661. child->parent = grandchild;
  662. // Attach beta and gamma to us and child.
  663. if (whichSide == AVLRight) {
  664. right = beta;
  665. if (beta) {
  666. beta->parent = this;
  667. }
  668. child->left = gamma;
  669. if (gamma) {
  670. gamma->parent = child;
  671. }
  672. } else {
  673. left = beta;
  674. if (beta) {
  675. beta->parent = this;
  676. }
  677. child->right = gamma;
  678. if (gamma) {
  679. gamma->parent = child;
  680. }
  681. }
  682. // Now update the balance fields.
  683. switch (grandchild->balance) {
  684. case AVLLeft:
  685. if (whichSide == AVLRight) {
  686. balance = AVLBalanced;
  687. child->balance = AVLRight;
  688. } else {
  689. balance = AVLRight;
  690. child->balance = AVLBalanced;
  691. }
  692. break;
  693. case AVLBalanced:
  694. balance = AVLBalanced;
  695. child->balance = AVLBalanced;
  696. break;
  697. case AVLRight:
  698. if (whichSide == AVLRight) {
  699. balance = AVLLeft;
  700. child->balance = AVLBalanced;
  701. } else {
  702. balance = AVLBalanced;
  703. child->balance = AVLLeft;
  704. }
  705. break;
  706. default:
  707. assert(!"Bogus balance value");
  708. }
  709. grandchild->balance = AVLBalanced;
  710. }
  711. template<class elementClass> void
  712. AVLElement<elementClass>::remove(
  713. AVLTree<elementClass> *fromTree)
  714. {
  715. assert(fromTree);
  716. assert(balance == AVLRight || balance == AVLLeft || balance == AVLBalanced);
  717. fromTree->deletions++;
  718. if (left == NULL) {
  719. // The right child either doesn't exist or is a leaf (because of the AVL balance property)
  720. assert((!right && balance == AVLBalanced) ||
  721. (balance == AVLRight && right->balance == AVLBalanced && right->right == NULL && right->left == NULL));
  722. if (right) {
  723. right->parent = parent;
  724. }
  725. if (parent) {
  726. if (parent->left == this) {
  727. parent->left = right;
  728. parent->gotOneShorter(fromTree,AVLLeft);
  729. } else {
  730. assert(parent->right == this);
  731. parent->right = right;
  732. parent->gotOneShorter(fromTree,AVLRight);
  733. }
  734. } else {
  735. assert(fromTree->tree == this);
  736. fromTree->tree = right;
  737. }
  738. } else if (right == NULL) {
  739. // The left child must be a left because of the AVL balance property
  740. assert(left && balance == AVLLeft && left->balance == AVLBalanced && left->right == NULL && left->left == NULL);
  741. left->parent = parent;
  742. if (parent) {
  743. if (parent->left == this) {
  744. parent->left = left;
  745. parent->gotOneShorter(fromTree,AVLLeft);
  746. } else {
  747. assert(parent->right == this);
  748. parent->right = left;
  749. parent->gotOneShorter(fromTree,AVLRight);
  750. }
  751. } else {
  752. assert(fromTree->tree == this);
  753. fromTree->tree = left;
  754. }
  755. } else {
  756. // Find the symmetric successor and promote it. The symmetric successor is the smallest element in the right
  757. // subtree; it's found by following all left links in the right subtree until we find a node with no left link.
  758. // That node may be promoted to the place of this without corrupting the binary tree ordering properties. (We could
  759. // just as easily use the symmetric predecessor by finding the largest element in the right subtree, but there's
  760. // no point.)
  761. AVLElement *successorCandidate = right;
  762. while (successorCandidate->left) {
  763. successorCandidate = successorCandidate->left;
  764. }
  765. AVLElement *shorterRoot;
  766. AVLBalance shorterSide;
  767. if (successorCandidate->parent->left == successorCandidate) {
  768. // We need to promote the successor's child (if any) to its position, then
  769. // promote it to our position.
  770. shorterRoot = successorCandidate->parent;
  771. shorterSide = AVLLeft;
  772. successorCandidate->parent->left = successorCandidate->right;
  773. if (successorCandidate->right) {
  774. successorCandidate->right->parent = successorCandidate->parent;
  775. }
  776. successorCandidate->right = right;
  777. successorCandidate->left = left;
  778. successorCandidate->balance = balance;
  779. successorCandidate->right->parent = successorCandidate;
  780. successorCandidate->left->parent = successorCandidate;
  781. if (parent) {
  782. if (parent->left == this) {
  783. parent->left = successorCandidate;
  784. } else {
  785. assert(parent->right == this);
  786. parent->right = successorCandidate;
  787. }
  788. } else {
  789. assert(fromTree->tree == this);
  790. fromTree->tree = successorCandidate;
  791. }
  792. successorCandidate->parent = parent;
  793. } else {
  794. // The successor was our child, just directly promote it.
  795. assert(successorCandidate->parent == this);
  796. if (parent) {
  797. if (parent->right == this) {
  798. parent->right = successorCandidate;
  799. } else {
  800. assert(parent->left == this);
  801. parent->left = successorCandidate;
  802. }
  803. } else {
  804. assert(fromTree->tree == this);
  805. fromTree->tree = successorCandidate;
  806. }
  807. successorCandidate->parent = parent;
  808. successorCandidate->left = left;
  809. if (left) {
  810. left->parent = successorCandidate;
  811. }
  812. // We just made our right subtree shorter.
  813. successorCandidate->balance = balance;
  814. shorterRoot = successorCandidate;
  815. shorterSide = AVLRight;
  816. }
  817. if (shorterRoot) {
  818. shorterRoot->gotOneShorter(fromTree,shorterSide);
  819. }
  820. }
  821. balance = AVLNew;
  822. left = right = parent = NULL;
  823. element = NULL;
  824. // fromTree->check();
  825. }
  826. template<class elementClass> void
  827. AVLElement<elementClass>::gotOneShorter(
  828. AVLTree<elementClass> *tree,
  829. AVLBalance whichSide)
  830. {
  831. assert(whichSide == AVLLeft || whichSide == AVLRight);
  832. if (balance == AVLBalanced) {
  833. // We've just shrunk one subttree, but our depth has stayed the same.
  834. // Reset our balance indicator and punt.
  835. if (whichSide == AVLRight) {
  836. balance = AVLLeft;
  837. } else {
  838. balance = AVLRight;
  839. }
  840. return;
  841. } else if (balance == whichSide) {
  842. // We just shrunk our heavy side; set our balance to neutral and recurse up the tree
  843. balance = AVLBalanced;
  844. if (parent) {
  845. if (parent->right == this) {
  846. parent->gotOneShorter(tree,AVLRight);
  847. } else {
  848. assert(parent->left == this);
  849. parent->gotOneShorter(tree,AVLLeft);
  850. }
  851. } // else we were the root; we're done
  852. return;
  853. } else {
  854. // We've just gone out of balance. Figure out a rotation to do. This is almost like having added a
  855. // node to the opposide side, except that the opposite side might be balanced.
  856. AVLBalance heavySide;
  857. AVLElement *heavyChild;
  858. AVLElement *replacement;
  859. if (whichSide == AVLRight) {
  860. heavySide = AVLLeft;
  861. heavyChild = left;
  862. } else {
  863. heavySide = AVLRight;
  864. heavyChild = right;
  865. }
  866. assert(heavyChild);
  867. if (heavyChild->balance == heavySide) {
  868. // Typical single rotation case
  869. singleRotate(tree,heavyChild,heavySide);
  870. replacement = heavyChild;
  871. } else if (heavyChild->balance == whichSide) {
  872. // Typical double rotation case
  873. AVLElement *grandchild;
  874. if (heavySide == AVLRight) {
  875. grandchild = heavyChild->left;
  876. } else {
  877. grandchild = heavyChild->right;
  878. }
  879. doubleRotate(tree,heavyChild,grandchild,heavySide);
  880. replacement = grandchild;
  881. } else {
  882. assert(heavyChild->balance == AVLBalanced);
  883. singleRotate(tree,heavyChild,heavySide);
  884. // singleRotate has incorrectly set the balances; reset them
  885. balance = heavySide;
  886. heavyChild->balance = whichSide;
  887. // Overall depth hasn't changed; we're done.
  888. return;
  889. }
  890. // NB: we have now changed position in the tree, so parent, right & left have changed!
  891. if (!replacement->parent) {
  892. // We just promoted our replacement to the root; we be done
  893. return;
  894. }
  895. if (replacement->parent->right == replacement) {
  896. replacement->parent->gotOneShorter(tree,AVLRight);
  897. } else {
  898. assert(replacement->parent->left == replacement);
  899. replacement->parent->gotOneShorter(tree,AVLLeft);
  900. }
  901. }
  902. }
  903. template<class elementClass> int
  904. AVLElement<elementClass>::inTree(void)
  905. {
  906. return(balance != AVLNew);
  907. }
  908. template <class elementClass> int
  909. AVLElement<elementClass>::operator<=(
  910. AVLElement<elementClass> *peer)
  911. {
  912. return(*element <= peer->element);
  913. }
  914. template <class elementClass> int
  915. AVLElement<elementClass>::operator<(
  916. AVLElement<elementClass> *peer)
  917. {
  918. return(*element < peer->element);
  919. }
  920. template <class elementClass> int
  921. AVLElement<elementClass>::operator==(
  922. AVLElement<elementClass> *peer)
  923. {
  924. return(*element == peer->element);
  925. }
  926. template <class elementClass> int
  927. AVLElement<elementClass>::operator>=(
  928. AVLElement<elementClass> *peer)
  929. {
  930. return(*element >= peer->element);
  931. }
  932. template <class elementClass> int
  933. AVLElement<elementClass>::operator>(
  934. AVLElement<elementClass> *peer)
  935. {
  936. return(*element > peer->element);
  937. }
  938. template <class elementClass> BOOLEAN
  939. AVLTree<elementClass>::insert(
  940. elementClass *element)
  941. {
  942. if (NULL == avlElementPool) {
  943. return FALSE;
  944. }
  945. assert(element);
  946. AVLElement<elementClass> *avlElement = (AVLElement<elementClass> *)avlElementPool->allocate();
  947. if (NULL == avlElement) {
  948. return FALSE;
  949. }
  950. avlElement->initialize();
  951. avlElement->insert(this,element);
  952. return TRUE;
  953. }
  954. template <class elementClass> void
  955. AVLTree<elementClass>::remove(
  956. elementClass *element)
  957. {
  958. assert(element);
  959. AVLElement<elementClass> *candidate = tree->findFirstLessThanOrEqualTo(element);
  960. assert(candidate && *candidate->element == element);
  961. candidate->remove(this);
  962. assert(avlElementPool); // if this isn't true, then we could never have had a successful insert
  963. avlElementPool->free((void *)candidate);
  964. }
  965. template <class elementClass> void
  966. AVLElement<elementClass>::initialize(void)
  967. {
  968. balance = AVLNew;
  969. left = right = parent = NULL;
  970. element = NULL;
  971. }
  972. template <class elementClass> void
  973. AVLTree<elementClass>::dumpPoolStats(void)
  974. {
  975. if (NULL == avlElementPool) {
  976. DbgPrint("Unable to allocate avlElementPool; this AVL tree is essentially useless\n");
  977. } else {
  978. DbgPrint("AVLTree AVLElement pool: %d allocations, %d frees, %d news, objectSize %d\n",
  979. avlElementPool->numAllocations(),
  980. avlElementPool->numFrees(),
  981. avlElementPool->numNews(),
  982. avlElementPool->getObjectSize());
  983. }
  984. }