Question

SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective...

SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS!

Objective

To gain experience with the operations involving binary search trees. This data structure as linked list uses dynamic memory allocation to grow as the size of the data set grows. Unlike linked lists, a binary search tree is very fast to insert, delete and search.

Project Description

When an author produce an index for his or her book, the first step in this process is to decide which words should go into the index; the second is to produce a list of the pages where each word occurs. Instead of trying to choose words out of our heads, we decided to let the computer produce a list of all the unique words used in the manuscript and their frequency of occurrence. We could then go over the list and choose which words to put into the index.

The main object in this problem is a "word" with associated frequency. The tentative definition of "word" here is a string of alphanumeric characters between markers where markers are white space and all punctuation marks; anything non-alphanumeric stops the reading. If we skip all un-allowed characters before getting the string, we should have exactly what we want. Ignoring words of fewer than three letters will remove from consideration such as "a", "is", "to", "do", and "by" that do not belong in an index.

In this project, you are asked to write a program to read any text file and then list all the "words" in alphabetic order with their frequency together appeared in the article. The "word" is defined above and has at least three letters.

  1. Your result should be printed to an output file named YourUserID.txt.
  2. You need to create a Binary Search Tree (BST) to store all the word object by writing an insertion or increment function. Finally, a proper traversal print function of the BST should be able to output the required results.
  3. The BST class in the text can not be used directly to solve this problem. It is also NOT a good idea to modify the BST class to solve this problem. Instead, the following codes are recommended to start your program.
//Data stored in the node type
 
 
struct WordCount
 
{
 
        string word;
 
        int count;
 
};
 
 
 
//Node type: 
 
struct TreeNode
 
{
 
        WordCount info;
 
        TreeNode * left;
 
        TreeNode * right;
 
};
 
 
// Two function's prototype
 
// Increments the frequency count if the string is in the tree
 
// or inserts the string if it is not there.
 
void Insert(TreeNode*&, string);
 
 
// Prints the words in the tree and their frequency counts.
 
void PrintTree(TreeNode* , ofstream&);
 
 
//Start your main function and the definitions of above two functions.

Sample Run

Please type the text file name: Lincoln.txt

Please give the output text file name: mus11.txt

You are done! You can open the file "mus11.txt" to check.

Press any key to continue

lincoln.txt

The Gettysburg Address

Gettysburg, Pennsylvania

November 19, 1863

Four score and seven years ago our fathers brought forth on this

continent, a new nation, conceived in

Liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or

any nation so conceived and

so dedicated, can long endure. We are met on a great battle-field of that

war. We have come to dedicate

a portion of that field, as a final resting place for those who here gave

their lives that that nation

might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate -- we can not consecrate -- we

can not hallow -- this ground.

The brave men, living and dead, who struggled here, have consecrated it,

far above our poor power to add

or detract. The world will little note, nor long remember what we say

here, but it can never forget what

they did here. It is for us the living, rather, to be dedicated here to

the unfinished work which they

who fought here have thus far so nobly advanced. It is rather for us to be

here dedicated to the great

task remaining before us -- that from these honored dead we take increased

devotion to that cause for

which they gave the last full measure of devotion -- that we here highly

resolve that these dead shall not

have died in vain -- that this nation, under God, shall have a new birth

of freedom -- and that government

of the people, by the people, for the people, shall not perish from the

earth.

mus11.txt

1863 1

Address 1

But 1

Four 1

Gettysburg 2

God 1

Liberty 1

November 1

Now 1

Pennsylvania 1

The 3

above 1

add 1

advanced 1

ago 1

all 1

altogether 1

and 6

any 1

are 3

battle-field 1

before 1

birth 1

brave 1

brought 1

but 1

can 5

cause 1

civil 1

come 1

conceived 2

consecrate 1

consecrated 1

continent 1

created 1

dead 3

dedicate 2

dedicated 4

detract 1

devotion 2

did 1

