Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. #ifndef BST_CPP
  2. #define BST_CPP
  3.  
  4. #include "BST.h"
  5.  
  6. namespace cs20 {
  7.  
  8. template <class Object>
  9. BST<Object>::BST() {
  10. root = NULL;
  11. }
  12.  
  13. template <class Object>
  14. BST<Object>::BST( const BST<Object>& rhs ) {
  15. root = NULL;
  16. *this = rhs;
  17. }
  18.  
  19. template <class Object>
  20. BST<Object>::BST( const Object& rootElement ) {
  21. root = new BSTNode<Object>( rootElement );
  22. }
  23.  
  24. template <class Object>
  25. BST<Object>::~BST() {
  26. makeEmpty();
  27. }
  28.  
  29. template <class Object>
  30. bool BST<Object>::isEmpty() const {
  31. return( root == NULL );
  32. }
  33.  
  34. template <class Object>
  35. void BST<Object>::makeEmpty() {
  36. makeEmpty( root );
  37. }
  38.  
  39. template <class Object>
  40. void BST<Object>::makeEmpty( NodePtr & node ) {
  41. if (node != NULL) {
  42. NodePtr l = node->getLeftSide();
  43. NodePtr r = node->getRightSide();
  44. makeEmpty( l );
  45. makeEmpty( r );
  46. delete node;
  47. node = NULL;
  48. }
  49. }
  50.  
  51. template <class Object>
  52. int BST<Object>::size() const {
  53. return( BSTNode<Object>::size( root ) );
  54. }
  55.  
  56. template <class Object>
  57. int BST<Object>::height() const {
  58. return( BSTNode<Object>::height( root ) );
  59. }
  60.  
  61. template <class Object>
  62. const Object& BST<Object>::findMin() const throw(InvalidTreeArgument) {
  63. if (root == NULL)
  64. throw InvalidTreeArgument();
  65. return( findMin( root ) );
  66. }
  67.  
  68. template <class Object>
  69. const Object& BST<Object>::findMax() const throw(InvalidTreeArgument) {
  70. if (root == NULL)
  71. throw InvalidTreeArgument();
  72. return( findMax( root ) );
  73. }
  74.  
  75. template <class Object>
  76. const Object& BST<Object>::findMin( NodePtr node ) const {
  77. while( node->getLeftSide() != NULL ) {
  78. node = node->getLeftSide();
  79. }
  80. return( node->getElement() );
  81. }
  82.  
  83. template <class Object>
  84. const Object& BST<Object>::findMax( NodePtr node ) const {
  85. while( node->getRightSide() != NULL ) {
  86. node = node->getRightSide();
  87. }
  88. return( node->getElement() );
  89. }
  90.  
  91. template <class Object>
  92. const Object& BST<Object>::find( const Object& x ) const throw (InvalidTreeArgument) {
  93. return( find( x, root ) );
  94. }
  95.  
  96. template <class Object>
  97. const Object& BST<Object>::find( const Object& x, NodePtr node ) const throw (InvalidTreeArgument) {
  98. if (node == NULL)
  99. throw InvalidTreeArgument();
  100. if (node->getElement() < x) {
  101. return( find( x, node->getLeftSide() ) );
  102. }
  103. if (node->getElement() > x) {
  104. return( find( x, node->getRightSide() ) );
  105. }
  106. return( node->getElement() );
  107. }
  108.  
  109. template <class Object>
  110. void BST<Object>::insert( const Object& x ) throw (InvalidTreeArgument) {
  111. insert( x, root );
  112. }
  113.  
  114. template <class Object>
  115. void BST<Object>::insert( const Object& x, NodePtr& node ) throw (InvalidTreeArgument) {
  116. if (node == NULL) {
  117. node = new BSTNode<Object>( x, NULL, NULL );
  118. }
  119. else if (x < node->element) {
  120. insert( x, node->leftSide );
  121. }
  122. else if (x > node->element) {
  123. insert( x, node->rightSide );
  124. }
  125. else if(x == node->element) {
  126. //insert( x, node->rightSide );
  127. node->incCount();
  128. }
  129. else
  130. throw InvalidTreeArgument();
  131. }
  132.  
  133. template <class Object>
  134. void BST<Object>::removeMin() throw (InvalidTreeArgument) {
  135. removeMin( root );
  136. }
  137.  
  138.  
  139. template <class Object>
  140. void BST<Object>::removeMin( NodePtr& node ) throw (InvalidTreeArgument) {
  141. if (node == NULL) {
  142. throw InvalidTreeArgument();
  143. }
  144. else if (node->leftSide != NULL) {
  145. removeMin( node->leftSide );
  146. }
  147. else if (node->getCount() > 1) {
  148. node->decCount();
  149. }
  150. else{
  151. NodePtr temp = node;
  152. node = node->rightSide;
  153. delete temp;
  154. }
  155. }
  156.  
  157. template <class Object>
  158. void BST<Object>::remove( const Object& x ) throw (InvalidTreeArgument) {
  159. remove( x, root );
  160. }
  161.  
  162. template <class Object>
  163. void BST<Object>::remove( const Object& x, NodePtr & node ) throw (InvalidTreeArgument) {
  164. if (node == NULL)
  165. throw InvalidTreeArgument();
  166. else if (x < node->element)
  167. remove( x, node->leftSide );
  168. else if (x > node->element)
  169. remove( x, node->rightSide );
  170. // on the matching node
  171. else if (node->leftSide != NULL && node->rightSide != NULL) {
  172. // 2 children
  173. if( node->getCount() > 1 ) {
  174. node->decCount();
  175. }
  176. else {
  177. node->element = findMin( node->rightSide );
  178. removeMin( node->rightSide );
  179. }
  180.  
  181. }
  182. // one or no children
  183. else if( node->getCount() > 1 ) {
  184. node->decCount();
  185. }
  186. else {
  187. NodePtr temp = node;
  188. if (node->leftSide != NULL) {
  189. node = node->leftSide;
  190. }
  191. else {
  192. // if( node->rightSide != NULL)
  193. node = node->rightSide;
  194. }
  195. delete temp;
  196. }
  197. }
  198.  
  199.  
  200. // Deep copy of tree
  201. template <class Object>
  202. const BST<Object>& BST<Object>::operator =( const BST<Object>& rhs ) {
  203. if (this != &rhs) {
  204. makeEmpty();
  205. if (rhs.root != NULL)
  206. root = rhs.root->duplicate();
  207. }
  208. return( *this );
  209. }
  210.  
  211. template <class Object>
  212. std::ostream& BST<Object>::printBST( std::ostream& outs ) const {
  213. if (isEmpty())
  214. outs << "Empty BST";
  215. else
  216. root->printBSTNode( outs );
  217. outs << endl;
  218. return( outs );
  219. }
  220.  
  221. template <class Object>
  222. BSTNode<Object>* BST<Object>::getRoot() const {
  223. return( root );
  224. }
  225. }
  226.  
  227. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement