Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <list>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. // File Descriptors
  10. ofstream outfile;
  11. ifstream infile;
  12.  
  13. // Class Definitions
  14. class TreeNode
  15. {
  16.     typedef string valueType;
  17.     private:
  18.         TreeNode* root;         //  The tree's root node's pointer
  19.         TreeNode* Lchild;       // Points to this node's left child
  20.         TreeNode* Rchild;       // Points to this node's right child
  21.         valueType word;         // Holds the word
  22.         int counter;            // Holds the number of occurences of the word
  23.     public:
  24.         // Constructer
  25.       TreeNode()
  26.       {
  27.         root = NULL;                // Root is not pointing to anything yet
  28.         counter = 0;                // No words counted yet
  29.       }
  30.       TreeNode* insert(TreeNode *root, valueType);  // Adds a word to the bst
  31.       TreeNode* search(TreeNode* r, valueType);     // Searches for instances of each word
  32.       TreeNode* get_root()          // Returns the root of the class
  33.       { return root;}
  34.       valueType get_word()          // Returns the word in current node
  35.       { return word;}
  36.       int get_counter()             // Returns the counter of current node
  37.       { return counter;}
  38.       void print(TreeNode*);        // Prints out each word along with it's count and frequency
  39.       TreeNode* myNewNode();        // Creates a new pointer to a new node in the bst
  40. };
  41.  
  42.  
  43. // Prototypes
  44. int OpenOutput();
  45. int OpenInput();
  46. void PrintHeader();
  47.  
  48. int main()
  49. {
  50.     OpenOutput();
  51.     OpenInput();
  52.     PrintHeader();
  53.  
  54.     string word;                    // Declare variable to hold each word from infile
  55.     //TreeNode* root = NULL;          // Declare a pointer of type TreeNode that points to our tree
  56.     TreeNode tree;                  // Declare an object of type TreeNode to start a tree
  57.     //tree.myNewNode();               // Create a node for root to point to
  58.     infile >> word;
  59.     while (infile)
  60.     {
  61.        tree.insert(tree.get_root(), word);  // Insert word into tree
  62.        infile >>  word;                     // Get next word
  63.  
  64.        // Display each word with total # of occurrences and % of input file
  65.        cout << "Word: " << setw(16) << tree.get_word() << setw(10) << "  Absolute: "
  66.             << tree.get_counter() << setw(11) << "Relative: " << endl;
  67.        outfile << "Word: " << setw(16) << tree.get_word() << setw(10) << "  Absolute: "
  68.                << tree.get_counter() << setw(11) << "Relative: "<< endl;
  69.  
  70.     }
  71.  
  72.     return 0;
  73. }
  74.  
  75. // Funtion Definitions
  76. // *********************************************************************
  77. TreeNode* TreeNode::myNewNode()
  78. {
  79.     TreeNode* temp = NULL;          // New pointer, points to nothing
  80.     temp = new TreeNode;            // Points to a newly created node
  81.     //.temp->word = NULL;              // Empty value in word section
  82.     temp->Lchild = NULL;            // There are no left children yet to point to
  83.     temp->Rchild = NULL;            // There are no right children yet to point to
  84.     temp->counter = 0;              // There are no words to count yet
  85.     return temp;
  86. }
  87.  
  88. // *********************************************************************
  89. //TreeNode* TreeNode::insert(TreeNode* r, valueType v)    // Adds a word to the bst
  90. TreeNode* TreeNode::insert(TreeNode *root, valueType v)    // Adds a word to the bst
  91.  
  92. {
  93.     TreeNode* parent;       // Points to parent node
  94.     TreeNode* temp;         // Temporary holding place
  95.  
  96.     if (root == NULL)       // If we don't have a tree yet
  97.     {
  98.         root = myNewNode(); // This starts us off
  99.         word = v;           // Put passed in word into tree node
  100.         counter = counter + 1;// Increment that word's counter
  101.         return root;        // Get out of function
  102.     }
  103.     while (root != NULL)    // While we do have a tree
  104.     {                       // Search for where to put new value
  105.         if(v < word)        // If new word is less than root's word
  106.         {
  107.             parent = root;  // Make the root a parent
  108.             root = Lchild;  // Move down left tree
  109.             counter = counter + 1;
  110.         }
  111.         else if (v > word)  // If new word is greater than root word
  112.         {
  113.             parent = root;  // Make the root a parent
  114.             root = Rchild;  // Move down right tree
  115.             counter = counter + 1;
  116.         }
  117.     }
  118.     temp = myNewNode();     // Create a new node
  119.     temp->word = v;         // Fill in the word field
  120.  
  121.     //  Update left and right parents
  122.     if (v < parent->word)
  123.         parent->Lchild = temp;
  124.     else if (v > parent->word)
  125.         parent->Rchild = temp;
  126.     return root;
  127. }
  128. // *********************************************************************
  129. TreeNode* TreeNode::search(TreeNode* r, valueType val)
  130. {
  131.     while (r != NULL && val != r->word)
  132.     {
  133.         if (val < r->word)
  134.         {
  135.             r = r->Lchild;
  136.         }
  137.         else
  138.         {
  139.             r = r->Rchild;
  140.         }
  141.     }
  142.     if (r->word == val)
  143.     {
  144.         // Update counter for that word
  145.         r->counter = counter + 1;
  146.     }
  147. }
  148.  
  149. // *********************************************************************
  150. void TreeNode::print(TreeNode*)
  151. {
  152.     if (root == NULL)
  153.         return;
  154.     print(Lchild);
  155.     cout << word;
  156.     print(Rchild);
  157. }
  158.  
  159. // *********************************************************************
  160. int OpenOutput()
  161. {
  162.     outfile.open("TreesOUT");
  163.     if (!outfile)
  164.     {
  165.         cout << "Error opening output file." << endl << endl;
  166.         outfile << "Error opening output file." << endl << endl;
  167.         return 1;
  168.     }
  169.     else
  170.     cout << "Output file opened correctly." << endl << endl;
  171.     outfile << "Output file opened correctly." << endl << endl;
  172. }
  173.  
  174. // *********************************************************************
  175. int OpenInput()
  176. {
  177.     infile.open("USconstitution.dat");
  178.     if (!infile)
  179.     {
  180.         cout << "Error opening input file." << endl << endl;
  181.         outfile << "Error opening input file."  << endl << endl;
  182.         return 1;
  183.     }
  184.     else
  185.     {
  186.         cout << "Input file opened correctly." << endl << endl;
  187.         outfile << "Input file opened correctly." << endl << endl;
  188.  
  189.     }
  190. }
  191.  
  192. // *********************************************************************
  193. void PrintHeader()
  194. {
  195.     cout << "Erin Corona" << endl;
  196.     cout << "CS 372.30137" << endl;
  197.     cout << "Trees " << endl;
  198.     cout << "Due Friday, July 13, 2012" << endl << endl;
  199.  
  200.     outfile << "Erin Corona" << endl;
  201.     outfile << "CS 372.30137" << endl;
  202.     outfile << "Trees " << endl;
  203.     outfile << "Due Friday, July 13, 2012" << endl << endl;
  204. }