Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. template <class Item>
  8. class BSNode
  9. {
  10. public:
  11. BSNode(Item data);
  12. BSNode(const BSNode<Item>& other);
  13.  
  14. ~BSNode();
  15.  
  16. void insert(Item value);
  17. BSNode<Item>& operator=(const BSNode<Item>& other);
  18.  
  19. bool isLeaf() const;
  20. Item getData() const;
  21. BSNode<Item>* getLeft() const;
  22. BSNode<Item>* getRight() const;
  23.  
  24. bool search(Item val) const;
  25.  
  26. int getHeight() const;
  27. int getDepth(const BSNode<Item>& root) const;
  28.  
  29. void printNodes() const; //for question 1 part C
  30.  
  31. private:
  32. Item _data;
  33. BSNode<Item>* _left;
  34. BSNode<Item>* _right;
  35.  
  36. int _count; //for question 1 part B
  37. int getCurrNodeDistFromInputNode(const BSNode* node) const; //auxiliary function for getDepth
  38.  
  39. };
  40.  
  41. template <class Item>
  42. BSNode<Item>::BSNode(Item data)
  43. {
  44. this->_data = data;
  45. this->_left = NULL;
  46. this->_right = NULL;
  47. }
  48.  
  49. template <class Item>
  50. BSNode<Item>::~BSNode()
  51. {
  52. if (this->_left != NULL)
  53. {
  54. delete this->_left;
  55. }
  56.  
  57. if (this->_right != NULL)
  58. {
  59. delete this->_right;
  60. }
  61. }
  62.  
  63. template <class Item>
  64. void BSNode<Item>::insert(Item value)
  65. {
  66. if (this->_data > value)
  67. {
  68. if (this->_left != NULL)
  69. {
  70. this->_left->insert(value);
  71. }
  72. else
  73. {
  74. this->_left = new BSNode<Item>(value);
  75. }
  76. }
  77. else if (this->_data < value)
  78. {
  79. if (this->_right != NULL)
  80. {
  81. this->_right->insert(value);
  82. }
  83. else
  84. {
  85. this->_right = new BSNode<Item>(value);
  86. }
  87. }
  88. }
  89.  
  90. template <class Item>
  91. BSNode<Item>* BSNode<Item>::getRight() const
  92. {
  93. return this->_right;
  94. }
  95.  
  96. template <class Item>
  97. BSNode<Item>* BSNode<Item>::getLeft() const
  98. {
  99. return this->_left;
  100. }
  101.  
  102. template <class Item>
  103. Item BSNode<Item>::getData() const
  104. {
  105. return this->_data;
  106. }
  107.  
  108. template <class Item>
  109. bool BSNode<Item>::isLeaf() const
  110. {
  111. return (this->_left == NULL && this->_right == NULL);
  112. }
  113.  
  114. template <class Item>
  115. bool BSNode<Item>::search(Item val) const
  116. {
  117. bool left = false;
  118. bool right = false;
  119.  
  120. if (this->_data == val)
  121. {
  122. return true;
  123. }
  124.  
  125. if (this->isLeaf())
  126. {
  127. return false;
  128. }
  129.  
  130. if (this->_left != NULL && this->_data > val)
  131. {
  132. left = this->_left->search(val);
  133. }
  134.  
  135. if (this->_right != NULL && this->_data < val)
  136. {
  137. right = this->_right->search(val);
  138. }
  139.  
  140. return left || right;
  141. }
  142.  
  143. template <class Item>
  144. int BSNode<Item>::getHeight() const
  145. {
  146. int left = 0;
  147. int right = 0;
  148.  
  149. if (this->_left != NULL)
  150. {
  151. left = 1 + this->_left->getHeight();
  152. }
  153.  
  154. if (this->_right != NULL)
  155. {
  156. right = this->_right->getHeight() + 1;
  157. }
  158.  
  159. return (right > left ? right : left);
  160. }
  161.  
  162. template <class Item>
  163. int BSNode<Item>::getDepth(const BSNode<Item>& root) const
  164. {
  165. if (this->_data == root._data)
  166. {
  167. return 0;
  168. }
  169.  
  170. if (root._left != NULL && root._data > this->_data)
  171. {
  172. return this->getDepth(*(root._left)) + 1;
  173. }
  174.  
  175. if (root._right != NULL && root._data < this->_data)
  176. {
  177. return this->getDepth(*(root._right)) + 1;
  178. }
  179.  
  180. return 0;
  181. }
  182.  
  183. template <class Item>
  184. BSNode<Item>::BSNode(const BSNode<Item>& other)
  185. {
  186. this->_data = other._data;
  187.  
  188. if (other._left != NULL)
  189. {
  190. this->_left = new BSNode<Item>(*other._left);
  191. }
  192. else
  193. {
  194. this->_left = NULL;
  195. }
  196.  
  197. if (other._right != NULL)
  198. {
  199. this->_right = new BSNode<Item>(*other._right);
  200. }
  201. else
  202. {
  203. this->_right = NULL;
  204. }
  205. }
  206.  
  207. template <class Item>
  208. BSNode<Item>& BSNode<Item>::operator=(const BSNode<Item>& other)
  209. {
  210. this->_data = other._data;
  211.  
  212. if (other._left != NULL)
  213. {
  214. this->_left = new BSNode<Item>(*(other._left));
  215. }
  216.  
  217. if (other._right != NULL)
  218. {
  219. this->_right = new BSNode<Item>(*other._right);
  220. }
  221.  
  222. return *this;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement