Objective : To gain experience with the operations involving binary search trees
ID: 666157 • Letter: O
Question
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.
Note:
Your result should be printed to an output file named YourUserID.txt.
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.
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.
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---
------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
//Binary Search Tree Program
#include <iostream>
#include <cstdlib>
#include<string>
using namespace std;
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
string word;
};
tree_node* root=NULL;
void inorder(tree_node*);
void insert(string);
// Smaller elements go left
// larger elements go right
void insert(string word)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = 1;
t->left = NULL;
t->right = NULL;
t->word=word;
parent = NULL;
// is this a new tree?
if(root==NULL)
{
root = t;
return;
}
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
parent = curr;
if(curr->word == t->word)
{
curr->data +=1;
return;
}
else if(t->word > curr->word)
curr = curr->right;
else
curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
void inorder(tree_node* p)
{
if(p==NULL)
return;
inorder(p->left);
ofstream out("output.txt",ios::app);
cout<<p->word<<" "<<p->data<<endl;
out<<p->word<<" "<<p->data<<endl;
out.close();
inorder(p->right);
}
int main()
{
string tmp;
ifstream in("YourUserID.txt",ios::in);
while(in>>tmp)
{
insert(tmp);
}
inorder(root);
}
OR
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct wc
{
string word;
int cnt;
};
int find(vector <wc> index, string s);
bool compare(const wc &a, const wc &b)
{
return a.word < b.word;
}
int main()
{
string s;
int ind,i;
vector <wc> index;
struct wc temp;
while(cin>>s)
{
ind = find(index,s);
if(ind!=-1)
{
index[ind].cnt+=1;
}
else
{
temp.word=s;
temp.cnt=1;
index.push_back(temp);
}
}
sort(index.begin(),index.end(),compare);
for(i=0;i<index.size();i++)
{
cout<<index[i].word<<" "<<index[i].cnt<<endl;
}
}
int find(vector <wc> index, string s)
{
for(int i=0;i<index.size();i++)
{
if(index[i].word==s)
return i;
}
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.