Question
Implement a dictionary by using Trie . Requirement: a. Complete trie.h, trie.cpp, and client1.cpp. client1 #include #include trie.h using namespace std; int main() { Trie
Implement a dictionary by using Trie . Requirement: a. Complete trie.h, trie.cpp, and client1.cpp.
client1
#include
using namespace std;
int main() { Trie vocabulary; cout << "Type '0'--quit; '1'--add a word; '2'--search a word; '3'--search prefix: "; int choice; cin >> choice; while(choice) { if(choice == 1) { cout << "Add to the vocabulary this word: "; string word; cin >> word; vocabulary.add(word); } else if(choice == 2) { cout << "Search this word: "; string key; cin >> key; if(vocabulary.contains(key)) cout << key << " exists!" << endl; else cout << key << " does not exists." << endl; } else if(choice == 3) { cout << "Search this prefix: "; string key; cin >> key; if(vocabulary.isPrefix(key)) cout << key << " is a prefix." << endl; else cout << key << " is not a prefix." << endl; } else { cout << "Input incorrect. Try again." << endl; } cout << "Type '0'--quit; '1'--add a word; '2'--search a word; '3'--search prefix: "; cin >> choice; } return 0; }
-------------------------------------------------------------------------------------------------------------------
trie.h
#ifndef TRIE_H #define TRIE_H
#define MAX_CHAR 256
class Node { private: Node *children[MAX_CHAR]; bool bisEnd; public: Node(); bool isEnd(); void insert(string suffix); Node* search(string pat); };
class Trie { private: Node root; public: void add(string word); bool contains(string pat); bool isPrefix(string pat); }; #endif
--------------------------------------------------------------------------------------------
trie.cpp
#include
using namespace std;
/* add your Trie implementation here */
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