I’m trying to write an automatic search completion program for my data structures class. At this step I’m trying to insert the queries from a provided text file named Dictionary.txt. Replit returns segmentation fault(core dumped) every time for me,but when I sent it to my professor for help, he said everything was running fine. I am new to replit and data structures in general, so I apologize for my lack of understanding of the platform
#include <iostream>
#include <fstream>
#include <string>
const int ALPHABET_SIZE = 26;
class Trie {
private:
struct TrieNode {
TrieNode *children[ALPHABET_SIZE];
bool isEndOfWord;
TrieNode() {
for (int i = 0; i < ALPHABET_SIZE; i++) {
children[i] = NULL;
}
isEndOfWord = false;
}
~TrieNode() {
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (children[i] != NULL) {
delete children[i];
}
}
}
};
TrieNode *root;
public:
Trie() {
root = new TrieNode();
}
~Trie() {
delete root;
}
void insert(const std::string &word) {
TrieNode *current = root;
for (char c : word) {
int index = c - 'a';
if (current->children[index] == NULL) {
current->children[index] = new TrieNode();
}
current = current->children[index];
}
current->isEndOfWord = true;
}
bool search(const std::string &word) {
TrieNode *current = root;
for (char c : word) {
int index = c - 'a';
if (current->children[index] == NULL) {
return false;
}
current = current->children[index];
}
return current->isEndOfWord;
}
};
int main() {
Trie trie;
std::ifstream file("Dictionary.txt");
if (file.is_open()) {
std::string word;
while (file >> word) {
trie.insert(word);
}
file.close();
}
std::cout << trie.search("hello") << std::endl;
std::cout << trie.search("world") << std::endl;
std::cout << trie.search("hi") << std::endl;
std::cout << trie.search("hey") << std::endl;
return 0;
}