home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / bstreeb.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-14  |  6.0 KB  |  182 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: bstreeb.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 01/23/1997 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25.  
  26. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  27. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  28. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  29. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  30. CORRECTION.
  31.  
  32. Abstract binary search tree node functions. These functions
  33. assume the nodes are part of binary search trees, but do not
  34. depend on the data stored in the nodes.
  35. */
  36. // ----------------------------------------------------------- //   
  37.  
  38. #include "bstreeb.h"
  39.  
  40. BNodeBase *Minimum(BNodeBase *Tree)
  41. // Assuming that Tree is a binary search tree, this function
  42. // returns the minimum node in the tree, which can be
  43. // found by going as far left as possible. If Tree is null,
  44. // a null is returned, else a pointer to the minimun
  45. // node is returned.
  46. {
  47.   if (Tree) {
  48.     while(Tree->Left) Tree = Tree->Left;
  49.   }
  50.   return Tree;
  51. }
  52.  
  53. BNodeBase *Maximum(BNodeBase *Tree)
  54. // Assuming that Tree is a binary search tree, this function
  55. // returns the maximum node in the tree, which can be
  56. // found by going as far right as possible. If Tree is null,
  57. // a null is returned, else a pointer to the minimun
  58. // node is returned.
  59. {
  60.   if (Tree) {
  61.     while(Tree->Right) Tree = Tree->Right;
  62.   }
  63.   return Tree;
  64. }
  65.  
  66. BNodeBase *ParentOfMinimum(BNodeBase *Tree)
  67. // Assuming that Tree is a binary search tree, this function
  68. // returns a pointer to the parent of the minimum node
  69. // in the tree, which can be found by going as far left
  70. // as possible. If Tree is null, or is itself the minimum,
  71. // a null is returned.
  72. {
  73.   BNodeBase *ptr = 0;
  74.   if (Tree) {
  75.     while(Tree->Left) {
  76.        ptr = Tree;
  77.        Tree = Tree->Left;
  78.     }
  79.   }
  80.   return ptr;
  81. }
  82.  
  83. BNodeBase *ParentOfMaximum(BNodeBase *Tree)
  84. // Assuming that Tree is a binary search tree, this function
  85. // returns a pointer to the parent of the maximum node
  86. // in the tree, which can be found by going as far right
  87. // as possible. If Tree is null, or is itself the maximum,
  88. // a null is returned.
  89. {
  90.   BNodeBase *ptr = 0;
  91.   if (Tree) {
  92.     while(Tree->Right) {
  93.        ptr = Tree;
  94.        Tree = Tree->Right;
  95.     }
  96.   }
  97.   return ptr;
  98. }
  99.  
  100. BNodeBase *ParentOfPredecessor(BNodeBase *Tree)
  101. // Returns parent of predecessor of Tree, assumed to be 
  102. // a binary search tree. Predecessor is the right child
  103. // of node returned, unless Tree itself is the parent. Then 
  104. // the left child is the predecessor. If Tree is a leaf, a 
  105. // 0 is returned. Assumes Tree is not Tree null.
  106. {
  107.   BNodeBase *ptr = 0;
  108.   // Go left, then all the way right
  109.   BNodeBase *q = Tree->Left;
  110.   if (q) {
  111.      ptr = Tree;
  112.      while(q->Right) {
  113.        ptr = q;
  114.        q = q->Right;
  115.      }
  116.   }
  117.   return ptr;
  118. }
  119.  
  120. BNodeBase *ParentOfSuccessor(BNodeBase *Tree)
  121. // Returns parent of successor of Tree, assumed to be 
  122. // a binary search tree. Successor is the left child
  123. // of node returned, unless Tree itself is the parent.
  124. // Then the right child is the successor. If Tree is a 
  125. // leaf, a 0 is returned. Assumes Tree is not Tree null.
  126. {
  127.   BNodeBase *ptr = 0;
  128.   // Go right, then all the way left
  129.   BNodeBase *q = Tree->Right;
  130.   if (q) {
  131.      ptr = Tree;
  132.      while(q->Left) {
  133.        ptr = q;
  134.        q = q->Left;
  135.      }
  136.   }
  137.   return ptr;
  138. }
  139.  
  140. BNodeBase *DetachNode
  141. (BNodeBase *&Root, BNodeBase *Tree, BNodeBase *p, int Side)
  142. // Detaches node Tree with parent p from the tree. Node Tree is
  143. // the left child if Side = 0, else it's the right child.
  144. // If p is 0, it means Tree is the root, and that is handled
  145. // accordingly. Redundantly returns the pointer Tree. May
  146. // have to update root pointer.
  147. {
  148.   BNodeBase *psucc, *replacement;
  149.  
  150.   if (Tree) {
  151.      if (Tree->Left == 0 || Tree->Right == 0) {
  152.         // At least one child is null, so use the other 
  153.         // as the replacement. (It may be null too.)
  154.         replacement = (Tree->Left) ? Tree->Left : Tree->Right;
  155.      }
  156.      else { // Neither child is null
  157.         psucc = ParentOfSuccessor(Tree); // guaranteed not null
  158.         if (psucc == Tree) { // Immediate successor
  159.            replacement = psucc->Right;
  160.         }
  161.         else { 
  162.            // Detach replacement from where it is and relocate
  163.            // it to where Tree used to be.
  164.            replacement = psucc->Left;
  165.            psucc->Left = psucc->Left->Right;
  166.            replacement->Right = Tree->Right;
  167.         }
  168.         // Finish relocating replacement to go where Tree used to.
  169.         replacement->Left = Tree->Left;
  170.      }
  171.      if (p) { // Fixup parent of Tree to point to replacement
  172.         if (Side) p->Right = replacement; else p->Left = replacement;
  173.      }
  174.      else Root = replacement; // No parent, so Tree was the root
  175.   }
  176.   return Tree;
  177. }
  178. // ----------------------------------------------------------- // 
  179. // ------------------------------- //
  180. // --------- End of File --------- //
  181. // ------------------------------- //
  182.