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.

138 lines
3.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1999
  6. //
  7. // File: splaytre.hxx
  8. //
  9. //--------------------------------------------------------------------------
  10. ////////////////////////////////////////////////////////////
  11. //
  12. // File name: splaytree.hxx
  13. //
  14. // Title: Splay Tree class
  15. //
  16. // Desciption:
  17. //
  18. // Author: Scott Holden (t-scotth)
  19. //
  20. // Date: August 16, 1994
  21. //
  22. ////////////////////////////////////////////////////////////
  23. #ifndef _SPLAY_TREE_HXX_
  24. #define _SPLAY_TREE_HXX_
  25. typedef int BOOLEAN;
  26. #define TRUE 1
  27. #define FALSE 0
  28. #define NULL 0
  29. template<class T> class SplayTree;
  30. template<class T>
  31. class SplayNode {
  32. friend class SplayTree<T>;
  33. private:
  34. SplayNode<T> *_Parent;
  35. SplayNode<T> *_RightChild;
  36. SplayNode<T> *_LeftChild;
  37. T *_Data;
  38. SplayNode( ) : _Data( NULL ),
  39. _Parent( NULL ),
  40. _RightChild( NULL ),
  41. _LeftChild( NULL ) { }
  42. SplayNode( T *a ) : _Data( a ),
  43. _Parent( NULL ),
  44. _RightChild( NULL ),
  45. _LeftChild( NULL ) { }
  46. ~SplayNode() { }
  47. };
  48. template<class T>
  49. class SplayTree {
  50. friend class SplayNode<T>;
  51. private:
  52. SplayNode<T> *_Root;
  53. unsigned int _Size;
  54. void Splay ( SplayNode<T> * );
  55. // returns pointer to the root of the tree
  56. void Delete( SplayNode<T> * );
  57. SplayNode<T>* InsertAsRightChild( SplayNode<T> *, SplayNode<T> * );
  58. SplayNode<T>* InsertAsLeftChild ( SplayNode<T> *, SplayNode<T> * );
  59. BOOLEAN IsRoot ( SplayNode<T> *a )
  60. { return ( a == _Root ? TRUE : FALSE ); }
  61. BOOLEAN IsRightChild( SplayNode<T> *a )
  62. {
  63. if ( a->_Parent != NULL) {
  64. return ( a == a->_Parent->_RightChild ? TRUE : FALSE );
  65. }
  66. return FALSE;
  67. }
  68. BOOLEAN IsLeftChild( SplayNode<T> *a )
  69. {
  70. if ( a->_Parent != NULL ) {
  71. return ( a == a->_Parent->_LeftChild ? TRUE : FALSE );
  72. }
  73. return FALSE;
  74. }
  75. SplayNode<T>* Successor ( SplayNode<T> * );
  76. SplayNode<T>* Predecessor ( SplayNode<T> * );
  77. SplayNode<T>* SubtreeSuccessor ( SplayNode<T> * );
  78. SplayNode<T>* SubtreePredecessor( SplayNode<T> * );
  79. SplayNode<T>* Find( SplayNode<T> *, T * );
  80. SplayNode<T>* MaximumNode( );
  81. SplayNode<T>* MinimumNode( );
  82. public:
  83. //SplayTree() : _Root( NULL ), _Size( 0 ) { }
  84. SplayTree( T *a = NULL ) : _Root( NULL ), _Size( 0 ) { if ( a != NULL ) { Insert( a ); } }
  85. ~SplayTree();
  86. void Insert( T * );
  87. T* Find ( T * );
  88. T* Delete( T * );
  89. T* Minimum( )
  90. {
  91. SplayNode<T> *Temp = MinimumNode( );
  92. return Temp->_Data;
  93. }
  94. T* Maximum( )
  95. {
  96. SplayNode<T> *Temp = MaximumNode( );
  97. return Temp->_Data;
  98. }
  99. T* Successor ( T * );
  100. T* Predecessor( T * );
  101. unsigned int Size() { return _Size; }
  102. #ifdef DEBUGRPC
  103. void Print( );
  104. unsigned int Depth( ) { return Depth( _Root, 0 ); }
  105. private:
  106. void Print( SplayNode<T> * );
  107. unsigned int Depth( SplayNode<T> *, unsigned int );
  108. #endif // DEBUGRPC
  109. };
  110. #include "splaytre.inl"
  111. #endif // _SPLAY_TREE_HXX_