Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE IN C++: read a file of words and count the occurrences of word and then pri

ID: 3838714 • Letter: C

Question

CODE IN C++:

read a file of words and count the occurrences of word and then print the words in alphabetic order with their count.

data structure used to store and count the words must be a binary tree. The tree must be in a class called WordTree. The WordTree class must provide operations to perform the following:

A constructor to initialize the tree

A recursive size operation - private

A recursive depth operation - private

An recursive insert operation that insert new nodes in the tree in order (sorted ascending) and, if the word is already in the tree, increases the word’s count by 1 - private

A recursive in order output operation that prints the words in alphabetical order with their counts - private

A “new node” helper operation that creates and returns (through the function call) a new node when one is needed for insertion into the tree - private

For all but the constructor and #6, public versions of operations 2 – 5 that are not recursive to be called from main (or wherever)

You can easily adapt the struct (or class) you made for words in Program 5 to this program.

STL classes are not permitted. You must code the tree operations.

should resemble this:

#include <iostream>

using namespace std;

struct Node {

   int       data;

   Node *left, *right;

  

};

class BinaryTree {

   Node   *root;

public:

  

   BinaryTree(){

       root = 0;

   }

  

   void insert(int val){

       root=insert(root, val);

      

   }

   void printPostOrder(){

       printPostOrder(root);

   }

   void printSorted(){

       printSorted(root);

   }

   int size(){

       return size(root);

   }

   int maxDepth(){

       return maxDepth(root);

   }

private:

   Node* NewNode(int val){

       Node* root = new Node();

       root->data = val;

       root->left = 0;

       root->right = 0;

       return root;

   }

   void printSorted(Node *node){

       if(node == 0) return;

       printSorted(node->left);

       cout << node->data << endl;

       printSorted(node->right);

   }

   void printPostOrder(Node *node){

       if(node == 0) return;

       printSorted(node->left);

       printSorted(node->right);

       cout << node->data << endl;

   }

  

   int size(Node *node){

       if(node==0)

           return 0;

       else

           return (size(node->left) + 1 + size(node->right));

   }

   Node* insert(Node* node, int val){

       if(node==0)

           return NewNode(val);

       else

           if(val <= node->data )

               node->left = insert(node->left, val);

           else

               node->right = insert(node->right, val);

       return node;

   }

  

   int maxDepth(Node *node){

       if(node==0)

           return 0;

       else

       {

           int leftDepth = maxDepth(node->left);

           int rightDepth = maxDepth(node->right);

           if(leftDepth>rightDepth)

               return leftDepth+1;

           else

               return rightDepth+1;

       }

   }

};

int main() {

   BinaryTree bt;//(11);

  

   bt.insert( 4);

   bt.insert( 17);

   bt.insert( 1);

   bt.insert( 7);

   cout << "Tree size is " << bt.size() << endl;

   cout << "Tree depth is " << bt.maxDepth() << endl;

   bt.printSorted();

   cout << "==========================" << endl;

   bt.printPostOrder();

   return 0;

}

Try you code on this:

To be or not to be that is the question Whether tis nobler in the mind to suffer

The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them To die to sleep No more and by a sleep to say we end The heartache and the thousand natural shocks That flesh is heir to Tis a consummation Devoutly to be wished To die to sleep To sleep perchance to dream ay there's the rub For in that sleep of death what dreams may come When we have shuffled off this mortal coil Must give us pause There's the respect That makes calamity of so long life For who would bear the whips and scorns of time The oppressor's wrong the proud man's contumely The pangs of despised love the law's delay The insolence of office and the spurns That patient merit of the unworthy takes When he himself might his quietus make With a bare bodkin Who would fardels bear To grunt and sweat under a weary life But that the dread of something after death The undiscovered country from whose bourn No traveller returns puzzles the will And makes us rather bear those ills we have Than fly to others that we know not of Thus conscience does make cowards of us all And thus the native hue of resolution Is sicklied o'er with the pale cast of thought And enterprise of great pitch and moment With this regard their currents turn awry And lose the name of action

produces:

A 5

ACTION 1

AFTER 1

AGAINST 1

ALL 1

AND 12

ARMS 1

ARROWS 1

AWRY 1

AY 1

BARE 1

BE 3

BEAR 3

BODKIN 1

BOURN 1

BUT 1

BY 2

CALAMITY 1

CAST 1

COIL 1

COME 1

CONSCIENCE 1

CONSUMMATION 1

CONTUMELY 1

COUNTRY 1

COWARDS 1

CURRENTS 1

DEATH 2

DELAY 1

DESPISED 1

DEVOUTLY 1

DIE 2

DOES 1

DREAD 1

DREAM 1

DREAMS 1

END 2

ENTERPRISE 1

FARDELS 1

FLESH 1

FLY 1

FOR 2

FORTUNE 1

FROM 1

GIVE 1

GREAT 1

GRUNT 1

HAVE 2

HE 1

HEARTACHE 1

HEIR 1

HIMSELF 1

HIS 1

HUE 1

ILLS 1

IN 2

INSOLENCE 1

IS 3

KNOW 1

LAW'S 1

LIFE 2

LONG 1

LOSE 1

LOVE 1

MAKE 2

MAKES 2

MAN'S 1

MAY 1

MERIT 1

MIGHT 1

MIND 1

MOMENT 1

MORE 1

MORTAL 1

MUST 1

NAME 1

NATIVE 1

NATURAL 1

NO 2

NOBLER 1

NOT 2

O'ER 1

OF 15

OFF 1

OFFICE 1

OPPOSING 1

OPPRESSOR'S 1

OR 2

OTHERS 1

OUTRAGEOUS 1

PALE 1

PANGS 1

PATIENT 1

PAUSE 1

PERCHANCE 1

PITCH 1

PROUD 1

PUZZLES 1

QUESTION 1

QUIETUS 1

RATHER 1

REGARD 1

RESOLUTION 1

RESPECT 1

RETURNS 1

RUB 1

SAY 1

SCORNS 1

SEA 1

SHOCKS 1

SHUFFLED 1

SICKLIED 1

SLEEP 5

SLINGS 1

SO 1

SOMETHING 1

SPURNS 1

SUFFER 1

SWEAT 1

TAKE 1

TAKES 1

THAN 1

THAT 7

THE 21

THEIR 1

THEM 1

THERE'S 2

THIS 2

THOSE 1

THOUGHT 1

THOUSAND 1

THUS 2

TIME 1

TIS 2

TO 15

TRAVELLER 1

TROUBLES 1

TURN 1

UNDER 1

UNDISCOVERED 1

UNWORTHY 1

US 3

WE 4

WEARY 1

WHAT 1

WHEN 2

WHETHER 1

WHIPS 1

WHO 2

WHOSE 1

WILL 1

WISHED 1

WITH 3

WOULD 2

WRONG 1

Press any key to continue . . .

Explanation / Answer

Modified the given program to suit the requirements in the question. Sample output is attached. Please don't forget to rate the answer if it helped. Thank you very much.

#include <iostream>
#include <fstream>
using namespace std;
struct Node {
string word;
int count;
Node *left, *right;

};
class WordTree {
Node *root;
public:
//constructor
WordTree(){
root = 0;
}

void insert(string val){
root=insert(root, val);

}
void printPostOrder(){
printPostOrder(root);
}
void printSorted(){
printSorted(root);
}
int size(){
return size(root);
}
int maxDepth(){
return maxDepth(root);
}
private:
Node* NewNode(string val){
Node* root = new Node();
root->word = val;
root->count = 1;
root->left = 0;
root->right = 0;
return root;
}
void printSorted(Node *node){
if(node == 0) return;
printSorted(node->left);
cout << node->word <<" "<<node->count<< endl;
printSorted(node->right);
}
void printPostOrder(Node *node){
if(node == 0) return;
printSorted(node->left);
printSorted(node->right);
cout << node->word << endl;
}

int size(Node *node){
if(node==0)
return 0;
else
return (size(node->left) + 1 + size(node->right));
}
Node* insert(Node* node, string val){
if(node==0)
return NewNode(val);
else
if(val < node->word)
node->left = insert(node->left, val);
else if(val > node->word)
node->right = insert(node->right, val);
else
node->count++;
return node;
}

int maxDepth(Node *node){
if(node==0)
return 0;
else
{
int leftDepth = maxDepth(node->left);
int rightDepth = maxDepth(node->right);
if(leftDepth>rightDepth)
return leftDepth+1;
else
return rightDepth+1;
}
}
};

//helper function to convert a word to upper case
string toupper(string word)
{
for(int i=0; i<word.length(); i++)
word[i] = toupper(word[i]);

return word;
}

