Don't like ads? PRO users don't see any ads ;-)
Guest

CS2370 Prog 8

By: mattfong on Jun 6th, 2011  |  syntax: None  |  size: 10.55 KB  |  hits: 43  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Matthew Fong
  2. //Program 8: maze
  3. //description: create a maze and see if you can get to the end
  4.  
  5. #ifndef CHARBINARYTREE_H_INCLUDED
  6. #define CHARBINARYTREE_H_INCLUDED
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. class charBinaryTree
  12. {
  13. private:
  14.    struct TreeNode
  15.    {
  16.       char value;         // The value in the node
  17.       TreeNode *left;    // Pointer to left child node
  18.       TreeNode *right;   // Pointer to right child node
  19.    };
  20.  
  21.    TreeNode *root;       // Pointer to the root node
  22.  
  23.    // Private member functions
  24.    void insert(TreeNode *&, TreeNode *&);
  25.    void destroySubTree(TreeNode *);
  26.    void deleteNode(char, TreeNode *&);
  27.    void makeDeletion(TreeNode *&);
  28.    void displayInOrder(TreeNode *) const;
  29.    void displayPreOrder(TreeNode *) const;
  30.    void displayPostOrder(TreeNode *) const;
  31.  
  32. public:
  33.    // Constructor
  34.    charBinaryTree()
  35.       { root = NULL; }
  36.  
  37.    // Destructor
  38.    ~charBinaryTree()
  39.       { destroySubTree(root); }
  40.  
  41.    // Binary tree operations
  42.    void insertNode(char);
  43.    bool searchNode(char);
  44.    void remove(char);
  45.  
  46.    void displayInOrder() const
  47.       {  displayInOrder(root); }
  48.  
  49.    void displayPreOrder() const
  50.       {  displayPreOrder(root); }
  51.  
  52.    void displayPostOrder() const
  53.       {  displayPostOrder(root); }
  54. };
  55.  
  56. #endif // CHARBINARYTREE_H_INCLUDED
  57.  
  58. #include <iostream>
  59. #include "charBinaryTree.h"
  60. using namespace std;
  61.  
  62. //*************************************************************
  63. // insert accepts a TreeNode pointer and a pointer to a node. *
  64. // The function inserts the node into the tree pointed to by  *
  65. // the TreeNode pointer. This function is called recursively. *
  66. //*************************************************************
  67.  
  68. void charBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
  69. {
  70.    if (nodePtr == NULL)
  71.       nodePtr = newNode;                  // Insert the node.
  72.    else if (newNode->value < nodePtr->value)
  73.       insert(nodePtr->left, newNode);     // Search the left branch
  74.    else
  75.       insert(nodePtr->right, newNode);    // Search the right branch
  76. }
  77.  
  78. //**********************************************************
  79. // insertNode creates a new node to hold num as its value, *
  80. // and passes it to the insert function.                   *
  81. //**********************************************************
  82.  
  83. void charBinaryTree::insertNode(char c)
  84. {
  85.    TreeNode *newNode;      // Pointer to a new node.
  86.  
  87.    // Create a new node and store num in it.
  88.    newNode = new TreeNode;
  89.    newNode->value = c;
  90.    newNode->left = newNode->right = NULL;
  91.  
  92.    // Insert the node.
  93.    insert(root, newNode);
  94. }
  95.  
  96. //***************************************************
  97. // destroySubTree is called by the destructor. It   *
  98. // deletes all nodes in the tree.                   *
  99. //***************************************************
  100.  
  101. void charBinaryTree::destroySubTree(TreeNode *nodePtr)
  102. {
  103.    if (nodePtr)
  104.    {
  105.       if (nodePtr->left)
  106.          destroySubTree(nodePtr->left);
  107.       if (nodePtr->right)
  108.          destroySubTree(nodePtr->right);
  109.       delete nodePtr;
  110.    }
  111. }
  112.  
  113. //***************************************************
  114. // searchNode determines if a value is present in   *
  115. // the tree. If so, the function returns true.      *
  116. // Otherwise, it returns false.                     *
  117. //***************************************************
  118.  
  119. bool charBinaryTree::searchNode(char c)
  120. {
  121.    TreeNode *nodePtr = root;
  122.  
  123.    while (nodePtr)
  124.    {
  125.       if (nodePtr->value == c)
  126.          return true;
  127.       else if (c < nodePtr->value)
  128.          nodePtr = nodePtr->left;
  129.       else
  130.          nodePtr = nodePtr->right;
  131.    }
  132.    return false;
  133. }
  134.  
  135. //**********************************************
  136. // remove calls deleteNode to delete the       *
  137. // node whose value member is the same as num. *
  138. //**********************************************
  139.  
  140. void charBinaryTree::remove(char c)
  141. {
  142.    deleteNode(c, root);
  143. }
  144.  
  145.  
  146. //********************************************
  147. // deleteNode deletes the node whose value   *
  148. // member is the same as num.                *
  149. //********************************************
  150.  
  151. void charBinaryTree::deleteNode(char c, TreeNode *&nodePtr)
  152. {
  153.    if (c < nodePtr->value)
  154.       deleteNode(c, nodePtr->left);
  155.    else if (c > nodePtr->value)
  156.       deleteNode(c, nodePtr->right);
  157.    else
  158.       makeDeletion(nodePtr);
  159. }
  160.  
  161.  
  162. //***********************************************************
  163. // makeDeletion takes a reference to a pointer to the node  *
  164. // that is to be deleted. The node is removed and the       *
  165. // branches of the tree below the node are reattached.      *
  166. //***********************************************************
  167.  
  168. void charBinaryTree::makeDeletion(TreeNode *&nodePtr)
  169. {
  170.    // Define a temporary pointer to use in reattaching
  171.    // the left subtree.
  172.    TreeNode *tempNodePtr;
  173.  
  174.    if (nodePtr == NULL)
  175.       cout << "Cannot delete empty node.\n";
  176.    else if (nodePtr->right == NULL)
  177.    {
  178.       tempNodePtr = nodePtr;
  179.       nodePtr = nodePtr->left;   // Reattach the left child
  180.       delete tempNodePtr;
  181.    }
  182.    else if (nodePtr->left == NULL)
  183.    {
  184.       tempNodePtr = nodePtr;
  185.       nodePtr = nodePtr->right;  // Reattach the right child
  186.       delete tempNodePtr;
  187.    }
  188.    // If the node has two children.
  189.    else
  190.    {
  191.       // Move one node the right.
  192.       tempNodePtr = nodePtr->right;
  193.       // Go to the end left node.
  194.       while (tempNodePtr->left)
  195.          tempNodePtr = tempNodePtr->left;
  196.       // Reattach the left subtree.
  197.       tempNodePtr->left = nodePtr->left;
  198.       tempNodePtr = nodePtr;
  199.       // Reattach the right subtree.
  200.       nodePtr = nodePtr->right;
  201.       delete tempNodePtr;
  202.    }
  203. }
  204.  
  205. //****************************************************************
  206. // The displayInOrder member function displays the values        *
  207. // in the subtree pointed to by nodePtr, via inorder traversal.  *
  208. //****************************************************************
  209.  
  210. void charBinaryTree::displayInOrder(TreeNode *nodePtr) const
  211. {
  212.    if (nodePtr)
  213.    {
  214.       displayInOrder(nodePtr->left);
  215.       cout << nodePtr->value << endl;
  216.       displayInOrder(nodePtr->right);
  217.    }
  218. }
  219.  
  220. //****************************************************************
  221. // The displayPreOrder member function displays the values       *
  222. // in the subtree pointed to by nodePtr, via preorder traversal. *
  223. //****************************************************************
  224.  
  225. void charBinaryTree::displayPreOrder(TreeNode *nodePtr) const
  226. {
  227.    if (nodePtr)
  228.    {
  229.       cout << nodePtr->value << endl;
  230.       displayPreOrder(nodePtr->left);
  231.       displayPreOrder(nodePtr->right);
  232.    }
  233. }
  234.  
  235. //****************************************************************
  236. // The displayPostOrder member function displays the values      *
  237. // in the subtree pointed to by nodePtr, via postorder traversal.*
  238. //****************************************************************
  239.  
  240. void charBinaryTree::displayPostOrder(TreeNode *nodePtr) const
  241. {
  242.    if (nodePtr)
  243.    {
  244.       displayPostOrder(nodePtr->left);
  245.       displayPostOrder(nodePtr->right);
  246.       cout << nodePtr->value << endl;
  247.    }
  248. }
  249.  
  250. #include "charBinaryTree.h"
  251. #include <iostream>
  252. using namespace std;
  253.  
  254. int main()
  255. {
  256.     char path; //User input for the maze
  257.     int count = 1; //attempts at solving the maze
  258.     charBinaryTree tree;
  259.     bool win = false;
  260.  
  261.     //sets the maze up
  262.     tree.insertNode('O');
  263.     tree.insertNode('K');
  264.     tree.insertNode('M');
  265.     tree.insertNode('B');
  266.     tree.insertNode('W');
  267.     tree.insertNode('X');
  268.     tree.insertNode('Z');
  269.     tree.insertNode('Q');
  270.     tree.insertNode('U');
  271.     tree.insertNode('V');
  272.     tree.insertNode('S');
  273.     tree.insertNode('R');
  274.     tree.insertNode('T');
  275.  
  276.     while(!win)
  277.     {
  278.         cout << "You are at O." << endl;
  279.         cout << "Which path would you like to take? Left or Right? L/R" << endl;
  280.         cin >> path;
  281.  
  282.         if(path == 'R' || path == 'r') //Go to W
  283.         {
  284.             cout << "You are at W." << endl;
  285.             cout << "Which path would you like to take? Left or Right? L/R" << endl;
  286.             cin >> path;
  287.  
  288.             if(path == 'R' || path == 'r') //Go to X
  289.             {
  290.                 cout << "You are at X." << endl;
  291.                 cout << "X leads to Z." << endl;
  292.                 cout << "Dead end." << endl;
  293.                 cout << endl;
  294.                 count++;
  295.             }
  296.  
  297.             if(path == 'L' || path == 'l') //Go to Q
  298.             {
  299.                 cout << "You are at Q." << endl;
  300.                 cout << "Which path would you like to take? Left or Right? L/R" << endl;
  301.                 cin >> path;
  302.  
  303.                 if(path == 'L' || path == 'l') //Go to S
  304.                 {
  305.                     cout << "You are at S." << endl;
  306.                     cout << "Which path would you like to take? Left or Right? L/R" << endl;
  307.                     cin >> path;
  308.  
  309.                     if(path == 'L' || path == 'l') //Go to R
  310.                     {
  311.                         cout << "You are at R." << endl;
  312.                         cout << "You reached the end of the maze." << endl;
  313.                         win = true;
  314.                         break;
  315.                     }
  316.  
  317.                     if(path == 'R' || path == 'r') //Go to T
  318.                     {
  319.                         cout << "You are at T." << endl;
  320.                         cout << "Dead end." << endl;
  321.                         cout << endl;
  322.                         count++;
  323.                     }
  324.                 }
  325.  
  326.                 if(path == 'R' || path == 'r') //Go to U
  327.                 {
  328.                     count++;
  329.                     cout << "You are at U." << endl;
  330.                     cout << "U leads to V." << endl;
  331.                     cout << "Dead end." << endl;
  332.                     cout << endl;
  333.                 }
  334.             }
  335.         }
  336.  
  337.         if(path == 'L' || path == 'l') //Go to K
  338.         {
  339.             count++; //Seeing as they already went the wrong way...
  340.             cout << "You are at K." << endl;
  341.             cout << "Which path would you like to take? Left or Right? L/R" << endl;
  342.             cin >> path;
  343.  
  344.             if(path == 'L' || path == 'l')
  345.             {
  346.                 cout << "You are at B." << endl;
  347.                 cout << "Dead end." << endl;
  348.                 cout << endl;
  349.             }
  350.  
  351.             if(path == 'R' || path == 'r')
  352.             {
  353.                 cout << "You are at M." << endl;
  354.                 cout << "Dead end." << endl;
  355.                 cout << endl;
  356.             }
  357.         }
  358.     }
  359.  
  360.     cout << "Times taken to solve the maze: " << count << endl;
  361.     return 0;
  362. }