Guest User

Untitled

a guest
Nov 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include<queue>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. class wordbstnode
  9. {
  10. public:
  11. string word;
  12. string explaination;
  13. wordbstnode * lcp; // leftchild;
  14. wordbstnode * rcp; // rightchild;
  15.  
  16. wordbstnode(string pw, string pe)
  17. {
  18. word = pw;
  19. explaination = pe;
  20. lcp = rcp = nullptr;
  21. }
  22.  
  23. wordbstnode()
  24. {
  25. lcp = rcp = nullptr;
  26. }
  27. ~wordbstnode()
  28. {
  29. delete lcp;
  30. delete rcp;
  31. }
  32. };
  33.  
  34. class dictionarybst
  35. {
  36.  
  37. public:
  38. wordbstnode * root;
  39.  
  40. dictionarybst()
  41. {
  42. root = nullptr;
  43.  
  44. }
  45.  
  46. void dftinorder(wordbstnode *r)
  47. {
  48. if (r == nullptr) return;
  49. dftinorder(r->lcp);
  50. cout << r->word << ": " << r->explaination << endl;
  51. dftinorder(r->rcp);
  52. }
  53. void breadthFirst(wordbstnode * r)
  54. {
  55. queue<wordbstnode*> q;
  56.  
  57. if (root) {
  58. q.push(root);
  59. }
  60. while (!q.empty())
  61. {
  62. const wordbstnode * const newNode = q.front();
  63. q.pop();
  64. cout << newNode->word << ": " << newNode->explaination << endl;
  65.  
  66. if (newNode->lcp) {
  67. q.push(newNode->lcp);
  68. }
  69. if (newNode->rcp) {
  70. q.push(newNode->rcp);
  71. }
  72. }
  73. }
  74. void preorder(wordbstnode* r)
  75. {
  76. if (r != NULL)
  77. {
  78. cout << r->word << ": " << r->explaination << endl;
  79. preorder(r->lcp);
  80. preorder(r->rcp);
  81. }
  82. else return;
  83. }
  84. void postorder(wordbstnode* r)
  85. {
  86. if (r != NULL)
  87. {
  88. postorder(r->lcp);
  89. postorder(r->rcp);
  90. cout << r->word << ": " << r->explaination << endl;
  91. }
  92. else return;
  93. }
  94. ~dictionarybst()
  95. {
  96. delete root;
  97. }
  98.  
  99. wordbstnode * search(string word)
  100. {
  101. transform(word.begin(), word.end(), word.begin(), toupper);
  102. wordbstnode * agent;
  103. agent = root;
  104.  
  105. while (agent != nullptr)
  106. {
  107. if (word == agent->word)
  108. {
  109. return agent;
  110. }
  111. else
  112. if (word < agent->word)
  113. {
  114. agent = agent->lcp;
  115. }
  116.  
  117. else
  118. {
  119. agent = agent->rcp;
  120. }
  121. }
  122.  
  123. return nullptr;
  124. }
  125.  
  126. void insert(string pw, string pe)
  127. {
  128. transform(pw.begin(), pw.end(), pw.begin(), toupper);
  129. wordbstnode *newword = new wordbstnode(pw, pe);
  130. wordbstnode * agent;
  131. wordbstnode * parent = nullptr;
  132. char whichbranch = '?'; //true for left, false for right
  133.  
  134. if (root == nullptr)
  135. {
  136. root = newword; // special case.
  137. return;
  138. }
  139.  
  140. agent = root;
  141. while (agent != nullptr)
  142. {
  143. if (pw == agent->word)
  144. return;
  145. else
  146. {
  147. parent = agent;
  148.  
  149. if (pw < agent->word)
  150. {
  151. whichbranch = 'l';
  152. agent = agent->lcp;
  153. }
  154. else
  155. {
  156. whichbranch = 'r';
  157. agent = agent->rcp;
  158. }
  159. }
  160. }
  161.  
  162. if (whichbranch == 'l')
  163. parent->lcp = newword;
  164. else
  165. parent->rcp = newword;
  166.  
  167. }
  168.  
  169. };
  170.  
  171.  
  172. int main()
  173. {
  174.  
  175.  
  176. dictionarybst bst;
  177. string word;
  178. string explanation;
  179.  
  180. char answer = 'y';
  181. int choice;
  182.  
  183. do
  184. {
  185. cout << "Please choose an operation from the following menu." << endl;
  186. cout << "1. add a new word\n"
  187. << "2. seach a word\n"
  188. << "3. breadth first traversal\n"
  189. << "4. depth first traversal (pre order)\n"
  190. << "5. depth first traversal (in order)\n"
  191. << "6. depth first traversal (post order)\n"
  192. << "7. Exit" << endl;
  193. cin >> choice;
  194. if (choice == 1)
  195. {
  196. cout << "Enter a word: ";
  197. cin.ignore();
  198. getline(cin, word);
  199. cout << "Enter a explanation: ";
  200. cin.ignore();
  201. getline(cin, explanation);
  202. wordbstnode * wbst = new wordbstnode(word, explanation);
  203. bst.insert(word, explanation);
  204. }
  205. if (choice == 2)
  206. {
  207. cout << "What word are you searching for? :" << endl;
  208. cin >> word;
  209. wordbstnode * searchResults;
  210. searchResults = bst.search(word);
  211. if (searchResults == NULL)
  212. {
  213. cout << "Word isnt in the tree!" << endl;
  214. }
  215. else
  216. cout << searchResults->word << ": " << searchResults->explaination << endl;
  217. }
  218. if (choice == 3)
  219. {
  220. bst.breadthFirst(bst.root);
  221. }
  222. if (choice == 4)
  223. {
  224. bst.preorder(bst.root);
  225. }
  226. if (choice == 5)
  227. {
  228. bst.dftinorder(bst.root);
  229. }
  230. if (choice == 6)
  231. {
  232. bst.postorder(bst.root);
  233. }
  234. if (choice == 7)
  235. {
  236. cout << "Program terminated.";
  237. break;
  238. }
  239.  
  240. cout << "Would you like to do another option? (y for yes, n for no.): " << endl;
  241. cin >> answer;
  242. } while (answer == 'y');
  243.  
  244. return 0;
  245. }
Add Comment
Please, Sign In to add comment