died 1

earth 1

endure 1

engaged 1

equal 1

far 2

fathers 1

field 1

final 1

fitting 1

for 5

forget 1

forth 1

fought 1

freedom 1

from 2

full 1

gave 2

government 1

great 3

ground 1

hallow 1

have 5

here 8

highly 1

honored 1

increased 1

larger 1

last 1

little 1

live 1

lives 1

living 2

long 2

measure 1

men 2

met 1

might 1

nation 5

never 1

new 2

nobly 1

nor 1

not 5

note 1

our 2

people 3

perish 1

place 1

poor 1

portion 1

power 1

proper 1

proposition 1

rather 2

remaining 1

remember 1

resolve 1

resting 1

say 1

score 1

sense 1

seven 1

shall 3

should 1

struggled 1

take 1

task 1

testing 1

that 13

the 9

their 1

these 2

they 3

this 4

those 1

thus 1

under 1

unfinished 1

vain 1

war 2

what 2

whether 1

which 2

who 3

will 1

work 1

world 1

years 1

SCREENSHOTS ONLY NO TEXT! Please add comments!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :

#include <iostream>
#include <string>
#include <fstream>


using namespace std;


struct WordCount
{
string word;
int count;
};

//Creates a struct data type for node type:
struct TreeNode

{
WordCount info;
TreeNode * left;
TreeNode * right;
};

// Function prototypes

void Insert(TreeNode*& root, string words);
// Increments the frequency count if the string is in the tree
// or inserts the string if it is not there.

void PrintTree(TreeNode* root, ofstream& outFile);
// Prints the words in the tree and their frequency counts.

string findWord(ifstream&);


int main()
{
string inFileName;
string outFileName;
ifstream inFile;
ofstream outFile;
string myWord;
int minLength;
TreeNode * root;

cout << "Please type the text file name: ";

cin >> inFileName;

inFile.open(inFileName.data());

if(!inFile) // Checks to see whether or not the file is there
{
cout << "File not found.";
return 0;
}

cout << "Please give the output text file name: ";

cin >> outFileName;

outFile.open(outFileName.data());

minLength = 3;

myWord = findWord(inFile);

while (inFile)
{
if (myWord.length() >= minLength)
{
Insert(root, myWord);
}
myWord = findWord(inFile);
}

PrintTree(root, outFile);
outFile.close();
inFile.close();

cout << endl; // Creates a blank line
cout << "You are done! You can open the file " << outFileName << " to check." << endl;

return 0;
}

void Insert(TreeNode*& root, string myWord)
{
if (root == NULL)
{
root = new TreeNode;
root->left = NULL;
root->right = NULL;
root->info.word = myWord;
root->info.count = 1;
}

else if (root->info.word == myWord)
{
root->info.count++;
}

else if (root->info.word < myWord)
{
Insert(root->right, myWord);
}

else
{
Insert(root->left, myWord);
}
}

void PrintTree(TreeNode* root, ofstream& outFile)
{
if (root != NULL)
{
PrintTree(root->left, outFile);
outFile << root->info.word;
outFile << " " << root->info.count;
outFile << endl;
PrintTree(root->right,outFile);
}
}

string findWord(ifstream& inFile)
{
char word;
string myWord = " ";

inFile.get(word);

while (inFile && !isalnum(word))
{
inFile.get(word);
}

if (!inFile)
{
return myWord;
}

else
{
do
{
word = tolower(word);
myWord = myWord + word;
inFile.get(word);
}
while (isalnum(word) && inFile);
}
return myWord;
}

Add a comment
Know the answer?
Add Answer to:
SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Four score and seven years ago our fathers brought forth on this continent, a new nation,...

    Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • Recursion and Trees Application – Building a Word Index Make sure you have read and understood...

    Recursion and Trees Application – Building a Word Index Make sure you have read and understood ·         lesson modules week 10 and 11 ·         chapters 9 and 10 of our text ·         module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT