vanllabean81

Vanllabean81 Tree Word Frequencies

Jul 10th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. // Erin Corona
  2. // CS 372.30137
  3. // Tree Word Frequencies Program
  4. // Due Friday, July 13, 2012
  5. //*********************************************************************
  6. // This program uses an ordered binary tree to count the instances of
  7. // different words of the US Constitution and output absolute and
  8. // relative frequencies.
  9. //*********************************************************************
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. // File Descriptors
  17. ofstream outfile;
  18. ifstream infile;
  19.  
  20. // Class Definition
  21. class tNode
  22. {
  23.     // Typedefs
  24.     typedef string tType;   // Declare typedef for all values in tree
  25.  
  26.     private:
  27.         tNode* LChild;      // Points to the left branch of tree
  28.         tType value;        // Value in node
  29.         tNode* RChild;      // Points to the right branch of tree
  30.     public:
  31.         void PrintTree(tNode* treeRoot)
  32.         // Prints out the contents of a tree, in order
  33.         // from smallest to largest
  34.         {
  35.             if (treeRoot== NULL)            // If there's no tree
  36.                 return;                 // There's nothing to print, so leave function
  37.             else
  38.             {
  39.                 PrintTree(treeRoot->LChild);
  40.                 cout << treeRoot->value;
  41.                 outfile << treeRoot->value;
  42.                 PrintTree(treeRoot->RChild);
  43.             }
  44.  
  45.         }
  46.         tNode* myNewTNode()
  47.         // Creates new tree node with a value,
  48.         // a left child, and a right child (pointers)
  49.         {
  50.             tNode* temp;
  51.             temp = new tNode;
  52.             temp->value = '0';
  53.             temp->LChild = NULL;
  54.             temp->RChild = NULL;
  55.             return temp;
  56.         }
  57.  
  58.         void insert(tNode* r, tType v)
  59.         // Inserts values into tree
  60.         {
  61.             tNode* parent;
  62.             tNode* temp;
  63.             if (r == NULL)                  // If there is no tree yet
  64.             {
  65.                 r = myNewTNode();           // Create one
  66.                 r->value = v;               // Set its value to v passed in
  67.                 parent = NULL;
  68.                 return;                     // Leave function
  69.             }
  70.  
  71.             while (r != NULL)               // While we do have a tree
  72.             {
  73.                 parent = r;
  74.                 if (v < r->value)       // Smaller values will go on the left
  75.                 {
  76.                     parent = r;         // Set parent to current root
  77.                     r = r->LChild;      // Set root to point to the left side of tree
  78.                 }
  79.                 else                    // Larger values go to the right
  80.                 {
  81.                     parent = r;         // Set parent to current root
  82.                     r = r->RChild;      // Set root to point to the right side of tree
  83.                 }
  84.                 temp = myNewTNode();    // Create a new node
  85.                 temp->value = v;        // Fill in the value
  86.                 if (v < parent->value)  // If passed in value is less than parent's value
  87.                     parent->LChild = temp;// Place new node as parent's left child
  88.                 else if ( v> parent->value)// If passed in value is larger than parent's value
  89.                     parent->RChild = temp; // Place new node as parent's right child
  90.                 return;
  91.             }
  92.         }
  93.  
  94.         tNode* Search(tNode* r, tType v)
  95.         // Searches through the tree for a specific value
  96.         // and returns pointer to value
  97.         {
  98.             tNode* parent;
  99.             while ((r != NULL) && (v != r->value))
  100.             {
  101.                 if (v < r->value)
  102.                 {
  103.                     parent = r;
  104.                     r = r->LChild;
  105.                 }
  106.                 else
  107.                 {
  108.                     parent = r;
  109.                     r = r->RChild;
  110.                 }
  111.             }
  112.             return r;
  113.         }
  114. };
  115.  
  116. // Prototypes
  117. int OpenOutput();
  118. int OpenInput();
  119. void PrintHeader();
  120.  
  121. int main()
  122. {
  123.     OpenOutput();       // Open file to hold answers
  124.     OpenInput();        // Open supplied file to read from
  125.     PrintHeader();      // Print header for homework
  126.  
  127.     string word;        // Declare a variable to hold each word
  128.     infile >> word;     // Get word
  129.  
  130.     // ????  Do I need a root AND a new object?  This part confuses me...
  131.     tNode* root;        // Points to the beginning of a tree
  132.     tNode tree1;        // Declare object of type tNode
  133.     root = tree1.myNewTNode();
  134.  
  135.     tree1.insert(root, word);// Insert word into tree
  136.     while (infile)
  137.     {
  138.         infile >> word;
  139.         tree1.insert(root, word);
  140.         tree1.PrintTree(root);
  141.  
  142.     }
  143.     //tree1.PrintTree(root);
  144.  
  145. return 0;
  146. }
  147.  
  148. int OpenOutput()
  149. {
  150.     outfile.open("TreesOUT");
  151.     if (!outfile)
  152.     {
  153.         cout << "Error opening output file." << endl << endl;
  154.         outfile << "Error opening output file." << endl << endl;
  155.         return 1;
  156.     }
  157.     else
  158.     cout << "Output file opened correctly." << endl << endl;
  159.     outfile << "Output file opened correctly." << endl << endl;
  160. }
  161.  
  162. int OpenInput()
  163. {
  164.     infile.open("USconstitution.dat");
  165.     if (!infile)
  166.     {
  167.         cout << "Error opening input file." << endl << endl;
  168.         outfile << "Error opening input file."  << endl << endl;
  169.         return 1;
  170.     }
  171.     else
  172.     {
  173.         cout << "Input file opened correctly." << endl << endl;
  174.         outfile << "Input file opened correctly." << endl << endl;
  175.  
  176.     }
  177. }
  178.  
  179. void PrintHeader()
  180. {
  181.     cout << "Erin Corona" << endl;
  182.     cout << "CS 372.30137" << endl;
  183.     cout << "Trees " << endl;
  184.     cout << "Due Friday, July 13, 2012" << endl << endl;
  185.  
  186.     outfile << "Erin Corona" << endl;
  187.     outfile << "CS 372.30137" << endl;
  188.     outfile << "Trees " << endl;
  189.     outfile << "Due Friday, July 13, 2012" << endl << endl;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment