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.

213 lines
4.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1995.
  5. //
  6. // File: avl.hxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 6/25/1995 RaviR Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #ifndef _AVL_HXX_
  18. #define _AVL_HXX_
  19. //----------------------------------------------------------------------------
  20. //
  21. // Class: CAVLNodeAtom
  22. //
  23. // History: 6/29/1995 RaviR Created
  24. //
  25. //----------------------------------------------------------------------------
  26. class CAVLNodeAtom;
  27. typedef CAVLNodeAtom * PAVLNODEATOM;
  28. class CAVLNodeAtom
  29. {
  30. public:
  31. CAVLNodeAtom() {;}
  32. virtual ~CAVLNodeAtom() {;}
  33. virtual int Compare(CAVLNodeAtom *pCAVLNodeAtom) = 0;
  34. #if (DBG == 1)
  35. virtual void Dump(void) {;}
  36. #endif
  37. };
  38. //----------------------------------------------------------------------------
  39. //
  40. // Class: CTreeNode
  41. //
  42. // History: 8/29/1995 RaviR Created
  43. //
  44. //----------------------------------------------------------------------------
  45. class CTreeNode
  46. {
  47. public:
  48. CTreeNode(PAVLNODEATOM pa)
  49. : _l(NULL), _r(NULL), _p(NULL), _pa(pa), _b(0), _flag(0) {;}
  50. ~CTreeNode()
  51. {
  52. delete _pa;
  53. delete _l;
  54. delete _r;
  55. }
  56. PAVLNODEATOM Atom(void) { return _pa; }
  57. char Balance(void) { return _b; }
  58. CTreeNode * Left(void) { return _l; }
  59. CTreeNode * Right(void) { return _r; }
  60. CTreeNode * Parent(void) { return _p; }
  61. void SetAtom(PAVLNODEATOM pa) { _pa = pa; }
  62. void SetBalance(char b) { _b = b; }
  63. void SetLeft(CTreeNode *l) { _l = l; if (l) l->_p = this; }
  64. void SetRight(CTreeNode *r) { _r = r; if (r) r->_p = this; }
  65. void SetParent(CTreeNode *p) { _p = p; }
  66. PAVLNODEATOM GetMin(void); // Methods reqd for Delete
  67. PAVLNODEATOM GetMax(void); // Methods reqd for Delete
  68. #if (DBG == 1)
  69. void Dump(void);
  70. #endif
  71. CTreeNode * _l; // left child
  72. CTreeNode * _r; // right child
  73. CTreeNode * _p; // parent
  74. PAVLNODEATOM _pa; // ptr to atom
  75. char _b; // balance
  76. BYTE _flag; // flag used by CAVLTree::GetFirst & CAVLTree::GetNext
  77. BOOL IsTraversed(void) { return _flag; }
  78. void SetTraversed(void) { _flag = TRUE; }
  79. void ResetTraversedFlag(void)
  80. {
  81. _flag = FALSE;
  82. if (_l != NULL)
  83. {
  84. _l->ResetTraversedFlag();
  85. }
  86. if (_r != NULL)
  87. {
  88. _r->ResetTraversedFlag();
  89. }
  90. }
  91. }; // class CTreeNode
  92. inline
  93. PAVLNODEATOM
  94. CTreeNode::GetMin(void)
  95. {
  96. CTreeNode * ptn = this;
  97. while (ptn->Left() != NULL)
  98. {
  99. ptn = ptn->Left();
  100. }
  101. return ptn->Atom();
  102. }
  103. inline
  104. PAVLNODEATOM
  105. CTreeNode::GetMax(void)
  106. {
  107. CTreeNode * ptn = this;
  108. while (ptn->Right() != NULL)
  109. {
  110. ptn = ptn->Right();
  111. }
  112. return ptn->Atom();
  113. }
  114. //----------------------------------------------------------------------------
  115. //
  116. // Class: CAVLTree
  117. //
  118. // History: 8/29/1995 RaviR Created
  119. //
  120. //----------------------------------------------------------------------------
  121. class CAVLTree
  122. {
  123. public:
  124. CAVLTree(void)
  125. : _pFirstNode(NULL),
  126. _pCurrentNode(NULL) {;}
  127. virtual ~CAVLTree()
  128. {
  129. delete _pFirstNode;
  130. }
  131. HRESULT Insert(PAVLNODEATOM pa);
  132. PAVLNODEATOM GetAtom(PAVLNODEATOM pa);
  133. PAVLNODEATOM GetFirst(void);
  134. PAVLNODEATOM GetNext(void);
  135. void Clear(void)
  136. {
  137. if (_pFirstNode != NULL)
  138. {
  139. delete _pFirstNode;
  140. _pFirstNode = NULL;
  141. }
  142. }
  143. void Delete(PAVLNODEATOM pa);
  144. #if (DBG == 1)
  145. void Dump(void);
  146. #endif
  147. private:
  148. BOOL _Insert(CTreeNode ** ppNode, PAVLNODEATOM pa);
  149. BOOL _Delete(CTreeNode ** ppNode, PAVLNODEATOM pa);
  150. CTreeNode * _pFirstNode; // First node in the tree.
  151. CTreeNode * _pCurrentNode; // used by getfirst getnext
  152. HRESULT _hrInsert; // used by Insert & _Insert
  153. }; // class CAVLTree
  154. #endif // _AVL_HXX_