I have this program to read in a text file and store each word in a Binary Search Tree and keep track of the frequency of this number in the file. My outputFile should read like:

32 a
54 and
4 hello
8 will
9 would

I have the program running correctly and storing every word and frequency in a binary search tree. I have a print inorder function to print the tree in the format required... My only problem is I dont know how to write this function output to the outputFile... i know i cant call the function inside the ouputFile.is_open if, so do I have to store my results in a char * or string, any help would be appreciated?

Thanks in advance, here is my code:

class BinarySearchTree
{
    private:
        struct tree_node
        {
           tree_node* left;
           tree_node* right;
           string data;
           int count;
        };
        tree_node* root;
    public:
        BinarySearchTree()
        {
           root = NULL;
        }
        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(tree_node*);
        void insert(string &d);
        //string results;
        //string results2;
        
};

void BinarySearchTree::insert(string &d)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->data = d;
    t->left = NULL;
    t->right = NULL;
    t->count = 1;
    parent = NULL;

  if(isEmpty()) root = t;
  else
  {
    
    
    tree_node* curr;
    curr = root;
    
    while(curr)
    {
        parent = curr;
        if(t->data == curr->data)
        {
                  curr->count++;
                  break;
        }
        else if(t->data > curr->data) curr = curr->right;
        else curr = curr->left;
    }
    
    if(t->data == parent->data)
    {
               parent = t;
    }
    if(t->data < parent->data)
       parent->left = t;
    else
       parent->right = t;
  }
}



void BinarySearchTree::print_inorder()
{
  inorder(root);
  return results,results2;
}

void BinarySearchTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout << " " << p->count << "   " <<  p->data << endl;
        
        if(p->right) inorder(p->right);
    }
    else return;
    
}



int main(int argc, char *argv[])
{
    BinarySearchTree b;
    int ch,tmp,tmp1;
    string test;
    string word;
   
    ifstream inputFile ( argv[1] );
    
    if (inputFile.is_open())
    {
     while (! inputFile.eof() )
     {
           inputFile >> word;
           b.insert(word);         
    }
     inputFile.close();
  }

else cout << "Unable to open file";

b.print_inorder();
    
    //cout << results;
    ofstream outputFile ( argv[2] );

if (outputFile.is_open())
    {
    
     //outputFile << results;
    
    //outputFile << b.print_inorder();
     outputFile.close();
  }

  else cout << "Unable to open file";


}
Ancient Dragon commented: Thanks for using code tags correctly :) +36

Recommended Answers

All 4 Replies

You should be able to print the output to your file, from your inorder() function, if that is the order you want it in.

You can open the output file in your BinarySearchTree::print_inorder() and then pass the root node and the file pointer to your inorder() function.

And fyi this return results,results2; is not valid.

1) Can I assume you have not tried to compile the code you posted? Or that you just ignored all compiler errors ? (Hint: Line 73 is wrong)

If you can print the information correctly using cout then you can print the same thing using any ofstream object -- cout is just another ofstream object. I suggest adding an ofstream parameter to print_inorder() then pass it along to the inorder() function so that the correct ofstream can be used.

void BinarySearchTree::print_inorder(ofstream& out)
{
  inorder(root, out);
  return results,results2;  // <<<<<<<<<< error here!
}

void BinarySearchTree::inorder(tree_node* p, ofstream& out)
{
   if(p != NULL)
    {
        if(p->left) inorder(p->left, out);
        out << " " << p->count << "   " <<  p->data << endl;
        
        if(p->right) inorder(p->right, out);
    }
    else return;

}

Thanks a lot for the help... Sorry I didnt mean to have the string eror in there, i was testing ways to get it to work and just left it in there. I got it working so thanks a lot.

Also, is there any way to ignore uppercase when using the string, or would i have to take it in as characters and use tolowercase? Also, how could i strtok the word string so as to not allow for punctuation?

comparsions are case sensitive, so you will need to use either toupper or tolower to make them the same.

I am not sure what you mean by not allowing the punctuation. But what the function strtok does is basically break your string up into tokens based on delimiters you provide. So if you had a string "I want, a blue ball" and your delimiter was , you would get the tokens "I want" and "a blue ball" back from the call.

Here is a reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.