Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Erin Corona
- // CS 372.30137
- // Tree Word Frequencies Program
- // Due Friday, July 13, 2012
- //*********************************************************************
- // This program uses an ordered binary tree to count the instances of
- // different words of the US Constitution and output absolute and
- // relative frequencies.
- //*********************************************************************
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- // File Descriptors
- ofstream outfile;
- ifstream infile;
- // Class Definition
- class tNode
- {
- // Typedefs
- typedef string tType; // Declare typedef for all values in tree
- private:
- tNode* LChild; // Points to the left branch of tree
- tType value; // Value in node
- tNode* RChild; // Points to the right branch of tree
- public:
- void PrintTree(tNode* treeRoot)
- // Prints out the contents of a tree, in order
- // from smallest to largest
- {
- if (treeRoot== NULL) // If there's no tree
- return; // There's nothing to print, so leave function
- else
- {
- PrintTree(treeRoot->LChild);
- cout << treeRoot->value;
- outfile << treeRoot->value;
- PrintTree(treeRoot->RChild);
- }
- }
- tNode* myNewTNode()
- // Creates new tree node with a value,
- // a left child, and a right child (pointers)
- {
- tNode* temp;
- temp = new tNode;
- temp->value = '0';
- temp->LChild = NULL;
- temp->RChild = NULL;
- return temp;
- }
- void insert(tNode* r, tType v)
- // Inserts values into tree
- {
- tNode* parent;
- tNode* temp;
- if (r == NULL) // If there is no tree yet
- {
- r = myNewTNode(); // Create one
- r->value = v; // Set its value to v passed in
- parent = NULL;
- return; // Leave function
- }
- while (r != NULL) // While we do have a tree
- {
- parent = r;
- if (v < r->value) // Smaller values will go on the left
- {
- parent = r; // Set parent to current root
- r = r->LChild; // Set root to point to the left side of tree
- }
- else // Larger values go to the right
- {
- parent = r; // Set parent to current root
- r = r->RChild; // Set root to point to the right side of tree
- }
- temp = myNewTNode(); // Create a new node
- temp->value = v; // Fill in the value
- if (v < parent->value) // If passed in value is less than parent's value
- parent->LChild = temp;// Place new node as parent's left child
- else if ( v> parent->value)// If passed in value is larger than parent's value
- parent->RChild = temp; // Place new node as parent's right child
- return;
- }
- }
- tNode* Search(tNode* r, tType v)
- // Searches through the tree for a specific value
- // and returns pointer to value
- {
- tNode* parent;
- while ((r != NULL) && (v != r->value))
- {
- if (v < r->value)
- {
- parent = r;
- r = r->LChild;
- }
- else
- {
- parent = r;
- r = r->RChild;
- }
- }
- return r;
- }
- };
- // Prototypes
- int OpenOutput();
- int OpenInput();
- void PrintHeader();
- int main()
- {
- OpenOutput(); // Open file to hold answers
- OpenInput(); // Open supplied file to read from
- PrintHeader(); // Print header for homework
- string word; // Declare a variable to hold each word
- infile >> word; // Get word
- // ???? Do I need a root AND a new object? This part confuses me...
- tNode* root; // Points to the beginning of a tree
- tNode tree1; // Declare object of type tNode
- root = tree1.myNewTNode();
- tree1.insert(root, word);// Insert word into tree
- while (infile)
- {
- infile >> word;
- tree1.insert(root, word);
- tree1.PrintTree(root);
- }
- //tree1.PrintTree(root);
- return 0;
- }
- int OpenOutput()
- {
- outfile.open("TreesOUT");
- if (!outfile)
- {
- cout << "Error opening output file." << endl << endl;
- outfile << "Error opening output file." << endl << endl;
- return 1;
- }
- else
- cout << "Output file opened correctly." << endl << endl;
- outfile << "Output file opened correctly." << endl << endl;
- }
- int OpenInput()
- {
- infile.open("USconstitution.dat");
- if (!infile)
- {
- cout << "Error opening input file." << endl << endl;
- outfile << "Error opening input file." << endl << endl;
- return 1;
- }
- else
- {
- cout << "Input file opened correctly." << endl << endl;
- outfile << "Input file opened correctly." << endl << endl;
- }
- }
- void PrintHeader()
- {
- cout << "Erin Corona" << endl;
- cout << "CS 372.30137" << endl;
- cout << "Trees " << endl;
- cout << "Due Friday, July 13, 2012" << endl << endl;
- outfile << "Erin Corona" << endl;
- outfile << "CS 372.30137" << endl;
- outfile << "Trees " << endl;
- outfile << "Due Friday, July 13, 2012" << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment