avlcommon.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * Copyright 2001 Adrian Thurston <thurston@cs.queensu.ca>
  3. */
  4. /* This file is part of Aapl.
  5. *
  6. * Aapl is free software; you can redistribute it and/or modify it under the
  7. * terms of the GNU Lesser General Public License as published by the Free
  8. * Software Foundation; either version 2.1 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Aapl; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* This header is not wrapped in ifndef becuase it is not intended to
  21. * be included by the user. */
  22. #include <assert.h>
  23. #ifdef AAPL_NAMESPACE
  24. namespace Aapl {
  25. #endif
  26. #ifdef WALKABLE
  27. /* This is used by AvlTree, AvlMel and AvlMelKey so it
  28. * must be protected by global ifdefs. */
  29. #ifndef __AAPL_AVLI_EL__
  30. #define __AAPL_AVLI_EL__
  31. /**
  32. * \brief Tree element properties for linked AVL trees.
  33. *
  34. * AvliTreeEl needs to be inherited by classes that intend to be element in an
  35. * AvliTree.
  36. */
  37. template<class SubClassEl> struct AvliTreeEl
  38. {
  39. /**
  40. * \brief Tree pointers connecting element in a tree.
  41. */
  42. SubClassEl *left, *right, *parent;
  43. /**
  44. * \brief Linked list pointers.
  45. */
  46. SubClassEl *prev, *next;
  47. /**
  48. * \brief Height of the tree rooted at this element.
  49. *
  50. * Height is required by the AVL balancing algorithm.
  51. */
  52. long height;
  53. };
  54. #endif /* __AAPL_AVLI_EL__ */
  55. #else /* not WALKABLE */
  56. /* This is used by All the non walkable trees so it must be
  57. * protected by a global ifdef. */
  58. #ifndef __AAPL_AVL_EL__
  59. #define __AAPL_AVL_EL__
  60. /**
  61. * \brief Tree element properties for linked AVL trees.
  62. *
  63. * AvlTreeEl needs to be inherited by classes that intend to be element in an
  64. * AvlTree.
  65. */
  66. template<class SubClassEl> struct AvlTreeEl
  67. {
  68. /**
  69. * \brief Tree pointers connecting element in a tree.
  70. */
  71. SubClassEl *left, *right, *parent;
  72. /**
  73. * \brief Height of the tree rooted at this element.
  74. *
  75. * Height is required by the AVL balancing algorithm.
  76. */
  77. long height;
  78. };
  79. #endif /* __AAPL_AVL_EL__ */
  80. #endif /* def WALKABLE */
  81. #if defined( AVLTREE_MAP )
  82. #ifdef WALKABLE
  83. /**
  84. * \brief Tree element for AvliMap
  85. *
  86. * Stores the key and value pair.
  87. */
  88. template <class Key, class Value> struct AvliMapEl :
  89. public AvliTreeEl< AvliMapEl<Key, Value> >
  90. {
  91. AvliMapEl(const Key &key)
  92. : key(key) { }
  93. AvliMapEl(const Key &key, const Value &value)
  94. : key(key), value(value) { }
  95. const Key &getKey() const { return key; }
  96. /** \brief The key. */
  97. Key key;
  98. /** \brief The value. */
  99. Value value;
  100. };
  101. #else /* not WALKABLE */
  102. /**
  103. * \brief Tree element for AvlMap
  104. *
  105. * Stores the key and value pair.
  106. */
  107. template <class Key, class Value> struct AvlMapEl :
  108. public AvlTreeEl< AvlMapEl<Key, Value> >
  109. {
  110. AvlMapEl(const Key &key)
  111. : key(key) { }
  112. AvlMapEl(const Key &key, const Value &value)
  113. : key(key), value(value) { }
  114. const Key &getKey() const { return key; }
  115. /** \brief The key. */
  116. Key key;
  117. /** \brief The value. */
  118. Value value;
  119. };
  120. #endif /* def WALKABLE */
  121. #elif defined( AVLTREE_SET )
  122. #ifdef WALKABLE
  123. /**
  124. * \brief Tree element for AvliSet
  125. *
  126. * Stores the key.
  127. */
  128. template <class Key> struct AvliSetEl :
  129. public AvliTreeEl< AvliSetEl<Key> >
  130. {
  131. AvliSetEl(const Key &key) : key(key) { }
  132. const Key &getKey() const { return key; }
  133. /** \brief The key. */
  134. Key key;
  135. };
  136. #else /* not WALKABLE */
  137. /**
  138. * \brief Tree element for AvlSet
  139. *
  140. * Stores the key.
  141. */
  142. template <class Key> struct AvlSetEl :
  143. public AvlTreeEl< AvlSetEl<Key> >
  144. {
  145. AvlSetEl(const Key &key) : key(key) { }
  146. const Key &getKey() const { return key; }
  147. /** \brief The key. */
  148. Key key;
  149. };
  150. #endif /* def WALKABLE */
  151. #endif /* AVLTREE_SET */
  152. /* Common AvlTree Class */
  153. template < AVLMEL_CLASSDEF > class AvlTree
  154. #if !defined( AVL_KEYLESS ) && defined ( WALKABLE )
  155. : public Compare, public BASELIST
  156. #elif !defined( AVL_KEYLESS )
  157. : public Compare
  158. #elif defined( WALKABLE )
  159. : public BASELIST
  160. #endif
  161. {
  162. public:
  163. /**
  164. * \brief Create an empty tree.
  165. */
  166. #ifdef WALKABLE
  167. AvlTree() : root(0), treeSize(0) { }
  168. #else
  169. AvlTree() : root(0), head(0), tail(0), treeSize(0) { }
  170. #endif
  171. /**
  172. * \brief Perform a deep copy of the tree.
  173. *
  174. * Each element is duplicated for the new tree. Copy constructors are used
  175. * to create the new elements.
  176. */
  177. AvlTree(const AvlTree &other);
  178. #if defined( AVLTREE_MAP ) || defined( AVLTREE_SET )
  179. /**
  180. * \brief Clear the contents of the tree.
  181. *
  182. * All element are deleted.
  183. */
  184. ~AvlTree() { empty(); }
  185. /**
  186. * \brief Perform a deep copy of the tree.
  187. *
  188. * Each element is duplicated for the new tree. Copy constructors are used
  189. * to create the new element. If this tree contains items, they are first
  190. * deleted.
  191. *
  192. * \returns A reference to this.
  193. */
  194. AvlTree &operator=( const AvlTree &tree );
  195. /**
  196. * \brief Transfer the elements of another tree into this.
  197. *
  198. * First deletes all elements in this tree.
  199. */
  200. void transfer( AvlTree &tree );
  201. #else
  202. /**
  203. * \brief Abandon all elements in the tree.
  204. *
  205. * Tree elements are not deleted.
  206. */
  207. ~AvlTree() {}
  208. /**
  209. * \brief Perform a deep copy of the tree.
  210. *
  211. * Each element is duplicated for the new tree. Copy constructors are used
  212. * to create the new element. If this tree contains items, they are
  213. * abandoned.
  214. *
  215. * \returns A reference to this.
  216. */
  217. AvlTree &operator=( const AvlTree &tree );
  218. /**
  219. * \brief Transfer the elements of another tree into this.
  220. *
  221. * All elements in this tree are abandoned first.
  222. */
  223. void transfer( AvlTree &tree );
  224. #endif
  225. #ifndef AVL_KEYLESS
  226. /* Insert a element into the tree. */
  227. Element *insert( Element *element, Element **lastFound = 0 );
  228. #ifdef AVL_BASIC
  229. /* Find a element in the tree. Returns the element if
  230. * element exists, false otherwise. */
  231. Element *find( const Element *element ) const;
  232. #else
  233. Element *insert( const Key &key, Element **lastFound = 0 );
  234. #ifdef AVLTREE_MAP
  235. Element *insert( const Key &key, const Value &val,
  236. Element **lastFound = 0 );
  237. #endif
  238. /* Find a element in the tree. Returns the element if
  239. * key exists, false otherwise. */
  240. Element *find( const Key &key ) const;
  241. /* Detach a element from the tree. */
  242. Element *detach( const Key &key );
  243. /* Detach and delete a element from the tree. */
  244. bool remove( const Key &key );
  245. #endif /* AVL_BASIC */
  246. #endif /* AVL_KEYLESS */
  247. /* Detach a element from the tree. */
  248. Element *detach( Element *element );
  249. /* Detach and delete a element from the tree. */
  250. void remove( Element *element );
  251. /* Free all memory used by tree. */
  252. void empty();
  253. /* Abandon all element in the tree. Does not delete element. */
  254. void abandon();
  255. /** Root element of the tree. */
  256. Element *root;
  257. #ifndef WALKABLE
  258. Element *head, *tail;
  259. #endif
  260. /** The number of element in the tree. */
  261. long treeSize;
  262. /** \brief Return the number of elements in the tree. */
  263. long length() const { return treeSize; }
  264. /** \brief Return the number of elements in the tree. */
  265. long size() const { return treeSize; }
  266. /* Various classes for setting the iterator */
  267. struct Iter;
  268. struct IterFirst { IterFirst( const AvlTree &t ) : t(t) { } const AvlTree &t; };
  269. struct IterLast { IterLast( const AvlTree &t ) : t(t) { } const AvlTree &t; };
  270. struct IterNext { IterNext( const Iter &i ) : i(i) { } const Iter &i; };
  271. struct IterPrev { IterPrev( const Iter &i ) : i(i) { } const Iter &i; };
  272. #ifdef WALKABLE
  273. /**
  274. * \brief Avl Tree Iterator.
  275. * \ingroup iterators
  276. */
  277. struct Iter
  278. {
  279. /* Default construct. */
  280. Iter() : ptr(0) { }
  281. /* Construct from an avl tree and iterator-setting classes. */
  282. Iter( const AvlTree &t ) : ptr(t.head) { }
  283. Iter( const IterFirst &af ) : ptr(af.t.head) { }
  284. Iter( const IterLast &al ) : ptr(al.t.tail) { }
  285. Iter( const IterNext &an ) : ptr(findNext(an.i.ptr)) { }
  286. Iter( const IterPrev &ap ) : ptr(findPrev(ap.i.ptr)) { }
  287. /* Assign from a tree and iterator-setting classes. */
  288. Iter &operator=( const AvlTree &tree ) { ptr = tree.head; return *this; }
  289. Iter &operator=( const IterFirst &af ) { ptr = af.t.head; return *this; }
  290. Iter &operator=( const IterLast &al ) { ptr = al.t.tail; return *this; }
  291. Iter &operator=( const IterNext &an ) { ptr = findNext(an.i.ptr); return *this; }
  292. Iter &operator=( const IterPrev &ap ) { ptr = findPrev(ap.i.ptr); return *this; }
  293. /** \brief Less than end? */
  294. bool lte() const { return ptr != 0; }
  295. /** \brief At end? */
  296. bool end() const { return ptr == 0; }
  297. /** \brief Greater than beginning? */
  298. bool gtb() const { return ptr != 0; }
  299. /** \brief At beginning? */
  300. bool beg() const { return ptr == 0; }
  301. /** \brief At first element? */
  302. bool first() const { return ptr && ptr->BASE_EL(prev) == 0; }
  303. /** \brief At last element? */
  304. bool last() const { return ptr && ptr->BASE_EL(next) == 0; }
  305. /** \brief Implicit cast to Element*. */
  306. operator Element*() const { return ptr; }
  307. /** \brief Dereference operator returns Element&. */
  308. Element &operator *() const { return *ptr; }
  309. /** \brief Arrow operator returns Element*. */
  310. Element *operator->() const { return ptr; }
  311. /** \brief Move to next item. */
  312. inline Element *operator++();
  313. /** \brief Move to next item. */
  314. inline Element *operator++(int);
  315. /** \brief Move to next item. */
  316. inline Element *increment();
  317. /** \brief Move to previous item. */
  318. inline Element *operator--();
  319. /** \brief Move to previous item. */
  320. inline Element *operator--(int);
  321. /** \brief Move to previous item. */
  322. inline Element *decrement();
  323. /** \brief Return the next item. Does not modify this. */
  324. IterNext next() const { return IterNext( *this ); }
  325. /** \brief Return the previous item. Does not modify this. */
  326. IterPrev prev() const { return IterPrev( *this ); }
  327. private:
  328. static Element *findPrev( Element *element ) { return element->BASE_EL(prev); }
  329. static Element *findNext( Element *element ) { return element->BASE_EL(next); }
  330. public:
  331. /** \brief The iterator is simply a pointer. */
  332. Element *ptr;
  333. };
  334. #else
  335. /**
  336. * \brief Avl Tree Iterator.
  337. * \ingroup iterators
  338. */
  339. struct Iter
  340. {
  341. /* Default construct. */
  342. Iter() : ptr(0), tree(0) { }
  343. /* Construct from a tree and iterator-setting classes. */
  344. Iter( const AvlTree &t ) : ptr(t.head), tree(&t) { }
  345. Iter( const IterFirst &af ) : ptr(af.t.head), tree(&af.t) { }
  346. Iter( const IterLast &al ) : ptr(al.t.tail), tree(&al.t) { }
  347. Iter( const IterNext &an ) : ptr(findNext(an.i.ptr)), tree(an.i.tree) { }
  348. Iter( const IterPrev &ap ) : ptr(findPrev(ap.i.ptr)), tree(ap.i.tree) { }
  349. /* Assign from a tree and iterator-setting classes. */
  350. Iter &operator=( const AvlTree &t )
  351. { ptr = t.head; tree = &t; return *this; }
  352. Iter &operator=( const IterFirst &af )
  353. { ptr = af.t.head; tree = &af.t; return *this; }
  354. Iter &operator=( const IterLast &al )
  355. { ptr = al.t.tail; tree = &al.t; return *this; }
  356. Iter &operator=( const IterNext &an )
  357. { ptr = findNext(an.i.ptr); tree = an.i.tree; return *this; }
  358. Iter &operator=( const IterPrev &ap )
  359. { ptr = findPrev(ap.i.ptr); tree = ap.i.tree; return *this; }
  360. /** \brief Less than end? */
  361. bool lte() const { return ptr != 0; }
  362. /** \brief At end? */
  363. bool end() const { return ptr == 0; }
  364. /** \brief Greater than beginning? */
  365. bool gtb() const { return ptr != 0; }
  366. /** \brief At beginning? */
  367. bool beg() const { return ptr == 0; }
  368. /** \brief At first element? */
  369. bool first() const { return ptr && ptr == tree->head; }
  370. /** \brief At last element? */
  371. bool last() const { return ptr && ptr == tree->tail; }
  372. /** \brief Implicit cast to Element*. */
  373. operator Element*() const { return ptr; }
  374. /** \brief Dereference operator returns Element&. */
  375. Element &operator *() const { return *ptr; }
  376. /** \brief Arrow operator returns Element*. */
  377. Element *operator->() const { return ptr; }
  378. /** \brief Move to next item. */
  379. inline Element *operator++();
  380. /** \brief Move to next item. */
  381. inline Element *operator++(int);
  382. /** \brief Move to next item. */
  383. inline Element *increment();
  384. /** \brief Move to previous item. */
  385. inline Element *operator--();
  386. /** \brief Move to previous item. */
  387. inline Element *operator--(int);
  388. /** \brief Move to previous item. */
  389. inline Element *decrement();
  390. /** \brief Return the next item. Does not modify this. */
  391. IterNext next() const { return IterNext( *this ); }
  392. /** \brief Return the previous item. Does not modify this. */
  393. IterPrev prev() const { return IterPrev( *this ); }
  394. private:
  395. static Element *findPrev( Element *element );
  396. static Element *findNext( Element *element );
  397. public:
  398. /** \brief The iterator is simply a pointer. */
  399. Element *ptr;
  400. /* The list is not walkable so we need to keep a pointerto the tree
  401. * so we can test against head and tail in O(1) time. */
  402. const AvlTree *tree;
  403. };
  404. #endif
  405. /** \brief Return first element. */
  406. IterFirst first() { return IterFirst( *this ); }
  407. /** \brief Return last element. */
  408. IterLast last() { return IterLast( *this ); }
  409. protected:
  410. /* Recursive worker for the copy constructor. */
  411. Element *copyBranch( Element *element );
  412. /* Recursively delete element in the tree. */
  413. void deleteChildrenOf(Element *n);
  414. /* rebalance the tree beginning at the leaf whose
  415. * grandparent is unbalanced. */
  416. Element *rebalance(Element *start);
  417. /* Move up the tree from a given element, recalculating the heights. */
  418. void recalcHeights(Element *start);
  419. /* Move up the tree and find the first element whose
  420. * grand-parent is unbalanced. */
  421. Element *findFirstUnbalGP(Element *start);
  422. /* Move up the tree and find the first element which is unbalanced. */
  423. Element *findFirstUnbalEl(Element *start);
  424. /* Replace a element in the tree with another element not in the tree. */
  425. void replaceEl(Element *element, Element *replacement);
  426. /* Remove a element from the tree and put another (normally a child of element)
  427. * in its place. */
  428. void removeEl(Element *element, Element *filler);
  429. /* Once an insertion point is found at a leaf then do the insert. */
  430. void attachRebal( Element *element, Element *parentEl, Element *lastLess );
  431. };
  432. /* Copy constructor. New up each item. */
  433. template <AVLMEL_TEMPDEF> AvlTree<AVLMEL_TEMPUSE>::
  434. AvlTree(const AvlTree<AVLMEL_TEMPUSE> &other)
  435. #ifdef WALKABLE
  436. :
  437. /* Make an empty list, copyBranch will fill in the details for us. */
  438. BASELIST()
  439. #endif
  440. {
  441. treeSize = other.treeSize;
  442. root = other.root;
  443. #ifndef WALKABLE
  444. head = 0;
  445. tail = 0;
  446. #endif
  447. /* If there is a root, copy the tree. */
  448. if ( other.root != 0 )
  449. root = copyBranch( other.root );
  450. }
  451. #if defined( AVLTREE_MAP ) || defined( AVLTREE_SET )
  452. /* Assignment does deep copy. */
  453. template <AVLMEL_TEMPDEF> AvlTree<AVLMEL_TEMPUSE> &AvlTree<AVLMEL_TEMPUSE>::
  454. operator=( const AvlTree &other )
  455. {
  456. /* Clear the tree first. */
  457. empty();
  458. /* Reset the list pointers, the tree copy will fill in the list for us. */
  459. #ifdef WALKABLE
  460. BASELIST::abandon();
  461. #else
  462. head = 0;
  463. tail = 0;
  464. #endif
  465. /* Copy the entire tree. */
  466. treeSize = other.treeSize;
  467. root = other.root;
  468. if ( other.root != 0 )
  469. root = copyBranch( other.root );
  470. return *this;
  471. }
  472. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  473. transfer(AvlTree<AVLMEL_TEMPUSE> &other)
  474. {
  475. /* Clear the tree first. */
  476. empty();
  477. treeSize = other.treeSize;
  478. root = other.root;
  479. #ifdef WALKABLE
  480. BASELIST::head = other.BASELIST::head;
  481. BASELIST::tail = other.BASELIST::tail;
  482. BASELIST::listLen = other.BASELIST::listLen;
  483. #else
  484. head = other.head;
  485. tail = other.tail;
  486. #endif
  487. other.abandon();
  488. }
  489. #else /* ! AVLTREE_MAP && ! AVLTREE_SET */
  490. /* Assignment does deep copy. This version does not clear the tree first. */
  491. template <AVLMEL_TEMPDEF> AvlTree<AVLMEL_TEMPUSE> &AvlTree<AVLMEL_TEMPUSE>::
  492. operator=( const AvlTree &other )
  493. {
  494. /* Reset the list pointers, the tree copy will fill in the list for us. */
  495. #ifdef WALKABLE
  496. BASELIST::abandon();
  497. #else
  498. head = 0;
  499. tail = 0;
  500. #endif
  501. /* Copy the entire tree. */
  502. treeSize = other.treeSize;
  503. root = other.root;
  504. if ( other.root != 0 )
  505. root = copyBranch( other.root );
  506. return *this;
  507. }
  508. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  509. transfer(AvlTree<AVLMEL_TEMPUSE> &other)
  510. {
  511. treeSize = other.treeSize;
  512. root = other.root;
  513. #ifdef WALKABLE
  514. BASELIST::head = other.BASELIST::head;
  515. BASELIST::tail = other.BASELIST::tail;
  516. BASELIST::listLen = other.BASELIST::listLen;
  517. #else
  518. head = other.head;
  519. tail = other.tail;
  520. #endif
  521. other.abandon();
  522. }
  523. #endif
  524. /*
  525. * Iterator operators.
  526. */
  527. /* Prefix ++ */
  528. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  529. operator++()
  530. {
  531. return ptr = findNext( ptr );
  532. }
  533. /* Postfix ++ */
  534. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  535. operator++(int)
  536. {
  537. Element *rtn = ptr;
  538. ptr = findNext( ptr );
  539. return rtn;
  540. }
  541. /* increment */
  542. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  543. increment()
  544. {
  545. return ptr = findNext( ptr );
  546. }
  547. /* Prefix -- */
  548. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  549. operator--()
  550. {
  551. return ptr = findPrev( ptr );
  552. }
  553. /* Postfix -- */
  554. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  555. operator--(int)
  556. {
  557. Element *rtn = ptr;
  558. ptr = findPrev( ptr );
  559. return rtn;
  560. }
  561. /* decrement */
  562. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  563. decrement()
  564. {
  565. return ptr = findPrev( ptr );
  566. }
  567. #ifndef WALKABLE
  568. /* Move ahead one. */
  569. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  570. findNext( Element *element )
  571. {
  572. /* Try to go right once then infinite left. */
  573. if ( element->BASE_EL(right) != 0 ) {
  574. element = element->BASE_EL(right);
  575. while ( element->BASE_EL(left) != 0 )
  576. element = element->BASE_EL(left);
  577. }
  578. else {
  579. /* Go up to parent until we were just a left child. */
  580. while ( true ) {
  581. Element *last = element;
  582. element = element->BASE_EL(parent);
  583. if ( element == 0 || element->BASE_EL(left) == last )
  584. break;
  585. }
  586. }
  587. return element;
  588. }
  589. /* Move back one. */
  590. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::Iter::
  591. findPrev( Element *element )
  592. {
  593. /* Try to go left once then infinite right. */
  594. if ( element->BASE_EL(left) != 0 ) {
  595. element = element->BASE_EL(left);
  596. while ( element->BASE_EL(right) != 0 )
  597. element = element->BASE_EL(right);
  598. }
  599. else {
  600. /* Go up to parent until we were just a left child. */
  601. while ( true ) {
  602. Element *last = element;
  603. element = element->BASE_EL(parent);
  604. if ( element == 0 || element->BASE_EL(right) == last )
  605. break;
  606. }
  607. }
  608. return element;
  609. }
  610. #endif
  611. /* Recursive worker for tree copying. */
  612. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  613. copyBranch( Element *element )
  614. {
  615. /* Duplicate element. Either the base element's copy constructor or defaul
  616. * constructor will get called. Both will suffice for initting the
  617. * pointers to null when they need to be. */
  618. Element *retVal = new Element(*element);
  619. /* If the left tree is there, copy it. */
  620. if ( retVal->BASE_EL(left) ) {
  621. retVal->BASE_EL(left) = copyBranch(retVal->BASE_EL(left));
  622. retVal->BASE_EL(left)->BASE_EL(parent) = retVal;
  623. }
  624. #ifdef WALKABLE
  625. BASELIST::addAfter( BASELIST::tail, retVal );
  626. #else
  627. if ( head == 0 )
  628. head = retVal;
  629. tail = retVal;
  630. #endif
  631. /* If the right tree is there, copy it. */
  632. if ( retVal->BASE_EL(right) ) {
  633. retVal->BASE_EL(right) = copyBranch(retVal->BASE_EL(right));
  634. retVal->BASE_EL(right)->BASE_EL(parent) = retVal;
  635. }
  636. return retVal;
  637. }
  638. /* Once an insertion position is found, attach a element to the tree. */
  639. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  640. attachRebal( Element *element, Element *parentEl, Element *lastLess )
  641. {
  642. /* Increment the number of element in the tree. */
  643. treeSize += 1;
  644. /* Set element's parent. */
  645. element->BASE_EL(parent) = parentEl;
  646. /* New element always starts as a leaf with height 1. */
  647. element->BASE_EL(left) = 0;
  648. element->BASE_EL(right) = 0;
  649. element->BASE_EL(height) = 1;
  650. /* Are we inserting in the tree somewhere? */
  651. if ( parentEl != 0 ) {
  652. /* We have a parent so we are somewhere in the tree. If the parent
  653. * equals lastLess, then the last traversal in the insertion went
  654. * left, otherwise it went right. */
  655. if ( lastLess == parentEl ) {
  656. parentEl->BASE_EL(left) = element;
  657. #ifdef WALKABLE
  658. BASELIST::addBefore( parentEl, element );
  659. #endif
  660. }
  661. else {
  662. parentEl->BASE_EL(right) = element;
  663. #ifdef WALKABLE
  664. BASELIST::addAfter( parentEl, element );
  665. #endif
  666. }
  667. #ifndef WALKABLE
  668. /* Maintain the first and last pointers. */
  669. if ( head->BASE_EL(left) == element )
  670. head = element;
  671. /* Maintain the first and last pointers. */
  672. if ( tail->BASE_EL(right) == element )
  673. tail = element;
  674. #endif
  675. }
  676. else {
  677. /* No parent element so we are inserting the root. */
  678. root = element;
  679. #ifdef WALKABLE
  680. BASELIST::addAfter( BASELIST::tail, element );
  681. #else
  682. head = tail = element;
  683. #endif
  684. }
  685. /* Recalculate the heights. */
  686. recalcHeights(parentEl);
  687. /* Find the first unbalance. */
  688. Element *ub = findFirstUnbalGP(element);
  689. /* rebalance. */
  690. if ( ub != 0 )
  691. {
  692. /* We assert that after this single rotation the
  693. * tree is now properly balanced. */
  694. rebalance(ub);
  695. }
  696. }
  697. #ifndef AVL_KEYLESS
  698. /**
  699. * \brief Insert an existing element into the tree.
  700. *
  701. * If the insert succeeds and lastFound is given then it is set to the element
  702. * inserted. If the insert fails then lastFound is set to the existing element in
  703. * the tree that has the same key as element. If the element's avl pointers are
  704. * already in use then undefined behaviour results.
  705. *
  706. * \returns The element inserted upon success, null upon failure.
  707. */
  708. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  709. insert( Element *element, Element **lastFound )
  710. {
  711. long keyRelation;
  712. Element *curEl = root, *parentEl = 0;
  713. Element *lastLess = 0;
  714. while (true) {
  715. if ( curEl == 0 ) {
  716. /* We are at an external element and did not find the key we were
  717. * looking for. Attach underneath the leaf and rebalance. */
  718. attachRebal( element, parentEl, lastLess );
  719. if ( lastFound != 0 )
  720. *lastFound = element;
  721. return element;
  722. }
  723. #ifdef AVL_BASIC
  724. keyRelation = this->compare( *element, *curEl );
  725. #else
  726. keyRelation = this->compare( element->BASEKEY(getKey()),
  727. curEl->BASEKEY(getKey()) );
  728. #endif
  729. /* Do we go left? */
  730. if ( keyRelation < 0 ) {
  731. parentEl = lastLess = curEl;
  732. curEl = curEl->BASE_EL(left);
  733. }
  734. /* Do we go right? */
  735. else if ( keyRelation > 0 ) {
  736. parentEl = curEl;
  737. curEl = curEl->BASE_EL(right);
  738. }
  739. /* We have hit the target. */
  740. else {
  741. if ( lastFound != 0 )
  742. *lastFound = curEl;
  743. return 0;
  744. }
  745. }
  746. }
  747. #ifdef AVL_BASIC
  748. /**
  749. * \brief Find a element in the tree with the given key.
  750. *
  751. * \returns The element if key exists, null if the key does not exist.
  752. */
  753. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  754. find( const Element *element ) const
  755. {
  756. Element *curEl = root;
  757. long keyRelation;
  758. while (curEl) {
  759. keyRelation = this->compare( *element, *curEl );
  760. /* Do we go left? */
  761. if ( keyRelation < 0 )
  762. curEl = curEl->BASE_EL(left);
  763. /* Do we go right? */
  764. else if ( keyRelation > 0 )
  765. curEl = curEl->BASE_EL(right);
  766. /* We have hit the target. */
  767. else {
  768. return curEl;
  769. }
  770. }
  771. return 0;
  772. }
  773. #else
  774. /**
  775. * \brief Insert a new element into the tree with given key.
  776. *
  777. * If the key is not already in the tree then a new element is made using the
  778. * Element(const Key &key) constructor and the insert succeeds. If lastFound is
  779. * given then it is set to the element inserted. If the insert fails then
  780. * lastFound is set to the existing element in the tree that has the same key as
  781. * element.
  782. *
  783. * \returns The new element upon success, null upon failure.
  784. */
  785. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  786. insert( const Key &key, Element **lastFound )
  787. {
  788. long keyRelation;
  789. Element *curEl = root, *parentEl = 0;
  790. Element *lastLess = 0;
  791. while (true) {
  792. if ( curEl == 0 ) {
  793. /* We are at an external element and did not find the key we were
  794. * looking for. Create the new element, attach it underneath the leaf
  795. * and rebalance. */
  796. Element *element = new Element( key );
  797. attachRebal( element, parentEl, lastLess );
  798. if ( lastFound != 0 )
  799. *lastFound = element;
  800. return element;
  801. }
  802. keyRelation = this->compare( key, curEl->BASEKEY(getKey()) );
  803. /* Do we go left? */
  804. if ( keyRelation < 0 ) {
  805. parentEl = lastLess = curEl;
  806. curEl = curEl->BASE_EL(left);
  807. }
  808. /* Do we go right? */
  809. else if ( keyRelation > 0 ) {
  810. parentEl = curEl;
  811. curEl = curEl->BASE_EL(right);
  812. }
  813. /* We have hit the target. */
  814. else {
  815. if ( lastFound != 0 )
  816. *lastFound = curEl;
  817. return 0;
  818. }
  819. }
  820. }
  821. #ifdef AVLTREE_MAP
  822. /**
  823. * \brief Insert a new element into the tree with key and value.
  824. *
  825. * If the key is not already in the tree then a new element is constructed and
  826. * the insert succeeds. If lastFound is given then it is set to the element
  827. * inserted. If the insert fails then lastFound is set to the existing element in
  828. * the tree that has the same key as element. This insert routine is only
  829. * available in AvlMap because it is the only class that knows about a Value
  830. * type.
  831. *
  832. * \returns The new element upon success, null upon failure.
  833. */
  834. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  835. insert( const Key &key, const Value &val, Element **lastFound )
  836. {
  837. long keyRelation;
  838. Element *curEl = root, *parentEl = 0;
  839. Element *lastLess = 0;
  840. while (true) {
  841. if ( curEl == 0 ) {
  842. /* We are at an external element and did not find the key we were
  843. * looking for. Create the new element, attach it underneath the leaf
  844. * and rebalance. */
  845. Element *element = new Element( key, val );
  846. attachRebal( element, parentEl, lastLess );
  847. if ( lastFound != 0 )
  848. *lastFound = element;
  849. return element;
  850. }
  851. keyRelation = this->compare(key, curEl->getKey());
  852. /* Do we go left? */
  853. if ( keyRelation < 0 ) {
  854. parentEl = lastLess = curEl;
  855. curEl = curEl->BASE_EL(left);
  856. }
  857. /* Do we go right? */
  858. else if ( keyRelation > 0 ) {
  859. parentEl = curEl;
  860. curEl = curEl->BASE_EL(right);
  861. }
  862. /* We have hit the target. */
  863. else {
  864. if ( lastFound != 0 )
  865. *lastFound = curEl;
  866. return 0;
  867. }
  868. }
  869. }
  870. #endif /* AVLTREE_MAP */
  871. /**
  872. * \brief Find a element in the tree with the given key.
  873. *
  874. * \returns The element if key exists, null if the key does not exist.
  875. */
  876. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  877. find( const Key &key ) const
  878. {
  879. Element *curEl = root;
  880. long keyRelation;
  881. while (curEl) {
  882. keyRelation = this->compare( key, curEl->BASEKEY(getKey()) );
  883. /* Do we go left? */
  884. if ( keyRelation < 0 )
  885. curEl = curEl->BASE_EL(left);
  886. /* Do we go right? */
  887. else if ( keyRelation > 0 )
  888. curEl = curEl->BASE_EL(right);
  889. /* We have hit the target. */
  890. else {
  891. return curEl;
  892. }
  893. }
  894. return 0;
  895. }
  896. /**
  897. * \brief Find a element, then detach it from the tree.
  898. *
  899. * The element is not deleted.
  900. *
  901. * \returns The element detached if the key is found, othewise returns null.
  902. */
  903. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  904. detach(const Key &key)
  905. {
  906. Element *element = find( key );
  907. if ( element ) {
  908. detach(element);
  909. }
  910. return element;
  911. }
  912. /**
  913. * \brief Find, detach and delete a element from the tree.
  914. *
  915. * \returns True if the element was found and deleted, false otherwise.
  916. */
  917. template <AVLMEL_TEMPDEF> bool AvlTree<AVLMEL_TEMPUSE>::
  918. remove(const Key &key)
  919. {
  920. /* Assume not found. */
  921. bool retVal = false;
  922. /* Look for the key. */
  923. Element *element = find( key );
  924. if ( element != 0 ) {
  925. /* If found, detach the element and delete. */
  926. detach( element );
  927. delete element;
  928. retVal = true;
  929. }
  930. return retVal;
  931. }
  932. #endif /* AVL_BASIC */
  933. #endif /* AVL_KEYLESS */
  934. /**
  935. * \brief Detach and delete a element from the tree.
  936. *
  937. * If the element is not in the tree then undefined behaviour results.
  938. */
  939. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  940. remove(Element *element)
  941. {
  942. /* Detach and delete. */
  943. detach(element);
  944. delete element;
  945. }
  946. /**
  947. * \brief Detach a element from the tree.
  948. *
  949. * If the element is not in the tree then undefined behaviour results.
  950. *
  951. * \returns The element given.
  952. */
  953. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  954. detach(Element *element)
  955. {
  956. Element *replacement, *fixfrom;
  957. long lheight, rheight;
  958. #ifdef WALKABLE
  959. /* Remove the element from the ordered list. */
  960. BASELIST::detach( element );
  961. #endif
  962. /* Update treeSize. */
  963. treeSize--;
  964. /* Find a replacement element. */
  965. if (element->BASE_EL(right))
  966. {
  967. /* Find the leftmost element of the right subtree. */
  968. replacement = element->BASE_EL(right);
  969. while (replacement->BASE_EL(left))
  970. replacement = replacement->BASE_EL(left);
  971. /* If replacing the element the with its child then we need to start
  972. * fixing at the replacement, otherwise we start fixing at the
  973. * parent of the replacement. */
  974. if (replacement->BASE_EL(parent) == element)
  975. fixfrom = replacement;
  976. else
  977. fixfrom = replacement->BASE_EL(parent);
  978. #ifndef WALKABLE
  979. if ( element == head )
  980. head = replacement;
  981. #endif
  982. removeEl(replacement, replacement->BASE_EL(right));
  983. replaceEl(element, replacement);
  984. }
  985. else if (element->BASE_EL(left))
  986. {
  987. /* Find the rightmost element of the left subtree. */
  988. replacement = element->BASE_EL(left);
  989. while (replacement->BASE_EL(right))
  990. replacement = replacement->BASE_EL(right);
  991. /* If replacing the element the with its child then we need to start
  992. * fixing at the replacement, otherwise we start fixing at the
  993. * parent of the replacement. */
  994. if (replacement->BASE_EL(parent) == element)
  995. fixfrom = replacement;
  996. else
  997. fixfrom = replacement->BASE_EL(parent);
  998. #ifndef WALKABLE
  999. if ( element == tail )
  1000. tail = replacement;
  1001. #endif
  1002. removeEl(replacement, replacement->BASE_EL(left));
  1003. replaceEl(element, replacement);
  1004. }
  1005. else
  1006. {
  1007. /* We need to start fixing at the parent of the element. */
  1008. fixfrom = element->BASE_EL(parent);
  1009. #ifndef WALKABLE
  1010. if ( element == head )
  1011. head = element->BASE_EL(parent);
  1012. if ( element == tail )
  1013. tail = element->BASE_EL(parent);
  1014. #endif
  1015. /* The element we are deleting is a leaf element. */
  1016. removeEl(element, 0);
  1017. }
  1018. /* If fixfrom is null it means we just deleted
  1019. * the root of the tree. */
  1020. if ( fixfrom == 0 )
  1021. return element;
  1022. /* Fix the heights after the deletion. */
  1023. recalcHeights(fixfrom);
  1024. /* Fix every unbalanced element going up in the tree. */
  1025. Element *ub = findFirstUnbalEl(fixfrom);
  1026. while ( ub )
  1027. {
  1028. /* Find the element to rebalance by moving down from the first unbalanced
  1029. * element 2 levels in the direction of the greatest heights. On the
  1030. * second move down, the heights may be equal ( but not on the first ).
  1031. * In which case go in the direction of the first move. */
  1032. lheight = ub->BASE_EL(left) ? ub->BASE_EL(left)->BASE_EL(height) : 0;
  1033. rheight = ub->BASE_EL(right) ? ub->BASE_EL(right)->BASE_EL(height) : 0;
  1034. assert( lheight != rheight );
  1035. if (rheight > lheight)
  1036. {
  1037. ub = ub->BASE_EL(right);
  1038. lheight = ub->BASE_EL(left) ?
  1039. ub->BASE_EL(left)->BASE_EL(height) : 0;
  1040. rheight = ub->BASE_EL(right) ?
  1041. ub->BASE_EL(right)->BASE_EL(height) : 0;
  1042. if (rheight > lheight)
  1043. ub = ub->BASE_EL(right);
  1044. else if (rheight < lheight)
  1045. ub = ub->BASE_EL(left);
  1046. else
  1047. ub = ub->BASE_EL(right);
  1048. }
  1049. else
  1050. {
  1051. ub = ub->BASE_EL(left);
  1052. lheight = ub->BASE_EL(left) ?
  1053. ub->BASE_EL(left)->BASE_EL(height) : 0;
  1054. rheight = ub->BASE_EL(right) ?
  1055. ub->BASE_EL(right)->BASE_EL(height) : 0;
  1056. if (rheight > lheight)
  1057. ub = ub->BASE_EL(right);
  1058. else if (rheight < lheight)
  1059. ub = ub->BASE_EL(left);
  1060. else
  1061. ub = ub->BASE_EL(left);
  1062. }
  1063. /* rebalance returns the grandparant of the subtree formed
  1064. * by the element that were rebalanced.
  1065. * We must continue upward from there rebalancing. */
  1066. fixfrom = rebalance(ub);
  1067. /* Find the next unbalaced element. */
  1068. ub = findFirstUnbalEl(fixfrom);
  1069. }
  1070. return element;
  1071. }
  1072. /**
  1073. * \brief Empty the tree and delete all the element.
  1074. *
  1075. * Resets the tree to its initial state.
  1076. */
  1077. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::empty()
  1078. {
  1079. if ( root ) {
  1080. /* Recursively delete from the tree structure. */
  1081. deleteChildrenOf(root);
  1082. delete root;
  1083. root = 0;
  1084. treeSize = 0;
  1085. #ifdef WALKABLE
  1086. BASELIST::abandon();
  1087. #endif
  1088. }
  1089. }
  1090. /**
  1091. * \brief Forget all element in the tree.
  1092. *
  1093. * Does not delete element. Resets the the tree to it's initial state.
  1094. */
  1095. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::abandon()
  1096. {
  1097. root = 0;
  1098. treeSize = 0;
  1099. #ifdef WALKABLE
  1100. BASELIST::abandon();
  1101. #endif
  1102. }
  1103. /* Recursively delete all the children of a element. */
  1104. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  1105. deleteChildrenOf( Element *element )
  1106. {
  1107. /* Recurse left. */
  1108. if (element->BASE_EL(left)) {
  1109. deleteChildrenOf(element->BASE_EL(left));
  1110. /* Delete left element. */
  1111. delete element->BASE_EL(left);
  1112. element->BASE_EL(left) = 0;
  1113. }
  1114. /* Recurse right. */
  1115. if (element->BASE_EL(right)) {
  1116. deleteChildrenOf(element->BASE_EL(right));
  1117. /* Delete right element. */
  1118. delete element->BASE_EL(right);
  1119. element->BASE_EL(right) = 0;
  1120. }
  1121. }
  1122. /* rebalance from a element whose gradparent is unbalanced. Only
  1123. * call on a element that has a grandparent. */
  1124. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  1125. rebalance(Element *n)
  1126. {
  1127. long lheight, rheight;
  1128. Element *a, *b, *c;
  1129. Element *t1, *t2, *t3, *t4;
  1130. Element *p = n->BASE_EL(parent); /* parent (Non-NUL). L*/
  1131. Element *gp = p->BASE_EL(parent); /* Grand-parent (Non-NULL). */
  1132. Element *ggp = gp->BASE_EL(parent); /* Great grand-parent (may be NULL). */
  1133. if (gp->BASE_EL(right) == p)
  1134. {
  1135. /* gp
  1136. * \
  1137. * p
  1138. */
  1139. if (p->BASE_EL(right) == n)
  1140. {
  1141. /* gp
  1142. * \
  1143. * p
  1144. * \
  1145. * n
  1146. */
  1147. a = gp;
  1148. b = p;
  1149. c = n;
  1150. t1 = gp->BASE_EL(left);
  1151. t2 = p->BASE_EL(left);
  1152. t3 = n->BASE_EL(left);
  1153. t4 = n->BASE_EL(right);
  1154. }
  1155. else
  1156. {
  1157. /* gp
  1158. * \
  1159. * p
  1160. * /
  1161. * n
  1162. */
  1163. a = gp;
  1164. b = n;
  1165. c = p;
  1166. t1 = gp->BASE_EL(left);
  1167. t2 = n->BASE_EL(left);
  1168. t3 = n->BASE_EL(right);
  1169. t4 = p->BASE_EL(right);
  1170. }
  1171. }
  1172. else
  1173. {
  1174. /* gp
  1175. * /
  1176. * p
  1177. */
  1178. if (p->BASE_EL(right) == n)
  1179. {
  1180. /* gp
  1181. * /
  1182. * p
  1183. * \
  1184. * n
  1185. */
  1186. a = p;
  1187. b = n;
  1188. c = gp;
  1189. t1 = p->BASE_EL(left);
  1190. t2 = n->BASE_EL(left);
  1191. t3 = n->BASE_EL(right);
  1192. t4 = gp->BASE_EL(right);
  1193. }
  1194. else
  1195. {
  1196. /* gp
  1197. * /
  1198. * p
  1199. * /
  1200. * n
  1201. */
  1202. a = n;
  1203. b = p;
  1204. c = gp;
  1205. t1 = n->BASE_EL(left);
  1206. t2 = n->BASE_EL(right);
  1207. t3 = p->BASE_EL(right);
  1208. t4 = gp->BASE_EL(right);
  1209. }
  1210. }
  1211. /* Perform rotation.
  1212. */
  1213. /* Tie b to the great grandparent. */
  1214. if ( ggp == 0 )
  1215. root = b;
  1216. else if ( ggp->BASE_EL(left) == gp )
  1217. ggp->BASE_EL(left) = b;
  1218. else
  1219. ggp->BASE_EL(right) = b;
  1220. b->BASE_EL(parent) = ggp;
  1221. /* Tie a as a leftchild of b. */
  1222. b->BASE_EL(left) = a;
  1223. a->BASE_EL(parent) = b;
  1224. /* Tie c as a rightchild of b. */
  1225. b->BASE_EL(right) = c;
  1226. c->BASE_EL(parent) = b;
  1227. /* Tie t1 as a leftchild of a. */
  1228. a->BASE_EL(left) = t1;
  1229. if ( t1 != 0 ) t1->BASE_EL(parent) = a;
  1230. /* Tie t2 as a rightchild of a. */
  1231. a->BASE_EL(right) = t2;
  1232. if ( t2 != 0 ) t2->BASE_EL(parent) = a;
  1233. /* Tie t3 as a leftchild of c. */
  1234. c->BASE_EL(left) = t3;
  1235. if ( t3 != 0 ) t3->BASE_EL(parent) = c;
  1236. /* Tie t4 as a rightchild of c. */
  1237. c->BASE_EL(right) = t4;
  1238. if ( t4 != 0 ) t4->BASE_EL(parent) = c;
  1239. /* The heights are all recalculated manualy and the great
  1240. * grand-parent is passed to recalcHeights() to ensure
  1241. * the heights are correct up the tree.
  1242. *
  1243. * Note that recalcHeights() cuts out when it comes across
  1244. * a height that hasn't changed.
  1245. */
  1246. /* Fix height of a. */
  1247. lheight = a->BASE_EL(left) ? a->BASE_EL(left)->BASE_EL(height) : 0;
  1248. rheight = a->BASE_EL(right) ? a->BASE_EL(right)->BASE_EL(height) : 0;
  1249. a->BASE_EL(height) = (lheight > rheight ? lheight : rheight) + 1;
  1250. /* Fix height of c. */
  1251. lheight = c->BASE_EL(left) ? c->BASE_EL(left)->BASE_EL(height) : 0;
  1252. rheight = c->BASE_EL(right) ? c->BASE_EL(right)->BASE_EL(height) : 0;
  1253. c->BASE_EL(height) = (lheight > rheight ? lheight : rheight) + 1;
  1254. /* Fix height of b. */
  1255. lheight = a->BASE_EL(height);
  1256. rheight = c->BASE_EL(height);
  1257. b->BASE_EL(height) = (lheight > rheight ? lheight : rheight) + 1;
  1258. /* Fix height of b's parents. */
  1259. recalcHeights(ggp);
  1260. return ggp;
  1261. }
  1262. /* Recalculates the heights of all the ancestors of element. */
  1263. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  1264. recalcHeights(Element *element)
  1265. {
  1266. long lheight, rheight, new_height;
  1267. while ( element != 0 )
  1268. {
  1269. lheight = element->BASE_EL(left) ? element->BASE_EL(left)->BASE_EL(height) : 0;
  1270. rheight = element->BASE_EL(right) ? element->BASE_EL(right)->BASE_EL(height) : 0;
  1271. new_height = (lheight > rheight ? lheight : rheight) + 1;
  1272. /* If there is no chage in the height, then there will be no
  1273. * change in any of the ancestor's height. We can stop going up.
  1274. * If there was a change, continue upward. */
  1275. if (new_height == element->BASE_EL(height))
  1276. return;
  1277. else
  1278. element->BASE_EL(height) = new_height;
  1279. element = element->BASE_EL(parent);
  1280. }
  1281. }
  1282. /* Finds the first element whose grandparent is unbalanced. */
  1283. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  1284. findFirstUnbalGP(Element *element)
  1285. {
  1286. long lheight, rheight, balanceProp;
  1287. Element *gp;
  1288. if ( element == 0 || element->BASE_EL(parent) == 0 ||
  1289. element->BASE_EL(parent)->BASE_EL(parent) == 0 )
  1290. return 0;
  1291. /* Don't do anything if we we have no grandparent. */
  1292. gp = element->BASE_EL(parent)->BASE_EL(parent);
  1293. while ( gp != 0 )
  1294. {
  1295. lheight = gp->BASE_EL(left) ? gp->BASE_EL(left)->BASE_EL(height) : 0;
  1296. rheight = gp->BASE_EL(right) ? gp->BASE_EL(right)->BASE_EL(height) : 0;
  1297. balanceProp = lheight - rheight;
  1298. if ( balanceProp < -1 || balanceProp > 1 )
  1299. return element;
  1300. element = element->BASE_EL(parent);
  1301. gp = gp->BASE_EL(parent);
  1302. }
  1303. return 0;
  1304. }
  1305. /* Finds the first element that is unbalanced. */
  1306. template <AVLMEL_TEMPDEF> Element *AvlTree<AVLMEL_TEMPUSE>::
  1307. findFirstUnbalEl(Element *element)
  1308. {
  1309. if ( element == 0 )
  1310. return 0;
  1311. while ( element != 0 )
  1312. {
  1313. long lheight = element->BASE_EL(left) ?
  1314. element->BASE_EL(left)->BASE_EL(height) : 0;
  1315. long rheight = element->BASE_EL(right) ?
  1316. element->BASE_EL(right)->BASE_EL(height) : 0;
  1317. long balanceProp = lheight - rheight;
  1318. if ( balanceProp < -1 || balanceProp > 1 )
  1319. return element;
  1320. element = element->BASE_EL(parent);
  1321. }
  1322. return 0;
  1323. }
  1324. /* Replace a element in the tree with another element not in the tree. */
  1325. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  1326. replaceEl(Element *element, Element *replacement)
  1327. {
  1328. Element *parent = element->BASE_EL(parent),
  1329. *left = element->BASE_EL(left),
  1330. *right = element->BASE_EL(right);
  1331. replacement->BASE_EL(left) = left;
  1332. if (left)
  1333. left->BASE_EL(parent) = replacement;
  1334. replacement->BASE_EL(right) = right;
  1335. if (right)
  1336. right->BASE_EL(parent) = replacement;
  1337. replacement->BASE_EL(parent) = parent;
  1338. if (parent)
  1339. {
  1340. if (parent->BASE_EL(left) == element)
  1341. parent->BASE_EL(left) = replacement;
  1342. else
  1343. parent->BASE_EL(right) = replacement;
  1344. }
  1345. else
  1346. root = replacement;
  1347. replacement->BASE_EL(height) = element->BASE_EL(height);
  1348. }
  1349. /* Removes a element from a tree and puts filler in it's place.
  1350. * Filler should be null or a child of element. */
  1351. template <AVLMEL_TEMPDEF> void AvlTree<AVLMEL_TEMPUSE>::
  1352. removeEl(Element *element, Element *filler)
  1353. {
  1354. Element *parent = element->BASE_EL(parent);
  1355. if (parent)
  1356. {
  1357. if (parent->BASE_EL(left) == element)
  1358. parent->BASE_EL(left) = filler;
  1359. else
  1360. parent->BASE_EL(right) = filler;
  1361. }
  1362. else
  1363. root = filler;
  1364. if (filler)
  1365. filler->BASE_EL(parent) = parent;
  1366. return;
  1367. }
  1368. #ifdef AAPL_NAMESPACE
  1369. }
  1370. #endif