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.

729 lines
20 KiB

  1. ////////////////////////////////////////////////////////////
  2. //
  3. // File name: splaytree.cxx
  4. //
  5. // Title: Splay Tree class
  6. //
  7. // Desciption:
  8. //
  9. // Author: Scott Holden (t-scotth)
  10. //
  11. // Date: August 16, 1994
  12. //
  13. ////////////////////////////////////////////////////////////
  14. #include <sysinc.h>
  15. #include "splaytre.hxx"
  16. template<class T>
  17. void
  18. SplayTree<T>::Splay(
  19. SplayNode<T> *node
  20. )
  21. {
  22. SplayNode<T> *Current = node;
  23. SplayNode<T> *Parent;
  24. SplayNode<T> *GrandParent;
  25. while ( !IsRoot( Current ) ) {
  26. Parent = Current->_Parent;
  27. GrandParent = Parent->_Parent;
  28. if ( IsLeftChild( Current ) ) {
  29. if ( IsRoot( Parent ) ) {
  30. //
  31. // P C
  32. // / \ / \
  33. // C x ==> y P
  34. // / \ / \
  35. // y z z x
  36. //
  37. Parent->_LeftChild = Current->_RightChild;
  38. if ( Parent->_LeftChild != NULL ) {
  39. Parent->_LeftChild->_Parent = Parent;
  40. }
  41. Current->_RightChild = Parent;
  42. _Root = Current;
  43. Current->_Parent = NULL;
  44. Parent->_Parent = Current;
  45. }
  46. else
  47. if ( IsLeftChild( Parent ) ) {
  48. //
  49. // | |
  50. // G C
  51. // / \ / \
  52. // P z ==> u P
  53. // / \y / \
  54. // C x G
  55. // / \ / \
  56. // u x y z
  57. //
  58. // connect Parent and x
  59. Parent->_LeftChild = Current->_RightChild;
  60. if ( Parent->_LeftChild != NULL ) {
  61. Parent->_LeftChild->_Parent = Parent;
  62. }
  63. // connect GrandParent and y
  64. GrandParent->_LeftChild = Parent->_RightChild;
  65. if ( GrandParent->_LeftChild != NULL ) {
  66. GrandParent->_LeftChild->_Parent = GrandParent;
  67. }
  68. // connect Current to Great GrandParent or assign as Root
  69. if ( IsRoot( GrandParent ) ) {
  70. _Root = Current;
  71. Current->_Parent = NULL;
  72. }
  73. else {
  74. Current->_Parent = GrandParent->_Parent;
  75. if ( IsLeftChild( GrandParent ) ) {
  76. Current->_Parent->_LeftChild = Current;
  77. }
  78. else { // GrandParent was a RightChild
  79. Current->_Parent->_RightChild = Current;
  80. }
  81. }
  82. // connect Current and Parent
  83. Current->_RightChild = Parent;
  84. Parent->_Parent = Current;
  85. // connect Parent and GrandParent
  86. Parent->_RightChild = GrandParent;
  87. GrandParent->_Parent = Parent;
  88. }
  89. else { // else Parent is a RightChild
  90. //
  91. // | |
  92. // G C
  93. // / \ / \
  94. // u P G P
  95. // / \ ==> / \ / \
  96. // C z u x y z
  97. // / \
  98. // x y
  99. //
  100. // connect GrandParent and x
  101. GrandParent->_RightChild = Current->_LeftChild;
  102. if ( GrandParent->_RightChild != NULL ) {
  103. GrandParent->_RightChild->_Parent = GrandParent;
  104. }
  105. // connect Parent and y
  106. Parent->_LeftChild = Current->_RightChild;
  107. if ( Parent->_LeftChild != NULL ) {
  108. Parent->_LeftChild->_Parent = Parent;
  109. }
  110. // connect Current and Great GrandParent or assign as Root
  111. if ( IsRoot( GrandParent ) ) {
  112. _Root = Current;
  113. Current->_Parent = NULL;
  114. }
  115. else {
  116. Current->_Parent = GrandParent->_Parent;
  117. if ( IsLeftChild( GrandParent ) ) {
  118. Current->_Parent->_LeftChild = Current;
  119. }
  120. else { // GrandParent was a RightChild
  121. Current->_Parent->_RightChild = Current;
  122. }
  123. }
  124. // connect Current to GrandParent
  125. Current->_LeftChild = GrandParent;
  126. GrandParent->_Parent = Current;
  127. // connect Current to Parent
  128. Current->_RightChild = Parent;
  129. Parent->_Parent = Current;
  130. }
  131. }
  132. else { // else Current is a RightChild
  133. if ( IsRoot( Parent ) ){
  134. //
  135. // P C
  136. // / \ / \
  137. // x C ==> P z
  138. // / \ / \
  139. // y z x y
  140. //
  141. Parent->_RightChild = Current->_LeftChild;
  142. if ( Parent->_RightChild != NULL ) {
  143. Parent->_RightChild->_Parent = Parent;
  144. }
  145. Current->_LeftChild = Parent;
  146. _Root = Current;
  147. Current->_Parent = NULL;
  148. Parent->_Parent = Current;
  149. }
  150. else
  151. if ( IsRightChild( Parent ) ) {
  152. //
  153. // | |
  154. // G C
  155. // / \ / \
  156. // u P P z
  157. // / \ ==> / \
  158. // x C G y
  159. // / \ / \
  160. // y z u x
  161. //
  162. // connect Parent and x
  163. Parent->_RightChild = Current->_LeftChild;
  164. if ( Parent->_RightChild != NULL ) {
  165. Parent->_RightChild->_Parent = Parent;
  166. }
  167. // connect GrandParent and y
  168. GrandParent->_RightChild = Parent->_LeftChild;
  169. if ( GrandParent->_RightChild != NULL ) {
  170. GrandParent->_RightChild->_Parent = GrandParent;
  171. }
  172. // connect Current to Great GrandParent or assign as Root
  173. if ( IsRoot( GrandParent ) ) {
  174. _Root = Current;
  175. Current->_Parent = NULL;
  176. }
  177. else {
  178. Current->_Parent = GrandParent->_Parent;
  179. if ( IsLeftChild( GrandParent ) ) {
  180. Current->_Parent->_LeftChild = Current;
  181. }
  182. else { // GrandParent was a RightChild
  183. Current->_Parent->_RightChild = Current;
  184. }
  185. }
  186. // connect Current and Parent
  187. Current->_LeftChild = Parent;
  188. Parent->_Parent = Current;
  189. // connect Parent and GrandParent
  190. Parent->_LeftChild = GrandParent;
  191. GrandParent->_Parent = Parent;
  192. }
  193. else { // else Parent is a LeftChild
  194. //
  195. // | |
  196. // G C
  197. // / \ / \
  198. // P z P G
  199. // / \ ==> / \ / \
  200. // u C u x y z
  201. // / \
  202. // x y
  203. //
  204. // connect GrandParent and x
  205. GrandParent->_LeftChild = Current->_RightChild;
  206. if ( GrandParent->_LeftChild != NULL ) {
  207. GrandParent->_LeftChild->_Parent = GrandParent;
  208. }
  209. // connect Parent and y
  210. Parent->_RightChild = Current->_LeftChild;
  211. if ( Parent->_RightChild != NULL ) {
  212. Parent->_RightChild->_Parent = Parent;
  213. }
  214. // connect Current and Great GrandParent or assign as Root
  215. if ( IsRoot( GrandParent ) ) {
  216. _Root = Current;
  217. Current->_Parent = NULL;
  218. }
  219. else {
  220. Current->_Parent = GrandParent->_Parent;
  221. if ( IsLeftChild( GrandParent ) ) {
  222. Current->_Parent->_LeftChild = Current;
  223. }
  224. else { // GrandParent was a RightChild
  225. Current->_Parent->_RightChild = Current;
  226. }
  227. }
  228. // connect Current to GrandParent
  229. Current->_RightChild = GrandParent;
  230. GrandParent->_Parent = Current;
  231. // connect Current to Parent
  232. Current->_LeftChild = Parent;
  233. Parent->_Parent = Current;
  234. }
  235. }
  236. }
  237. }
  238. template<class T>
  239. void
  240. SplayTree<T>::Delete(
  241. SplayNode<T> * node
  242. )
  243. {
  244. SplayNode<T> *x;
  245. SplayNode<T> *y;
  246. if ( node == NULL ) {
  247. return;
  248. }
  249. if ( ( node->_RightChild == NULL ) ||
  250. ( node->_LeftChild == NULL ) ) {
  251. y = node;
  252. }
  253. else {
  254. y = Successor( node );
  255. }
  256. if ( y->_LeftChild != NULL ) {
  257. x = y->_LeftChild;
  258. }
  259. else {
  260. x = y->_RightChild;
  261. }
  262. if ( x != NULL ) {
  263. x->_Parent = y->_Parent;
  264. }
  265. if ( y->_Parent == NULL ) {
  266. _Root = x;
  267. }
  268. else if ( IsLeftChild( y ) ) {
  269. y->_Parent->_LeftChild = x;
  270. }
  271. else {
  272. y->_Parent->_RightChild = x;
  273. }
  274. if ( y != node ) {
  275. node->_Data = y->_Data;
  276. }
  277. if ( y ) {
  278. delete y;
  279. }
  280. return;
  281. }
  282. //
  283. // This function takes a pointer to two nodes. The parent node must
  284. // be a member of the tree and must NOT have a right child. The child
  285. // node must NOT be a current member of the tree, and must NOT
  286. // already have a parent
  287. //
  288. template<class T>
  289. SplayNode<T>*
  290. SplayTree<T>::InsertAsRightChild(
  291. SplayNode<T> *Parent,
  292. SplayNode<T> *Child
  293. )
  294. {
  295. if ( ( Parent == NULL ) || ( Child == NULL ) ) {
  296. return NULL;
  297. }
  298. if ( Parent->_RightChild == NULL ) {
  299. Parent->_RightChild = Child;
  300. Child->_Parent = Parent;
  301. return Child;
  302. }
  303. return NULL;
  304. }
  305. //
  306. // This function takes a pointer to two nodes. The parent node must
  307. // be a member of the tree and must NOT have a left child. The child
  308. // node must NOT be a current member of the tree, and must NOT
  309. // already have a parent
  310. //
  311. template<class T>
  312. SplayNode<T>*
  313. SplayTree<T>::InsertAsLeftChild(
  314. SplayNode<T> *Parent,
  315. SplayNode<T> *Child
  316. )
  317. {
  318. if ( ( Parent == NULL ) || ( Child == NULL ) ) {
  319. return NULL;
  320. }
  321. if ( Parent->_LeftChild == NULL ) {
  322. Parent->_LeftChild = Child;
  323. Child->_Parent = Parent;
  324. return Child;
  325. }
  326. return NULL;
  327. }
  328. //
  329. // The Successor takes an input to a node in the tree
  330. // and returns a pointer to the successor of that
  331. // node in the entire tree. If there is no successor
  332. // a NULL value is returned.
  333. //
  334. template<class T>
  335. SplayNode<T>*
  336. SplayTree<T>::Successor(
  337. SplayNode<T> *Node
  338. )
  339. {
  340. if ( Node == NULL ) {
  341. return NULL;
  342. }
  343. // if a success exists in a subtree ...
  344. SplayNode<T> *Temp = SubtreeSuccessor( Node );
  345. if ( Temp ) {
  346. return Temp;
  347. }
  348. // else there is no right child, so find the first
  349. // ancestor that we are the left decendent of.
  350. Temp = Node;
  351. while (IsRightChild( Temp ) ) {
  352. Temp = Temp->_Parent;
  353. }
  354. if ( IsLeftChild( Temp ) ) {
  355. return Temp->_Parent;
  356. }
  357. return NULL;
  358. }
  359. //
  360. // The Predecessor takes an input to a node in the tree
  361. // and returns a pointer to the predecessor of that
  362. // node in the entire tree. If there is no predecessor,
  363. // a NULL value is returned.
  364. //
  365. template<class T>
  366. SplayNode<T>*
  367. SplayTree<T>::Predecessor(
  368. SplayNode<T> *Node
  369. )
  370. {
  371. if ( Node == NULL ) {
  372. return NULL;
  373. }
  374. // if a predecessor exists in the subtree
  375. SplayNode<T> *Temp = SubtreePredecessor( Node );
  376. if ( Temp ) {
  377. return Temp;
  378. }
  379. // else there is no left child, so find the first
  380. // ancestor that we are the right decendent of
  381. Temp = Node;
  382. while ( IsLeftChild( Temp ) ) {
  383. Temp = Temp->_Parent;
  384. }
  385. if ( IsRightChild( Temp ) ) {
  386. return Temp->_Parent;
  387. }
  388. return NULL;
  389. }
  390. //
  391. // The SubtreePredecessor takes an input to a node in the tree
  392. // and returns a pointer to the predecessor of a subtree
  393. // rooted at the input node. If there is no predecessor, a
  394. // NULL value is returned.
  395. //
  396. template<class T>
  397. SplayNode<T>*
  398. SplayTree<T>::SubtreePredecessor(
  399. SplayNode<T> *Node
  400. )
  401. {
  402. if ( Node == NULL ) {
  403. return NULL;
  404. }
  405. // the predecessor is the right-most node in the left sub-tree
  406. SplayNode<T> *Temp = Node->_LeftChild;
  407. if ( Temp != NULL ) {
  408. while ( Temp->_RightChild != NULL ) {
  409. Temp = Temp->_RightChild;
  410. }
  411. return Temp;
  412. }
  413. return NULL;
  414. }
  415. //
  416. // The SubtreeSuccessor takes an input to a node in the tree
  417. // and returns a pointer to the successor of a subtree
  418. // rooted at the input node. If there is no successor, a
  419. // NULL value is returned.
  420. //
  421. template<class T>
  422. SplayNode<T>*
  423. SplayTree<T>::SubtreeSuccessor(
  424. SplayNode<T> *Node
  425. )
  426. {
  427. if ( Node == NULL ) {
  428. return NULL;
  429. }
  430. // the successor is the left-most node in the right sub tree
  431. SplayNode<T> *Temp = Node->_RightChild;
  432. if ( Temp != NULL ) {
  433. while ( Temp->_LeftChild != NULL ) {
  434. Temp = Temp->_LeftChild;
  435. }
  436. return Temp;
  437. }
  438. return NULL;
  439. }
  440. template<class T>
  441. void
  442. SplayTree<T>::Insert(
  443. T *NewData
  444. )
  445. {
  446. SplayNode<T> *Temp = NULL;
  447. SplayNode<T> *TempRoot = _Root;
  448. SplayNode<T> *NewNode = NULL;
  449. while ( TempRoot != NULL ) {
  450. Temp = TempRoot;
  451. ASSERT( *( NewData ) != *( TempRoot->_Data ) );
  452. //if ( *( NewData ) == *( TempRoot->_Data ) ) {
  453. // return;
  454. //}
  455. if ( *( NewData ) < *( TempRoot->_Data ) ) {
  456. TempRoot = TempRoot->_LeftChild;
  457. }
  458. //else {
  459. else if ( *( NewData ) > *( TempRoot->_Data ) ) {
  460. TempRoot = TempRoot->_RightChild;
  461. }
  462. else return; // *NewData == *TempRoot->_Data
  463. }
  464. if ( Temp == NULL ) {
  465. _Root = new SplayNode<T>( NewData );
  466. }
  467. else
  468. if ( *( NewData ) < *( Temp->_Data ) ) {
  469. NewNode = InsertAsLeftChild( Temp, new SplayNode<T>( NewData ) );
  470. Splay( NewNode );
  471. }
  472. else {
  473. NewNode = InsertAsRightChild( Temp, new SplayNode<T>( NewData ) );
  474. Splay( NewNode );
  475. }
  476. _Size++;
  477. return;
  478. }
  479. //
  480. // Given an element, find the element in the tree
  481. // (if it exists) and return a pointer to the element.
  482. // If the element is not found, then the return
  483. // value is NULL.
  484. //
  485. template<class T>
  486. T*
  487. SplayTree<T>::Find(
  488. T *FindData
  489. )
  490. {
  491. if ( FindData == NULL ) {
  492. return NULL;
  493. }
  494. SplayNode<T> *DataNode = Find( _Root, FindData );
  495. if ( DataNode == NULL ) {
  496. return NULL;
  497. }
  498. Splay( DataNode );
  499. return ( DataNode->_Data );
  500. }
  501. //
  502. // Given a pointer to a node (which is the root of a
  503. // subtree) and a pointer to an element, determine
  504. // if the element exists in the tree.
  505. // If the element exist, return a pointer to the node
  506. // containing such an element, or return a NULL.
  507. //
  508. template<class T>
  509. SplayNode<T>*
  510. SplayTree<T>::Find(
  511. SplayNode<T> *SubRoot,
  512. T *FindData
  513. )
  514. {
  515. //if ( ( SubRoot == NULL ) ||
  516. // ( *( FindData ) == *( SubRoot->_Data ) ) ) {
  517. // return SubRoot;
  518. //}
  519. if ( !SubRoot ) {
  520. return SubRoot;
  521. }
  522. if ( *( FindData ) < *( SubRoot->_Data ) ) {
  523. return Find( SubRoot->_LeftChild, FindData );
  524. }
  525. else if ( *( FindData ) > * ( SubRoot->_Data ) ) {
  526. return Find( SubRoot->_RightChild, FindData );
  527. }
  528. else return SubRoot; // *FindData == *SubRoot->_Data
  529. }
  530. //
  531. // Given an element, find the element in the tree
  532. // (if it exists) and delete the element from the
  533. // tree. The function returns a pointer to the
  534. // element. If the element is not found, then
  535. // the return value is NULL.
  536. //
  537. template<class T>
  538. T*
  539. SplayTree<T>::Delete(
  540. T *DeleteData
  541. )
  542. {
  543. SplayNode<T> *DeleteNode = Find( _Root, DeleteData );
  544. // must copy the data out of the node, since a Delete( ..)
  545. // is allowed to change the data within the node
  546. T *Data = DeleteNode->_Data;
  547. if ( DeleteNode ) {
  548. Delete( DeleteNode );
  549. _Size--;
  550. return Data;
  551. }
  552. return NULL;
  553. }
  554. //
  555. // Find the node in the tree with the smallest value,
  556. // and return a pointer to such a node.
  557. // If there are no nodes in the tree, return a NULL.
  558. //
  559. template<class T>
  560. SplayNode<T>*
  561. SplayTree<T>::MinimumNode( )
  562. {
  563. SplayNode<T> *SubRoot = _Root;
  564. if ( _Root == NULL ) {
  565. return NULL;
  566. }
  567. while ( SubRoot->_LeftChild != NULL ) {
  568. SubRoot = SubRoot->_LeftChild;
  569. }
  570. return SubRoot;
  571. }
  572. //
  573. // Find the node in the tree with the largest value.
  574. // and return a pointer to such a node.
  575. // If there are no nodes in the tree, return a NULL.
  576. //
  577. template<class T>
  578. SplayNode<T>*
  579. SplayTree<T>::MaximumNode( )
  580. {
  581. SplayNode<T> *SubRoot = _Root;
  582. if ( _Root == NULL ) {
  583. return NULL;
  584. }
  585. while ( SubRoot->_RightChild != NULL ) {
  586. SubRoot = SubRoot->_RightChild;
  587. }
  588. return SubRoot;
  589. }
  590. template<class T>
  591. T*
  592. SplayTree<T>::Successor( T *Data )
  593. {
  594. SplayNode<T> *DataNode;
  595. SplayNode<T> *SuccNode;
  596. if ( Data ) {
  597. DataNode = Find( _Root, Data );
  598. if ( DataNode ) {
  599. SuccNode = Successor( DataNode );
  600. if ( SuccNode ) {
  601. Splay( SuccNode );
  602. return SuccNode->_Data;
  603. }
  604. }
  605. }
  606. return NULL;
  607. }
  608. template<class T>
  609. T*
  610. SplayTree<T>::Predecessor( T *Data )
  611. {
  612. SplayNode<T> *DataNode;
  613. SplayNode<T> *PredNode;
  614. if ( Data ) {
  615. DataNode = Find( _Root, Data );
  616. if ( DataNode ) {
  617. PredNode = Predecessor( DataNode );
  618. if ( PredNode ) {
  619. Splay( PredNode );
  620. return PredNode->_Data;
  621. }
  622. }
  623. }
  624. return NULL;
  625. }
  626. #ifdef DEBUGRPC
  627. template<class T>
  628. void
  629. SplayTree<T>::Print()
  630. {
  631. if ( _Root == NULL ) {
  632. return;
  633. }
  634. Print( _Root );
  635. }
  636. template<class T>
  637. void
  638. SplayTree<T>::Print( SplayNode<T> *TempRoot )
  639. {
  640. if ( TempRoot == NULL ) {
  641. return;
  642. }
  643. Print( TempRoot->_LeftChild );
  644. if ( IsLeftChild( TempRoot ) ) {
  645. if ( *( TempRoot->_Parent->_Data ) < *( TempRoot->_Data ) ) {
  646. cout << "Bastard tree! ";
  647. }
  648. else cout << "OK ";
  649. }
  650. else
  651. if ( IsRightChild( TempRoot ) ) {
  652. if ( *( TempRoot->_Parent->_Data ) > *( TempRoot->_Data ) ) {
  653. cout << "Bastard tree! ";
  654. }
  655. else cout << "OK ";
  656. }
  657. cout << *( TempRoot->_Data ) << " \n";
  658. Print( TempRoot->_RightChild );
  659. return;
  660. }
  661. template<class T>
  662. unsigned int
  663. SplayTree<T>::Depth( SplayNode<T> *TempRoot, unsigned int CurrentDepth )
  664. {
  665. unsigned int right = 0;
  666. unsigned int left = 0;
  667. if ( TempRoot->_RightChild ) {
  668. right = Depth( TempRoot->_RightChild, CurrentDepth + 1 );
  669. }
  670. if ( TempRoot->_LeftChild ) {
  671. left = Depth( TempRoot->_LeftChild, CurrentDepth + 1 );
  672. }
  673. if ( ( right > left ) && ( right > CurrentDepth ) ) {
  674. return right;
  675. }
  676. else
  677. if ( (left > right ) && ( left > CurrentDepth ) ) {
  678. return left;
  679. }
  680. return ( CurrentDepth + 1 );
  681. }
  682. #endif // DEBUGRPC