Leaked source code of windows server 2003
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.

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