int main() {
WordTree tree;
ifstream infile;
string filename;

cout<<"Enter input filename : ";
cin>>filename;

infile.open(filename.c_str());

if(!infile.is_open())
{
cout<<"Error opening file "<<filename<<endl;
return 1;
}

string word;

while(infile>>word)
{
word = toupper(word);
tree.insert(word);
}

tree.printSorted();
return 0;
}

sample input file sample.txt

To be or not to be that is the question Whether tis nobler in the mind to suffer
The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them To die to sleep No more and by a sleep to say we end The heartache and the thousand natural shocks That flesh is heir to Tis a consummation Devoutly to be wished To die to sleep To sleep perchance to dream ay there's the rub For in that sleep of death what dreams may come When we have shuffled off this mortal coil Must give us pause There's the respect That makes calamity of so long life For who would bear the whips and scorns of time The oppressor's wrong the proud man's contumely The pangs of despised love the law's delay The insolence of office and the spurns That patient merit of the unworthy takes When he himself might his quietus make With a bare bodkin Who would fardels bear To grunt and sweat under a weary life But that the dread of something after death The undiscovered country from whose bourn No traveller returns puzzles the will And makes us rather bear those ills we have Than fly to others that we know not of Thus conscience does make cowards of us all And thus the native hue of resolution Is sicklied o'er with the pale cast of thought And enterprise of great pitch and moment With this regard their currents turn awry And lose the name of action

output

Enter input filename : sample.txt
A 5
ACTION 1
AFTER 1
AGAINST 1
ALL 1
AND 12
ARMS 1
ARROWS 1
AWRY 1
AY 1
BARE 1
BE 3
BEAR 3
BODKIN 1
BOURN 1
BUT 1
BY 2
CALAMITY 1
CAST 1
COIL 1
COME 1
CONSCIENCE 1
CONSUMMATION 1
CONTUMELY 1
COUNTRY 1
COWARDS 1
CURRENTS 1
DEATH 2
DELAY 1
DESPISED 1
DEVOUTLY 1
DIE 2
DOES 1
DREAD 1
DREAM 1
DREAMS 1
END 2
ENTERPRISE 1
FARDELS 1
FLESH 1
FLY 1
FOR 2
FORTUNE 1
FROM 1
GIVE 1
GREAT 1
GRUNT 1
HAVE 2
HE 1
HEARTACHE 1
HEIR 1
HIMSELF 1
HIS 1
HUE 1
ILLS 1
IN 2
INSOLENCE 1
IS 3
KNOW 1
LAW'S 1
LIFE 2
LONG 1
LOSE 1
LOVE 1
MAKE 2
MAKES 2
MAN'S 1
MAY 1
MERIT 1
MIGHT 1
MIND 1
MOMENT 1
MORE 1
MORTAL 1
MUST 1
NAME 1
NATIVE 1
NATURAL 1
NO 2
NOBLER 1
NOT 2
O'ER 1
OF 15
OFF 1
OFFICE 1
OPPOSING 1
OPPRESSOR'S 1
OR 2
OTHERS 1
OUTRAGEOUS 1
PALE 1
PANGS 1
PATIENT 1
PAUSE 1
PERCHANCE 1
PITCH 1
PROUD 1
PUZZLES 1
QUESTION 1
QUIETUS 1
RATHER 1
REGARD 1
RESOLUTION 1
RESPECT 1
RETURNS 1
RUB 1
SAY 1
SCORNS 1
SEA 1
SHOCKS 1
SHUFFLED 1
SICKLIED 1
SLEEP 5
SLINGS 1
SO 1
SOMETHING 1
SPURNS 1
SUFFER 1
SWEAT 1
TAKE 1
TAKES 1
THAN 1
THAT 7
THE 21
THEIR 1
THEM 1
THERE'S 2
THIS 2
THOSE 1
THOUGHT 1
THOUSAND 1
THUS 2
TIME 1
TIS 2
TO 15
TRAVELLER 1
TROUBLES 1
TURN 1
UNDER 1
UNDISCOVERED 1
UNWORTHY 1
US 3
WE 4
WEARY 1
WHAT 1
WHEN 2
WHETHER 1
WHIPS 1
WHO 2
WHOSE 1
WILL 1
WISHED 1
WITH 3
WOULD 2
WRONG 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote