Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include "BinarySearchTree.h"
  2. #include <iostream>
  3.  
  4. // **Constructors **
  5.  
  6. std::string inorder_values = "";
  7. std::string preorder_values = "";
  8. std::string postorder_values = "";
  9. std::string insertedWord = "";
  10. bool valueExists = false;
  11.  
  12. BinarySearchTree::BinarySearchTree()
  13. {
  14.  
  15. }
  16.  
  17.  
  18. BinarySearchTree::BinarySearchTree(std::string word)
  19. {
  20. Node* node = new Node;
  21. node->word = word;
  22. node->left = node->right = nullptr;
  23.  
  24. /*Node* node1 = new Node;
  25. node1->word = "aasd";
  26. node1->left = node1->right = nullptr;
  27.  
  28. Node* node2 = new Node;
  29. node2->word = "zasd";
  30. node2->left = node2->right = nullptr;
  31.  
  32. node->left = node1;
  33. node->right = node2;*/
  34.  
  35. root = node;
  36. }
  37.  
  38.  
  39. BinarySearchTree::BinarySearchTree(const BinarySearchTree &rhs)
  40. {
  41.  
  42. }
  43.  
  44.  
  45. BinarySearchTree::BinarySearchTree(const std::vector<std::string> &words)
  46. {
  47.  
  48. }
  49. //destructor
  50. BinarySearchTree::~BinarySearchTree()
  51. {
  52.  
  53. }
  54.  
  55. // **Methods**
  56. void insertWord(Node* node, std::string wordValue) {
  57. if (node == NULL)
  58. {
  59. return;
  60. }
  61.  
  62. if (node->word == wordValue) {
  63. return;
  64. }
  65.  
  66. Node* tempNode = new Node;
  67. tempNode->word = wordValue;
  68. tempNode->left = tempNode->right = nullptr;
  69.  
  70. if (node->word > wordValue && node->left == NULL) {
  71. node->left = tempNode;
  72. return;
  73. }
  74. else if (node->word > wordValue && node->left != NULL) {
  75. insertWord(node->left, wordValue);
  76. }
  77.  
  78. if (node->word < wordValue && node->right == NULL) {
  79. node->right = tempNode;
  80. return;
  81. }
  82. else if (node->word < wordValue && node->right != NULL) {
  83. insertWord(node->right, wordValue);
  84. }
  85. }
  86.  
  87.  
  88. void BinarySearchTree::insert(std::string word)
  89. {
  90. if (root == NULL) {
  91. Node* node = new Node;
  92. node->word = word;
  93. node->left = node->right = nullptr;
  94. root = node;
  95. return;
  96. }
  97. insertWord(root, word);
  98. }
  99.  
  100. bool nodeExists(Node* node, std::string wordValue)
  101. {
  102. if (node == NULL)
  103. {
  104. return false;
  105. }
  106.  
  107. if (node->word == wordValue) {
  108. valueExists = true;
  109. }
  110.  
  111. if (node->left != NULL && !valueExists) {
  112. nodeExists(node->left, wordValue);
  113. }
  114.  
  115. if (node->right != NULL && !valueExists) {
  116. nodeExists(node->right, wordValue);
  117. }
  118.  
  119. return valueExists;
  120. }
  121.  
  122. bool BinarySearchTree::exists(std::string word) const
  123. {
  124. bool isWordExisting = nodeExists(root, word);
  125. valueExists = false;
  126. return isWordExisting; // change this to implement the method, returning true if word exists
  127. }
  128.  
  129. std::string printInorder(Node* node)
  130. {
  131. std::string value = "";
  132.  
  133. if (node == NULL)
  134. {
  135. return value;
  136. }
  137.  
  138. if (node->left != NULL) {
  139. printInorder(node->left);
  140. }
  141.  
  142. inorder_values += node->word + " ";
  143.  
  144. if (node->right != NULL) {
  145. printInorder(node->right);
  146. }
  147.  
  148. return inorder_values;
  149. }
  150.  
  151. std::string BinarySearchTree::inorder() const
  152. {
  153. // change this to return a string representation of the words
  154. // in the tree inorder.
  155. if (root == NULL)
  156. {
  157. return std::string("");
  158. }
  159.  
  160. std::string value = "";
  161.  
  162. // not sure if this 'if' statement is needed
  163. if (root->left == NULL && root->right == NULL)
  164. {
  165. return value;
  166. }
  167.  
  168. value += printInorder(root);
  169. inorder_values = "";
  170.  
  171. return value;
  172. }
  173.  
  174. std::string printPreorder(Node* node)
  175. {
  176. std::string value = "";
  177.  
  178. if (node == NULL)
  179. {
  180. return value;
  181. }
  182.  
  183. preorder_values += node->word + " ";
  184.  
  185. if (node->left != NULL) {
  186. printPreorder(node->left);
  187. }
  188.  
  189. if (node->right != NULL) {
  190. printPreorder(node->right);
  191. }
  192.  
  193. return preorder_values;
  194. }
  195.  
  196. std::string BinarySearchTree::preorder() const
  197. {
  198. // change this to return a string representation of the words
  199. // in the tree preorder.
  200.  
  201. std::string value = "";
  202.  
  203. if (root == NULL)
  204. {
  205. return value;
  206. }
  207.  
  208. if (root->left == NULL && root->right == NULL)
  209. {
  210. value += root->word;
  211. return value;
  212. }
  213.  
  214. value += printPreorder(root);
  215. preorder_values = "";
  216.  
  217. return value;
  218. }
  219.  
  220.  
  221. std::string printPostorder(Node* node)
  222. {
  223. std::string value = "";
  224.  
  225. if (node == NULL)
  226. {
  227. return value;
  228. }
  229.  
  230. if (node->left != NULL) {
  231. printPostorder(node->left);
  232. }
  233.  
  234. if (node->right != NULL) {
  235. printPostorder(node->right);
  236. }
  237.  
  238. postorder_values += node->word + " ";
  239.  
  240. return postorder_values;
  241. }
  242.  
  243.  
  244. std::string BinarySearchTree::postorder() const
  245. {
  246. // change this to return a string representation of the words
  247. // in the tree postorder.
  248.  
  249. std::string value = "";
  250.  
  251. if (root == NULL)
  252. {
  253. return value;
  254. }
  255.  
  256. if (root->left == NULL && root->right == NULL)
  257. {
  258. value += root->word;
  259. return value;
  260. }
  261.  
  262. value += printPostorder(root);
  263. postorder_values = "";
  264.  
  265. return value;
  266. }
  267.  
  268. // **Operator overloads**
  269.  
  270.  
  271. BinarySearchTree& BinarySearchTree::operator+(std::string word)
  272. {
  273. return *this; // returns a reference to the modified tree
  274. }
  275.  
  276. BinarySearchTree& BinarySearchTree::operator=(const BinarySearchTree &rhs)
  277. {
  278. return *this;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement