Advertisement
pichumy

Untitled

Mar 13th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <fstream>
  6. #include "TwentyQuestionsTree.h"
  7.  
  8. TwentyQuestionsTree::TwentyQuestionsTree(FILE *fp)
  9. {
  10. if(root==NULL)
  11. {
  12. char buffer[255];
  13. const char *token;
  14.  
  15. fgets(buffer, 255, fp);
  16. token = strtok(buffer, ",\n");
  17. strcpy(root->question, token);
  18. token = strtok (NULL, ",");
  19. strcpy(root->left->question, token);
  20. token = strtok (NULL, ",");
  21. strcpy(root->right->question, token);
  22. }
  23. else
  24. {
  25. if(insert(root->left, left->question, left, right)==false)
  26. {
  27. insert(root->right, right->question, left, right);
  28. }
  29. }
  30. // TODO Implement the constructor that builds the initial tree from the file
  31. }
  32.  
  33. // Default constructor - creates a tree that asks no questions - it merely
  34. // guesses that it was a platypus.
  35. TwentyQuestionsTree::TwentyQuestionsTree()
  36. {
  37. root = new BinaryNode("Is it a platypus?");
  38. }
  39.  
  40. /* insert
  41. * This inserts two questions into the tree - the children of the parent.
  42. * This is used when building the tree from scratch.
  43. * The assumption is that the parent is already in the tree, and we are
  44. * merely adding the left and right children.
  45. *
  46. * First, this *finds* the parent. Then it inserts the left and right
  47. * child into the tree.
  48. */
  49. bool TwentyQuestionsTree::insert(BinaryNode *r, const char *parent,
  50. const char *left, const char *right)
  51. {
  52. if(r==NULL)
  53. return false;
  54. if(r->question == left || r->question == right)
  55. {
  56. r->question = parent;
  57. r->left->question = left;
  58. r->right->question = right;
  59. return true;
  60. }
  61. else
  62. {
  63. if(insert(r->left, parent, left, right)==false)
  64. {
  65. insert(r->right, parent, left, right);
  66. }
  67. }
  68. // TODO - make sure you read the description, especially the assumption
  69. // about what is already in the tree!!!!!
  70. }
  71.  
  72. /* modifyAndInsert
  73. *
  74. * This modifies the tree as a result of the game being incorrect. Either
  75. * the left child or right child is already in the tree. This method replaces
  76. * that existing one with the new parent and creates new left and right
  77. * children.
  78. *
  79. * First this *finds* either the left or right child. Then it replaces that
  80. * node with the parent and adds two new nodes - left and right.
  81. */
  82. bool TwentyQuestionsTree::modifyAndInsert(BinaryNode *n, const char *parent,
  83. const char *left, const char *right)
  84. {
  85. if(n==NULL)
  86. return false;
  87. if(n->question == left || n->question == right)
  88. {
  89. n->question = parent;
  90. n->left->question = left;
  91. n->right->question = right;
  92. return true;
  93. }
  94. else
  95. {
  96. if(modifyAndInsert(n->left, parent, left, right)==false)
  97. {
  98. modifyAndInsert(n->right, parent, left, right);
  99. }
  100. }
  101.  
  102. }
  103.  
  104. /* public version of modifyAndInsert */
  105. void TwentyQuestionsTree::modifyAndInsert(const char *parent, const char *left,
  106. const char *right)
  107. {
  108. if(root->question == left || root->question == right)
  109. {
  110. root->question = parent;
  111. root->left->question = left;
  112. root->right->question = right;
  113. return;
  114. }
  115. else
  116. {
  117. if(modifyAndInsert(root->left, parent, left, right)==false)
  118. {
  119. modifyAndInsert(root->right, parent, left, right);
  120. }
  121. }
  122. }
  123.  
  124. /* reset
  125. *
  126. * This resets the iterator to the beginning of the game
  127. */
  128. void TwentyQuestionsTree::reset()
  129. {
  130. iterator = root;
  131. }
  132.  
  133. /* currentQuestions
  134. *
  135. * This has been provided for you. This looks at the current position of
  136. * the iterator and returns the question stored in that BinaryNode.
  137. */
  138. const char *TwentyQuestionsTree::currentQuestion()
  139. {
  140. if (iterator == NULL)
  141. return NULL;
  142. else
  143. return iterator->question;
  144. }
  145.  
  146. /* recordAnswer
  147. *
  148. * This advances the iterator. If the answer was yes (1), go right. If
  149. * no (0), go left.
  150. */
  151. void TwentyQuestionsTree::recordAnswer(int answer)
  152. {
  153. if(1)
  154. iterator = iterator->right;
  155. if(0)
  156. iterator = iterator->left;
  157. }
  158.  
  159. /* storeTree
  160. *
  161. * This writes the tree out to the file in the same format as the
  162. * sample input file was.
  163. */
  164. void TwentyQuestionsTree::storeTree(BinaryNode *n,FILE *fp)
  165. {
  166. if(root==NULL)
  167. return;
  168. else
  169. {
  170. fprintf(fp, "%s,%s,%s\n", n->question, n->left, n->right);
  171. }
  172. if(root->right->right!=NULL)
  173. {
  174. storeTree(root->right, fp);
  175. }
  176. if(root->left->left!=NULL)
  177. {
  178. storeTree(root->left, fp);
  179. }
  180. return;
  181. }
  182. void TwentyQuestionsTree::storeTree(FILE *fp)
  183. {
  184. if(root==NULL)
  185. return;
  186. else
  187. {
  188. fprintf(fp,"%s,%s,%s\n", root->question, root->left, root->right);
  189. }
  190. if(root->right->right!=NULL)
  191. {
  192. storeTree(root->right, fp);
  193. }
  194. if(root->left->left!=NULL)
  195. {
  196. storeTree(root->left, fp);
  197. }
  198. return;
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement