Question: #ifndef AUTOCOMPLETER _ H #define AUTOCOMPLETER _ H #include #include using namespace std; class Autocompleter { / / For the mandatory running times below: /
#ifndef AUTOCOMPLETERH
#define AUTOCOMPLETERH
#include
#include
using namespace std;
class Autocompleter
For the mandatory running times below:
n is the number of strings in the dictionary.
Assume that the length of every string is O
public:
Creates a new Autocompleter with an empty dictionary.
Must run in O time.
Autocompleter;
Adds a string x to the dictionary.
If x is already in the dictionary, does nothing.
Must run in Ologn time.
void insertstring x int freq;
Returns the number of strings in the dictionary
of possible completions.
Must run in On time.
int size;
Fills the vector T with the three mostfrequent completions of x
If x has less than three completions, then
T is filled with all completions of x
The completions appear in T from most to least frequent.
Must run fast. In particular, you should not search all nodes in the
tree for possible completions.
Instead, only search regions of the tree for which a completion could
be present, which will yield a run time bound of Ok log n time,
where k is the number of completions in the tree.
void completionsstring x vector &T;
Reports height of the AVL tree, runs in O time.
int height
return heightroot;
private:
A helper class that stores a string and a frequency.
class Entry
public:
string s;
int freq;
;
A helper class that implements a binary search tree node.
class Node
public:
Node
height ;
left right nullptr;
NodeEntry e
thise e;
height ;
left right nullptr;
Entry e;
int height;
Node left;
Node right;
;
A convenience method for getting the height of a subtree.
Returns the height of the subtree rooted at p
Useful for rebalance
static int heightNode p
if p nullptr
return ;
return pheight;
Root of the binarysearchtreebased data structure
Node root;
Optional helper methods youll probably want them
Returns the size of the binary tree rooted at p
Should run in On time.
int sizerecurseNode p;
Fills C with the completions of x in the BST rooted at p
void completionsrecursestring x Node p vector &C;
Inserts an Entry into an AVL tree rooted at p
Should run in Ologn time.
void insertrecurseEntry e Node &p;
Rebalances the AVL tree rooted at p
Helpful for insert
Should be called on every node visited during
the search in reverse search order.
Should run in O time.
void rebalanceNode &p;
Perform left and right rotations
of an AVL tree rooted at p helpful for implementing rebalance
Should run in O time.
void rightrotateNode &p;
void leftrotateNode &p;
A useful method to update
the height of a node,
assuming subtrees already have
the correct height.
void updateheightNode& p
if p nullptr
pheight maxheightpleft heightpright;
;
#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
