Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Solve the Longest Prefix Matching problem: Given a dictionary of words and an in

ID: 3841477 • Letter: S

Question

Solve the Longest Prefix Matching problem: Given a dictionary of words and an input string, find the longest prefix of the string which is also a word in dictionary. For example, assume the dictionary contains words “he” and “hello”, then the longest prefix of “hello” is “he”, and the longest prefix of “he” is empty (or an empty string). (this question is for extra bonus only)

Requirement:

a. You must use the Trie-based dictionary you created above for Question-18 to solve the problem in Question-19.

b. You must use Trie to solve this problem. Otherwise, you will not get points.

c. You can improve or simply add additional functions to your Trie implementation above to solve this problem.

d. Make you changes in the same trie.h and trie.cpp files. Simply improve your code directly in trie.h and trie.cpp.

e. Improve trie.h and trie.cpp. Compile trie.h, trie.cpp, and client2.cpp.

//client2.cpp

#include
#include "trie.h"

using namespace std;

int main()
{
Trie vocabulary;
cout << "Type a word ('q' for quit): ";
string word;
cin >> word;
while(word != "q") {
       vocabulary.add(word);
cout << "The longest prefix is: " << vocabulary.longestPrefix(word) << endl;
cout << "Type a word ('q' for quit): ";
cin >> word;
}
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
#include "trie.h"

using namespace std;

/* add your Trie implementation here */

For question 18:

Implement a dictionary by using Trie.

Requirement:

a. Complete trie.h, trie.cpp, and client1.cpp.

b. Include appropriate comments in your code

trie.h and trie.cpp is shown above and client1.cpp is shown below. (I also posted question18 in questionaire since asking only one question is allowed but in case you wonder, here it is)

//client1.cpp

#include
#include "trie.h"

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;
}

Explanation / Answer

Step-1:

The initial step is to create the structure for the trie.h file:-

Trie.h Program:-

// This is the header files in C++ for embedding the methods and properties of each class

#include <algorithm>
#include <iostream>
#include <iterator>

// Created a dictionary for keeping all the words logs data

void trieDictionaryMet(DictionaryImpl& trieDic)
{
trieDic.Add("hyperbranching");
trieDic.Add("hyperactivate");
   trieDic.Add("audiobook");
   trieDic.Add("democracy");
}

void tricViewPrefixWords(std::ostream& tricOutStream,
const std::string& tricPrefix,
const std::string& tricWords,
const std::vector<std::string>& tricResultView)
{
tricOutStream << tricPrefix << tricWords << std::endl;
std::copy(tricResultView.begin(),
tricResultView.end(),
std::ostream_iterator<std::string>(tricOutStream, " "));
}

Step-2:

The next step is to define the trie.cpp program which contains all the main() functionality.

Trie.Cpp Program:-

// This is the main() function

int _main(int argc, _tchar* argv[])
{
DictionaryImpl trieDic;

trieDictionaryMet(trieDic);

std::cout << "We welocme you to Tric Based Dictionary" << std::endl;
std::string tricInputWord;
while (true) {
std::cin >> tricInputWord;
if (*tricInputWord.rbegin() == '?') {
std::string tricWords(tricInputWord.begin(), tricInputWord.end() - 1);
std::vector<std::string> tricResultView;
if (tricWords.empty()) {
trieDic.RetrieveAll(tricResultView);
tricViewPrefixWords(std::cout, "Kindly print all the words at a go:", "", tricResultView);
} else {
trieDic.Query(tricWords, tricResultView);
if (tricResultView.empty()) {
std::cout << tricWords << " There is no data for the given input" << std::endl;
} else {
tricViewPrefixWords(std::cout, "There is successfully matched the given input word: ", tricWords, tricResultView);
}
}
}
else {
std::vector<std::string> tricResultView;
trieDic.RetrieveAll(tricResultView);
tricViewPrefixWords(std::cout, "The words before adding is:", tricInputWord, tricResultView);
trieDic.Add(tricInputWord);
trieDic.RetrieveAll(tricResultView);
tricViewPrefixWords(std::cout, "The words after adding is: ", tricInputWord, tricResultView);
}
}

return 0;
}

Step-3:

The next step is to create a Client2.cpp file which checks the given input word into a matching substance and provides the output for the given input.

Client2.Cpp Program:-


// This is the header files in C++ for embedding the methods and properties of each class

#include "trie.h"
#include <stack>
#include <stdlib.h>
#include <string.h>

using namespace std;

// The main() method is the starting point of the program

int main()
{
  
// Declare the different variables
  
string checkInputDict;
string DictionaryTrie;
{
if (!checkInputDict) {
*checkInputDict = static_cast<DictionaryTrie*> (malloc(sizeof(checkInputDict)));
(*checkInputDict)->str = 0;
      
      
   // The for loop uses the for different input value
      
      
for (int trieObj = 0; trieObj < The No of input values is; ++ trieObj) {
(*checkInputDict)->children[trieObj] = 0;
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote