Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include <fstream>
- #include "TwentyQuestionsTree.h"
- TwentyQuestionsTree::TwentyQuestionsTree(FILE *fp)
- {
- if(root==NULL)
- {
- char buffer[255];
- const char *token;
- fgets(buffer, 255, fp);
- token = strtok(buffer, ",\n");
- strcpy(root->question, token);
- token = strtok (NULL, ",");
- strcpy(root->left->question, token);
- token = strtok (NULL, ",");
- strcpy(root->right->question, token);
- }
- else
- {
- if(insert(root->left, left->question, left, right)==false)
- {
- insert(root->right, right->question, left, right);
- }
- }
- // TODO Implement the constructor that builds the initial tree from the file
- }
- // Default constructor - creates a tree that asks no questions - it merely
- // guesses that it was a platypus.
- TwentyQuestionsTree::TwentyQuestionsTree()
- {
- root = new BinaryNode("Is it a platypus?");
- }
- /* insert
- * This inserts two questions into the tree - the children of the parent.
- * This is used when building the tree from scratch.
- * The assumption is that the parent is already in the tree, and we are
- * merely adding the left and right children.
- *
- * First, this *finds* the parent. Then it inserts the left and right
- * child into the tree.
- */
- bool TwentyQuestionsTree::insert(BinaryNode *r, const char *parent,
- const char *left, const char *right)
- {
- if(r==NULL)
- return false;
- if(r->question == left || r->question == right)
- {
- r->question = parent;
- r->left->question = left;
- r->right->question = right;
- return true;
- }
- else
- {
- if(insert(r->left, parent, left, right)==false)
- {
- insert(r->right, parent, left, right);
- }
- }
- // TODO - make sure you read the description, especially the assumption
- // about what is already in the tree!!!!!
- }
- /* modifyAndInsert
- *
- * This modifies the tree as a result of the game being incorrect. Either
- * the left child or right child is already in the tree. This method replaces
- * that existing one with the new parent and creates new left and right
- * children.
- *
- * First this *finds* either the left or right child. Then it replaces that
- * node with the parent and adds two new nodes - left and right.
- */
- bool TwentyQuestionsTree::modifyAndInsert(BinaryNode *n, const char *parent,
- const char *left, const char *right)
- {
- if(n==NULL)
- return false;
- if(n->question == left || n->question == right)
- {
- n->question = parent;
- n->left->question = left;
- n->right->question = right;
- return true;
- }
- else
- {
- if(modifyAndInsert(n->left, parent, left, right)==false)
- {
- modifyAndInsert(n->right, parent, left, right);
- }
- }
- }
- /* public version of modifyAndInsert */
- void TwentyQuestionsTree::modifyAndInsert(const char *parent, const char *left,
- const char *right)
- {
- if(root->question == left || root->question == right)
- {
- root->question = parent;
- root->left->question = left;
- root->right->question = right;
- return;
- }
- else
- {
- if(modifyAndInsert(root->left, parent, left, right)==false)
- {
- modifyAndInsert(root->right, parent, left, right);
- }
- }
- }
- /* reset
- *
- * This resets the iterator to the beginning of the game
- */
- void TwentyQuestionsTree::reset()
- {
- iterator = root;
- }
- /* currentQuestions
- *
- * This has been provided for you. This looks at the current position of
- * the iterator and returns the question stored in that BinaryNode.
- */
- const char *TwentyQuestionsTree::currentQuestion()
- {
- if (iterator == NULL)
- return NULL;
- else
- return iterator->question;
- }
- /* recordAnswer
- *
- * This advances the iterator. If the answer was yes (1), go right. If
- * no (0), go left.
- */
- void TwentyQuestionsTree::recordAnswer(int answer)
- {
- if(1)
- iterator = iterator->right;
- if(0)
- iterator = iterator->left;
- }
- /* storeTree
- *
- * This writes the tree out to the file in the same format as the
- * sample input file was.
- */
- void TwentyQuestionsTree::storeTree(BinaryNode *n,FILE *fp)
- {
- if(root==NULL)
- return;
- else
- {
- fprintf(fp, "%s,%s,%s\n", n->question, n->left, n->right);
- }
- if(root->right->right!=NULL)
- {
- storeTree(root->right, fp);
- }
- if(root->left->left!=NULL)
- {
- storeTree(root->left, fp);
- }
- return;
- }
- void TwentyQuestionsTree::storeTree(FILE *fp)
- {
- if(root==NULL)
- return;
- else
- {
- fprintf(fp,"%s,%s,%s\n", root->question, root->left, root->right);
- }
- if(root->right->right!=NULL)
- {
- storeTree(root->right, fp);
- }
- if(root->left->left!=NULL)
- {
- storeTree(root->left, fp);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement