Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the copy constructor, firstWithPrefix(), load_trie() In these implementations, neither the length of the word nor the length of the input string is

Need help with the copy constructor, firstWithPrefix(), load_trie()

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

In these implementations, neither the length of the word nor the length of the input string is specified. That means recursion could be used for the trie structure. So as a hint, most of the functions are recursive. After you've designed the functions in 'trie.cpp', which should be submitted on Gradescope, you should write a test file to check if they are correct Tried The default constructor that initializes the 'roots' array to be all NULL pointers. Trie(Trie const& other) The copy constructor that copies 'other' into this'; Trie) The destructor that deallocates memory space in the Trie structure. Trie& operator=(Trie const& other) Similar to the copy constructor, it sets the current trie structure identical to 'other'. void insert(char const* const str) Insert a word in 'str into Trie. Ignores the other characters than letters. bool check(char const* const str) const Check if the word in 'str' exists in Trie. char* firstWithPrefix(char const* const str,unsigned depth) const Returns the first word with a given prefix in str. depth is by default 0 and has no specific functionality here. For example, if there are apple' and apply' in the Trie, and the prefix is 'a', 'ap', 'app' or 'appl', you should just return the first one 'apple' according to the alphabetical order. char* Trie::firstWithPrefix(char const* const str, unsigned depth) const{ // You'll need two paths to solve this problem: // Case 1: You've used up all characters in str and are looking for the next stored input. Search depth-first in lexographic (a-z) order, // stopping when you encounter the first endpoint you marked in insert(). // Case 2: You have characters left in the input. Act like check(), // but when you run out of input, proceed to the other case. // If you can't find any matching roots to a chunk of input, return NULL. // If you encounter unrecognized characters in the input, skip them. // Increment depth on every recursion, except unrecognized characters. // For your return value, you can't allocate your cstring in advance // because you do not know how deep in the structure you will have to // search. Instead, increment depth on every (not-unrecognized) recursion. // Then, when you reach an end of word you want to return, allocate your // new char[depth+1] and fill it backward, based on your depth! // Remember to check whether your recursive call gave you back NULL! // TODO: Fix this stub. Trie load_trie(istream& is){ // Given an open input stream, read words on separate lines into a new Trie. // If no lines can be read from the input stream, return a default-constructed // Otherwise, return a Trie filled with all words that can be read until the // first empty line. // Words will be placed on individual lines, like: // apple // application // applied apply // // (The last line there was empty, indicating the end of the dictionary.) // You can read these easily with one of the formats of std::getline(). // Remember, you already skip whitespace in Trie::insert(), so don't worry // about stripping any whitespace here! // TODO: Fix this stub. Trie load_trie(string filename) { // Open an input stream to the file 'filename`, // then pass that input stream to the other form of load_trie(). // Should be easy! return Trie(); } In these implementations, neither the length of the word nor the length of the input string is specified. That means recursion could be used for the trie structure. So as a hint, most of the functions are recursive. After you've designed the functions in 'trie.cpp', which should be submitted on Gradescope, you should write a test file to check if they are correct Tried The default constructor that initializes the 'roots' array to be all NULL pointers. Trie(Trie const& other) The copy constructor that copies 'other' into this'; Trie) The destructor that deallocates memory space in the Trie structure. Trie& operator=(Trie const& other) Similar to the copy constructor, it sets the current trie structure identical to 'other'. void insert(char const* const str) Insert a word in 'str into Trie. Ignores the other characters than letters. bool check(char const* const str) const Check if the word in 'str' exists in Trie. char* firstWithPrefix(char const* const str,unsigned depth) const Returns the first word with a given prefix in str. depth is by default 0 and has no specific functionality here. For example, if there are apple' and apply' in the Trie, and the prefix is 'a', 'ap', 'app' or 'appl', you should just return the first one 'apple' according to the alphabetical order. char* Trie::firstWithPrefix(char const* const str, unsigned depth) const{ // You'll need two paths to solve this problem: // Case 1: You've used up all characters in str and are looking for the next stored input. Search depth-first in lexographic (a-z) order, // stopping when you encounter the first endpoint you marked in insert(). // Case 2: You have characters left in the input. Act like check(), // but when you run out of input, proceed to the other case. // If you can't find any matching roots to a chunk of input, return NULL. // If you encounter unrecognized characters in the input, skip them. // Increment depth on every recursion, except unrecognized characters. // For your return value, you can't allocate your cstring in advance // because you do not know how deep in the structure you will have to // search. Instead, increment depth on every (not-unrecognized) recursion. // Then, when you reach an end of word you want to return, allocate your // new char[depth+1] and fill it backward, based on your depth! // Remember to check whether your recursive call gave you back NULL! // TODO: Fix this stub. Trie load_trie(istream& is){ // Given an open input stream, read words on separate lines into a new Trie. // If no lines can be read from the input stream, return a default-constructed // Otherwise, return a Trie filled with all words that can be read until the // first empty line. // Words will be placed on individual lines, like: // apple // application // applied apply // // (The last line there was empty, indicating the end of the dictionary.) // You can read these easily with one of the formats of std::getline(). // Remember, you already skip whitespace in Trie::insert(), so don't worry // about stripping any whitespace here! // TODO: Fix this stub. Trie load_trie(string filename) { // Open an input stream to the file 'filename`, // then pass that input stream to the other form of load_trie(). // Should be easy! return Trie(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Answered: 1 week ago

Answered: 1 week ago