Question

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 characters to be lower case, and then insert them into your data structure. The provided dictionaries are all lower case without punctuation. Once the structures are built, the program should write an output file with the following format: The title of the first classic text Number of words not appearing in the first dictionary, followed by the words Number of words not appearing in the second dictionary, followed by the words The total number of words in the text, followed by the unique words (Note: this will be the same as the Sum of the frequencies, and the number of nodes in your binary search tree for the classic text) The number of words in the requested word range and the frequency of each of those words:

The title of the first classic text

Number of words not appearing in the first dictionary, followed by the words

Number of words not appearing in the second dictionary, followed by the words

The total number of words in the text, followed by the unique words (Note: this will be the same as the Sum of the frequencies, and the number of nodes in your binary search tree for the classic text) .

The number of words in the requested word range and the frequency of each of those words.

Their frequencies in a specified lexical range (called RangeQuery) for each text (for example, all the words between raft and reflex might be : rag, rage, rails, rearranged, and reels).

Here is the pseudo code for the RangeQuery:

Algorithm RangeQuery(key1, key2, v):

Input: Search keys, key1 and key2 and a node v of a binary search tree T

Output: The elements stored in the subtree of T rooted at v whose keys are in the range [key1,key2]

if T.isExternal(v) then return 0

if key1 <= key(v) <= key2 then print RangeQuery(key1, key2, T.leftChild(v))

printData(v);

print RangeQuery(key1, key2, T.rightChild(v))

else if key(v) < key1 then print RangeQuery(key1, key2, T.rightChild(v))

else if key2 < key(v) then print RangeQuery(key1, key2, T.leftChild(v)) end RangeQuery

I have to make 4 text files, one for each of the classic texts.

Here is an example of an output for all the words between raft and reflex in the War And Peace Text:

War And Peace

0

2 : Michaela, gumshoed

10,431 total words

2,010 unique words

8

rag : 1

rage : 3

rails : 4

rearranged : 2

reels : 1

Here is my Code:

// bst.h

#ifndef bst_h
#define bst_h

#ifndef NULL
#define NULL 0x00
#endif


#include <string>

class BSTNode {
private:
  
std::string data;
int frequency;
  
BSTNode *left;
BSTNode *right;
  
public:
  
BSTNode(std::string d) { data = d; frequency = 1; left = NULL; right = NULL; }
~BSTNode() {}

friend class BSTree;
};


class BSTree {
private:
  
BSTNode *root;

void destroy(BSTNode*);
  
BSTNode* find(BSTNode*, std::string);

void increment_frequency(BSTNode *ptr);
  
void insert(BSTNode**, std::string);
  
void print_list(BSTNode*, int*);
  
void print_range(std::string, std::string, BSTNode*);

  
public:
  
BSTree();
~BSTree();
  
void insert(std::string);
  
void print_list(int n);

void print_tree(int n);
  
void print_range(std::string, std::string); // output all the strings in the tree lexically between the parameters
};


#endif

--------------------------------------------------------------------

// bst.cpp

//#include "bst.h"
//#include <iostream>

//BSTree::BSTree()
{
// root = NULL;
//}

destructor:
// call destroy on the root of the tree

destroy(BSTNode* p)
// **recursively** destroy the tree pointed to by p (what traversal?)

find(BSTNode* node, std::string word)
// **recursively** search for word starting at node
// return a pointer to the target node -OR-
// the pointer where the target node should be

increment_frequency(BSTNode* ptr)
// increment the frequency at the node ptr

insert( BSTNode* *p, std::string word)
// if the pointer p is pointing to null then
// create a new node at p
// otherwise, _insert_ word to the left or right subtree
// (**recursively**)

insert(std::string word)
// insert word to the root of the tree by first _finding_ the
// place to insert. If word is already in the tree, _increment_
// its frequency, otherwise _insert_ the word

print_list( BSTNode* p, int *n)
// if p is not null,
// if we have more to print
// recursively print the left tree
// cout the data : frequency and decrement the count (n)
// print the right tree. (what traversal?)

print_list(int n)
// call print_list on the root

print_tree(int n)
// call print_list on the root

print_range( std::string k1, std::string k2, BSTNode* p)
// use pseudocode Range Query

print_range( std::string k1, std::string k2)
// call print_range on the root of this tree

--------------------------------------------------------------

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <ctime>
// add any other needed include files..

int main(int argc, const char * argv[]) {
  
// declare the data structures here
// BSTree myTree;
  

// Here is the code I started ..
// std::ifstream infile(argv[1]);
  
// std::string line;
// std::string word;

  
// while(std::getline(infile, line))
// {
// std::istringstream iss(line);
// while(iss >> word)
// {
// word.erase(std::remove_if (word.begin(), word.end(), ispunct), word.end());
// std::transform(word.begin(), word.end(), word.begin(), ::tolower);
  
//
// at this point word contains a punctuation free lower case word
// from the text (HINT: do something with it)
//
  
// }
// }
  
return 0;
}

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

PROGRAM :

#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#define COUNT 5
using namespace std;
struct node
{
string data;
node *Left_link;
node *Right_link;
};
node* insert_node(node *temp,node *newnode);
struct node *root=NULL;
class Binarytree
{
public:
bool add(string str);
public:
void Display();
void DrawTree(struct node *root, int space);
};
bool Binarytree::add(string x)
{
node *temp=root;
node *newnode;
newnode=new node;
newnode->Left_link=NULL;
newnode->Right_link=NULL;
newnode->data=x;
root=insert_node(temp,newnode);
}
node* insert_node(node *temp,node *newnode)
{
if(temp==NULL)
{
temp=newnode;
}
else if(temp->data < newnode->data)
{
insert_node(temp->Right_link,newnode);
if(temp->Right_link==NULL)
temp->Right_link=newnode;
}
else
{
insert_node(temp->Left_link,newnode);
if(temp->Left_link==NULL)
temp->Left_link=newnode;
}
return temp;
}
void inorderdisplay(node *t = root)
{
if(root==NULL)
{
printf("\nThe Tree is Empty");
}
else if(t!=NULL)
{
inorderdisplay(t->Left_link);
cout<<t->data<<"-->";
inorderdisplay(t->Right_link);
}
}
node* TreeSearch(node *root, string key)
{
node *temp;
temp = root;
while (temp != NULL)
{
if(temp->data==key)
{
cout<<"\nThe Element "<<temp->data<<"is Present"<<endl;
return temp;
}
if(temp->data > key)
temp = temp->Left_link;
else
temp = temp->Right_link;
}
return NULL;
}
void printSideways(node *root, int space)
{
if (root == NULL)
return;
space += COUNT;
printSideways(root->Right_link, space);
cout<<endl;
for(int i=COUNT;i<space;i++)
cout<<" ";
cout<<root->data;
printSideways(root->Left_link, space);
}
int main()
{
Binarytree obj;
string datavalue;
ifstream infile;
infile.open("words.txt");//reading Words from the File
for(int i=0;i<8;i++)
{
infile>>datavalue;
obj.add(datavalue);
}
int option;
cout<<"\n-------------------------"<<endl;
cout<<"\nImplementation of Binary tree"<<endl;
while(true)
{
cout<<"\n** MENU **"<<endl;
cout<<"\n1.Insert the Data"<<endl;
cout<<"\n2.Display"<<endl;
cout<<"\n3.Search"<<endl;
cout<<"\n4.printSideways"<<endl;
cout<<"\n5.exit"<<endl;
cout<<"\nPlease Select any Choice"<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<"\nPlease Enter the New Node"<<endl;
cin>>datavalue;
obj.add(datavalue);
break;
case 2:
cout<<"\nInorder Display"<<endl;
inorderdisplay();
break;
case 3:
cout<<"\nEnter the Word to Search:"<<endl;
cin>>datavalue;
TreeSearch(root,datavalue);
break;
case 4:
printSideways(root,4);
break;
case 5:
exit(0);
default:
cout<<"\nInvalid Option"<<endl;
}
}
}

input file(words.txt):-
===============
Beth
Sue
Dave
Pat
Mike
Dawn
Cindi
Gina
Sample output:-
============
-------------------------
Implementation of Binary tree
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
4
Sue
Pat
Mike
Gina
Dawn
Dave
Cindi
Beth
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
2
Inorder Display
Beth-->Cindi-->Dave-->Dawn-->Gina-->Mike-->Pat-->Sue-->
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
2
Inorder Display
Beth-->Cindi-->Dave-->Dawn-->Gina-->Mike-->Pat-->Sue-->
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
3
Enter the Word to Search:
Dave
The Element Daveis Present
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
1
Please Enter the New Node
venkanna
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
2
Inorder Display
Beth-->Cindi-->Dave-->Dawn-->Gina-->Mike-->Pat-->Sue-->venkanna-->
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
4
venkanna
Sue
Pat
Mike
Gina
Dawn
Dave
Cindi
Beth
** MENU **
1.Insert the Data
2.Display
3.Search
4.printSideways
5.exit
Please Select any Choice
5
--------------------------------
Process exited after 144.4 seconds with return value 0
Press any key to continue . . .

Implementation of Binary tree MENU 1. Insert the Data 2.Display 3.Search 4.printsideways 5.exit Please Select any Choice 4 Sue Pat Mike Gina Dawn Dave Cindi Beth MENU 1. Insert the Data 2.Display 3.Search 4.printsideways 5.exit Please Select any Choice Inorder Display

Add a comment
Know the answer?
Add Answer to:
I need help in C++ implementing binary search tree. I have the .h file for the...
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
  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • From the code below with Binary Search Tree recurrence T(n)=? use the recursion method and substitution method to solve the recurrence. Find the tightest bound g(n) for T(n) you can for which T(n)= O(...

    From the code below with Binary Search Tree recurrence T(n)=? use the recursion method and substitution method to solve the recurrence. Find the tightest bound g(n) for T(n) you can for which T(n)= O(g(n)). Explain your answer and use recursion tree method. void insert(int data) { struct node *tempNode = (struct node*) malloc(sizeof(struct node)); struct node *current; struct node *parent; tempNode->data = data; tempNode->leftChild = NULL; tempNode->rightChild = NULL; //if tree is empty if(root == NULL) { root = tempNode;...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

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