Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please provide assistance to fix the segmentation fault error I am receiving and solve the following problem I am working on: My goal is to
Please provide assistance to fix the segmentation fault error I am receiving and solve the following problem I am working on:
My goal is to build a Trie data structure in C++ that can do the following:
- Capable to insert any given dictionary .txt file filled with a single word per line (i.e. file includes ant, bat, car, desk, etc.) into the Trie. A sample dictionary file that I'm working with can be found at http://txt.do/1pht5
- Prompts the user to input some prefix search and provides an autocomplete suggestion (i.e. file includes bat, bar, bass, and user inputs ba which should suggest bat, bar, bass, etc.)
- Lastly, if the user inputs a prefix search that is not within the Trie, it will recommend the top 3 most similar entries in the Trie (i.e. user prefix input is baq which is not part of the dictionary txt file but bat, bar, bass are which should be suggested)
I have been able to write the following code so far but I'm having difficulties in fixing the segmentation error and making it run.
#include#define alphabet_size 26 using namespace std; class TrieNode { public: TrieNode* children[alphabet_size]; bool endofLeaf; TrieNode(){ for(int i = 0; i < alphabet_size; i++) children[i] = NULL; endofLeaf = false; } }; class Trie { public: TrieNode* root; Trie() {root = new TrieNode();} void Dict_insert(string word){ TrieNode* temp = root; for(int i = 0; i < word.length(); i++){ if(temp->children[word[i]-'a'] == NULL){ temp->children[word[i]-'a'] = new TrieNode(); } temp = temp->children[word[i]-'a']; } temp->endofLeaf = true; } bool search (string word){ TrieNode* tmp = root; for(int i = 0; i < word.size(); i++) { if(tmp->children[word[i]-'a'] == NULL){ return false; } tmp = tmp->children[word[i]-'a']; } return tmp; } void auto_recommendation(string word){ TrieNode* autorec = root; if(autorec->endofLeaf == true) cout << word << endl; for(int i = 0; i < alphabet_size; i++){ if(autorec->children[i] != NULL){ word.push_back((char)i); auto_recommendation(word); word.pop_back(); } } } }; int main(){ Trie* t = new Trie(); fstream dictionaryfile; dictionaryfile.open("Dictionary.txt",ios::in); if (dictionaryfile.is_open()){ string DictEntry; while(getline(dictionaryfile, DictEntry)){ t->Dict_insert(DictEntry); } dictionaryfile.close(); } string searchQuery; cout << "Type in a search: " << endl; getline(cin,searchQuery); cout << "/nDid you mean: " << endl; t->search(searchQuery); if (t->search(searchQuery) == false) /* Call a recommendation search function which recommends the top 3 most similar entries within the Trie*/ cout<<"No suggestions available. "; else t->auto_recommendation(searchQuery); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started