Question: I need help to implement the following class:: #ifndef AUTOCOMPLETER_H #define AUTOCOMPLETER_H #include #include #include #include using namespace std; class Autocompleter { private: // A
I need help to implement the following class::
#ifndef AUTOCOMPLETER_H
#define AUTOCOMPLETER_H
#include
#include
#include
#include
using namespace std;
class Autocompleter
{
private:
// A helper class that stores a string and a frequency.
struct Entry
{
public:
string s;
int freq;
};
// A helper class that implements a BST node.
class Node
{
public:
Node()
{
left = right = nullptr;
}
Node(Entry e)
{
this->e = e;
left = right = nullptr;
}
Node(Entry e, Node* left, Node* right)
{
this->e = e;
left = left;
right = right;
}
Entry e;
Node* left;
Node* right;
};
Node* root;
int TreeSize; //the size of the tree
public:
// Creates a dictionary of strings and associated frequencies,
// using the file as a source. The file is promised to have
// the following format:
//
// string1,freq1
// string2,freq2
// ...
// stringN,freqN
//
// where string1 < string2 < ... < stringN
//
// Must run in O(n) time.
// update the size of the tree
Autocompleter(string filename);
//insert a node to your BST
// Must run in O(\log n) time
// remember to update the size of the tree
void insert(string s, int freq)
{
root = insert(root, s, freq);
TreeSize++;
}
//insert a node of string s and frequence freq to your BST
// Must run in O(\log n) time
Node * insert(Node* node, string s, int freq);
// Returns the number of strings in the dictionary
//
// Must run in O(1) time.
int size();
// return the most frequent string which contains prefix x
// Must run in O(log(n) + k) time,
// where k is the number of strings with perfix x in the dictionary.
string completions(string x);
// print all strings in the increasing order in the tree
// Must run in O(n) time
// void InorderTraverse();
// Root of the binary-search-tree-based data structure
// Optional helper methods (you'll probably want them).
// Returns the root of a BST containing the elements
// of the portion of a sorted vector E from index l to r.
//
// Should run in O(r-l) time.
//Node* construct_recurse(vector
// Returns the size of the binary tree rooted at root.
//
// Should run in O(n) time.
//int size_recurse(Node* root);
// Fills T with the three most-frequent completions of x
// that are either:
// -In the BST rooted at root.
// -Already in T.
//
// Should run in O(log(n) + k), where
// -n is the size of the BST rooted at root.
// -k is the number of Entrys in the BST rooted at root
// whose strings start with x.
//void completions_recurse(string x, Node* root, vector
}
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
