Question: #include #include using namespace std; class Trie { private: class Node { public: //Variable denoting if this node is the end //character of a string
#include#include using namespace std; class Trie { private: class Node { public: //Variable denoting if this node is the end //character of a string in the Trie bool marked; //An augmentation so that each Node //keeps track of the longest word in //it's subtree. string longest; Node * children[256]; Node() { marked = false; for (int i = 0; i < 256; i++) children[i] = nullptr; } }; Node * root; //Return the number of strings in the Trie //rooted at Node * p. int numWords(Node * p) { if (p == nullptr) { return 0; } else { int total = 0; //count node P if it's marked if (p->marked) total++; //add in words for all subtrees for (int i = 0; i < 256; i++) { total += numWords(p->children[i]); } return total; } } public: Trie() { root = new Node(); }
//Add string s to the Trie void insert(string s) { int i = 0; Node * current = root; while (i < s.length()) { //update length of longest word in node's subtree if (s.length() > current->longest.length()) current->longest = s; //create node if path not already in Trie if (current->children[s[i]] == nullptr) current->children[s[i]] = new Node(); //move pointer down proper path current = current->children[ s[i] ]; i++; } //update length of longest word in node's subtree if (s.length() > current->longest.length()) current->longest = s; //set marked to true to add s to Trie if (current->marked == false) current->marked = true; else cout << "REPEAT WORD INSERTED!!!" << endl; } }; //prints out the height of the trie | int height(node * p) { | |
| if (p == NULL) { | |
| return 0; | |
| } | |
| else | |
| { | |
| //code here | |
| } }
//return number of leaves in the trie |
| int recLeaves(node * p) | |
| { | |
| if (p == NULL) | |
| return 0; | |
| //code here | |
| } |
//display data in trie
| void display(node * p) | |
| { | |
| if(p==NULL) | |
| { | |
| //Nothing! | |
| } | |
| else | |
| { | |
| //code here | |
| } | |
| } |
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
