URGENT HELP needed! BST Output the popular words

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

URGENT HELP needed! BST Output the popular words

Postby Cresenia » Fri May 25, 2012 10:01 pm

Sorry, this might be abit of a rush and sudden. I have to finish this program in 3 hours time. I would appreciate if there's any kind soul willing to help me with the code. Thank you.

Output of the program should be like this.

Top popular 12 words

1 of 6

2 the 5

3 all 3

4 people 3

5 some 3

6 times 3

7 and 2

8 can 1

9 fool 1

10 not 1

11 them 1

12 you 1

Below is the text file I'm suppose to read.

===infile.txt===
you can fool some of the people
some of the times, and all of the people some of
the times and not all of the people all of them times.

===Word.h===
Code: Select all
    #include <iostream>
    #include <fstream>
    using namespace std;
    class Word
    {
    public:
    Word();
    Word (char *);
    int getSumASCII () const;
    void increment ();
    int getCount () const;
    char* getWord () const;
    int getKey () const;
    void setCount (int);
    void setWord (char*);
    void setKey (int);
    private:
    char *w;
    int count;
    int sumASCII () const;
    int n;
    };


===Word.cpp===
Code: Select all
#include "Word.h"
Word::Word()
{
count = 0;
}
Word::Word (char *w)
{
this -> w = new char [strlen(w) + 1];
strcpy(this -> w, w);
count = 1;
}
int Word::getSumASCII () const
{
int sum = 0;
char * p = &w[0];
while(*p != '\0')
{
sum += *p;
p++;
}
return sum;
}
void Word::increment ()
{
++count;
}
int Word::getCount () const
{
return count;
}
char* Word::getWord () const
{
return w;
}
int Word::getKey () const
{
return n;
}
void Word::setCount (int count)
{
this -> count = count;
}
void Word::setWord (char *p)
{
w = p;
}
void Word::setKey (int n)
{
this -> n = n;
}


===HashTable.h===
Code: Select all
    #include "Word.h"
    class HashTable
    {
    public:
    HashTable();
    HashTable(int);
    void insertion (Word);
    void printTable () const;
    private:
    Word* wordArray;
    int size;
    };


===HashTable.cpp===
Code: Select all
    #include "HashTable.h"
    HashTable::HashTable()
    {
    }
    HashTable::HashTable(int size)
    {
    wordArray = new Word[size]; // insertion the size of the table
    char *ww = new char [20];
    for(int i = 0; i < size; i++)
    {
    wordArray[i].setWord ("\0");
    wordArray[i].setCount(0);
    }
    this-> size = size;
    }
    void HashTable::insertion (Word w)
    {
    int n = w.getSumASCII () % size;
    int i = n;
    bool found = false;
    bool collision = false;
    while (!found)
    {
    if (wordArray [i].getCount() == 0)
    {
    cout << w.getWord () << "\t";
    cout << "with % value = " << n << " inserted"
    << endl;
    wordArray [i] = w;
    wordArray[i].setKey (n);
    found = true;
    }
    else if (strcmp (wordArray[i].getWord (), w.getWord ()) == 0 && w.getCount() != 0)
    {
    wordArray [i].increment ();
    cout << w.getWord () << "\t";
    cout << "increase count" << endl;
    found = true;
    }
    else
    {
    collision = true;
    ++i;
    i = i % size;
    }
    }
    if (collision)
    {
    cout << w.getWord () << "\t";
    cout << "with % value = " << n
    << " inserted with collisions"
    << endl;
    }
    }
    void HashTable::printTable () const
    {
    double num, rate;
    cout <<endl
    << "Summary of Hash table, position occupied"
    << endl << endl;
    cout << "Element"
    << "\t" << "\t"
    << "Word" << "\t"
    << "Sum ASCII" << "\t"
    << "Mod 15" << "\t" << "\t"
    << "Count" << endl;
    for (int i = 0; i < size; i++)
    {
    if(wordArray[i].getCount () == 0)
    {
    }
    else
    {
    cout << "Table ["
    << i << "]\t"
    << wordArray [i].getWord () << "\t"
    << wordArray [i].getSumASCII ()
    << "\t" << "\t"
    << wordArray [i].getKey () << "\t" << "\t"
    << wordArray [i].getCount () << "\t"
    << endl;
    ++num;
    }
    }
    cout << endl;
    rate = (num /size) * 100;
    cout << "Occupancy rate: " << rate << "%";
    }


===BST.h===
Code: Select all
    #include "HashTable.h"
    class BST
    {
    public:
    BST ();
    ~BST ();
    void insert (Word *); //insert hash table
    bool findNode (Word) const;
    void printBST () const;
    private:
    struct Node;
    typedef Node* NodePtr;
    struct Node
    {
    Word data;
    NodePtr left, right;
    };
    NodePtr root;
    int compareWP (Word, Word) const;
    void insert (NodePtr&, Word);
    bool findNode (NodePtr, Word) const;
    void inorderPrint (NodePtr) const;
    };


===BST.cpp===
Code: Select all
    #include "BST.h"
    BST::BST()
    {
    root = NULL;
    }
    BST::~BST()
    {
    destroy (root);
    //destroy not declared
    }
    void BST::insert (Word * w) //inserting hash table
    {
    insert (root, w);
    //Error will occur - resulted no matching function for call to 'BST::insert (BST::Node*&, Word*&)'
    }
    bool BST::findNode (Word w) const
    {
    return findNode (root, w);
    }
    void BST::printBST () const
    {
    inorderPrint (root);
    }
    /*int BST::compareWP (Word w1, Word w2) const
    {
    //I'm wondering must I really compare the words?
    }*/
    void BST::insert (NodePtr& root, Word w)
    {
    if (root == NULL)
    {
    NodePtr temp = new Node;
    temp -> data = w;
    temp -> left = NULL;
    temp -> right = NULL;
    root = temp;
    }
    else if (compareWP (root -> data, w) > 0)
    insert (root -> left, w);
    else
    insert (root -> right, w);
    }
    bool BST::findNode (NodePtr root, Word w) const
    {
    if (root == NULL)
    return false;
    else
    {
    int k = compareWP (root -> data, w);
    if (k == 0)
    return true;
    else if (k > 0)
    return findNode (root -> left, w);
    else
    return findNode (root -> right, w);
    }
    }
    void BST::inorderPrint (NodePtr) const
    {
    }


===Main.cpp===
Code: Select all
    #include "HashTable.h"
    int main()
    {
    int size;
    int num;
    char* filename = new char [20];
    fstream fin;
    cout << "Enter the file name for analysis: ";
    cin >> filename;
    cout << endl;
    fin.open(filename, ios::in);
    if(!fin)
    {
    cout << "File unable to open!" << endl;
    exit(1);
    }
    cout << "Enter the size of hash table: ";
    cin >> size;
    HashTable table (size);
    cout << "How many top popular words?: ";
    cin >> num;
    cout << "Analysis of insertion"
    << endl << endl;
    char* p = new char [20];
    while(fin >> p)
    {
    //p = strtok (NULL, " .,?:;");
    Word w(p);
    table.insertion (w);
    }
    table.printTable ();
    }
Cresenia
 
Posts: 2
Joined: Fri May 25, 2012 9:56 pm
Location: Small little dot

Re: URGENT HELP needed! BST Output the popular words

Postby exomo » Sat May 26, 2012 1:51 am

Sorry your time is up, but I'll answer anyway just in case you are still interested.

Code: Select all
void BST::insert (Word * w) //inserting hash table
{
    insert (root, *w);
    //Error will occur - resulted no matching function for call to 'BST::insert (BST::Node*&, Word*&)'
}
w is a pointer to Word here, the other insert method requires a word (no pointer). Just change your insert to have a parameter Word w.

Do you use char arrays for a reason? In most cases it is a lot easier to use the c++ string class. You don't have to care about memory allocation or word lengths, you can use comparison operators, and so on... I didn't look through all of your code, but there are at least some memory leaks, you allocate memory that is never deleted. If you want to use the char arrays, you have to #include <cstring> in every file that uses c string functions.

Code: Select all
/*int BST::compareWP (Word w1, Word w2) const
{
//I'm wondering must I really compare the words?
}*/
Of course you have to compare the words in a binary tree. You can use strcmp()
Code: Select all
int BST::compareWP (Word w1, Word w2) const
{
    return strcmp(w1.getWord(), w2.getWord());
}
Who needs a signature anyway.
User avatar
exomo
 
Posts: 879
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: URGENT HELP needed! BST Output the popular words

Postby Cresenia » Sat May 26, 2012 2:06 am

exomo wrote:Sorry your time is up, but I'll answer anyway just in case you are still interested.

Code: Select all
void BST::insert (Word * w) //inserting hash table
{
    insert (root, *w);
    //Error will occur - resulted no matching function for call to 'BST::insert (BST::Node*&, Word*&)'
}
w is a pointer to Word here, the other insert method requires a word (no pointer). Just change your insert to have a parameter Word w.

Do you use char arrays for a reason? In most cases it is a lot easier to use the c++ string class. You don't have to care about memory allocation or word lengths, you can use comparison operators, and so on... I didn't look through all of your code, but there are at least some memory leaks, you allocate memory that is never deleted. If you want to use the char arrays, you have to #include <cstring> in every file that uses c string functions.

Code: Select all
/*int BST::compareWP (Word w1, Word w2) const
{
//I'm wondering must I really compare the words?
}*/
Of course you have to compare the words in a binary tree. You can use strcmp()
Code: Select all
int BST::compareWP (Word w1, Word w2) const
{
    return strcmp(w1.getWord(), w2.getWord());
}



Thanks exomo. It's alright if the time is up but at least what I want is to learn. In case, in the near future I might encounter such obstacle.

Code: Select all
int BST::compareWP (Word i, Word w) const
{
   
   if (i.getWord() == w.getWord ())
      return 0;
   else if (i.getWord() > w.getWord ())
      return 1;
   else
       return -1;

}


Can I code it like this? I'm still trying to get the output for the popular words (cin >> num).

I have my latest code below for BST.h, BST.cpp and main.cpp:

===BST.h===
Code: Select all
#include "HashTable.h"

class BST
{
   public:
      BST ();
      ~BST ();
      
      void insert (Word *); //insert hash table
      bool findNode (Word) const;
      void printBST () const;
      
   private:
      struct Node;
      typedef Node* NodePtr;
      
      struct Node
      {
         Word data;
         NodePtr left, right;
      };
      
      NodePtr root;
      
      void insert (Word);
      
      int compareWP (Word, Word) const;
      
      void insert (NodePtr&, Word);
      bool findNode (NodePtr, Word) const;
      void inorderPrint (NodePtr) const;
};



===BST.cpp===
Code: Select all
#include "BST.h"
   
BST::BST()
{
   root = NULL;
}
   
BST::~BST()
{
//  destroy (root);
}
   
void BST::insert (Word w)
{
   insert (root, w);
}
   
bool BST::findNode (Word w) const
{
   return findNode (root, w);
}
   
void BST::printBST () const
{
   inorderPrint (root);
}
   
int BST::compareWP (Word i, Word w) const
{
   
   if (i.getWord() == w.getWord ())
      return 0;
   else if (i.getWord() > w.getWord ())
      return 1;
   else
       return -1;
   
}
         
void BST::insert (NodePtr& root, Word w)
{
   if (root == NULL)
   {
      NodePtr temp = new Node;
      temp -> data = w;
      temp -> left = NULL;
      temp -> right = NULL;
      
      root = temp;
   }
   else if (compareWP (root -> data, w) > 0)
      insert (root -> left, w);
   else
      insert (root -> right, w);
}
   
bool BST::findNode (NodePtr root, Word w) const
{
   if (root == NULL)
      return false;
   else
   {
      int k = compareWP (root -> data, w);
      
      if (k == 0)
         return true;
      else if (k > 0)
         return findNode (root -> left, w);
      else
         return findNode (root -> right, w);
   }
}
   
void BST::inorderPrint (NodePtr root) const
{
   Word w;
   w = root->data;
   if (root != NULL)
   {
      inorderPrint (root -> left);
      cout << w.getWord () << w.getCount();
      inorderPrint (root -> right);
   }
   else
      cout << endl;
}


===main.cpp===
Code: Select all
#include "BST.h"

int main()
{
   int size;
   int num;
   char* filename = new char [20];
   fstream fin;
   
   cout << "Enter the file name for analysis: ";
   cin >> filename;
   cout << endl;
   
   fin.open(filename, ios::in);
   
   if(!fin)
   {
      cout << "File unable to open!" << endl;
      exit(1);
   }
   
   cout << "Enter the size of hash table: ";
   cin >> size;
   
   HashTable table (size);
   
   cout << "How many top popular words?: ";
   cin >> num;
   
   
   BST b1 ();
   
   cout << "Analysis of insertion"
      << endl << endl;
      
   char* p = new char [20];
   
   while(fin >> p)
   {
      Word w(p);
      table.insertion (w);    
   }
   
   table.printTable ();
   b1.printBST (); //There's an error here. What should I do? ('error: request for member 'printBST' in 'b1', which non-class type 'BST ()()' )
}   

Cresenia
 
Posts: 2
Joined: Fri May 25, 2012 9:56 pm
Location: Small little dot

Re: URGENT HELP needed! BST Output the popular words

Postby exomo » Sat May 26, 2012 8:55 am

Your compareWP could work if you used strings. But you can't use it to compare char array strings. Actually you are comparing the pointers/addresses of the allocated memory. To compare c-strings you must use strcmp().
And for better code encapsulation compareWP() should not be a member of BST, but rather belong to the Word class somehow. You could overload the <, >, = operators for Word, or make a static compare method or something.

Create your b1 object without (), just "BST b1";

And btw: root is a member of your BST class, there is no need to pass it to any method of this class. You don't need inorderPrint and the second insert. Just use the methods without root parameter and do everything you want to do in there.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 879
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden


Return to Homeworks

Who is online

Users browsing this forum: Google Adsense [Bot] and 1 guest