Esempio n. 1
0
void naiveTest() {
	TrieNode* trie = new TrieNode();
	trie->insert("HELLO",1);
	assert(1 == trie->find("HELLO")->terminal);
	assert(NULL == trie->find("HELLOB"));
	assert(-1 == trie->find("HELL")->terminal);
	delete trie;
}
 // Returns if the word is in the trie.
 bool search(string word) {
     TrieNode *cur = root, *tmp;
     for (auto c: word) {
         if ((tmp = cur->find(c)) == NULL)
             return false;
         cur = tmp;
     }
     return cur->find('\0') != NULL;
 }
Esempio n. 3
0
		// Returns if the word is in the trie.
		bool search(string word) {
			TrieNode* node = root;
			for(int i = 0; i < word.length(); i++)
			{
				TrieNode* nextNode = node->find(word[i]);
				if(!nextNode)
					return false;
				node = nextNode;
			}

			TrieNode* finalNode = node->find('\0');
			if(finalNode)
				return true;
			else
				return false;
		}
 // Returns if there is any word in the trie
 // that starts with the given prefix.
 bool startsWith(string prefix) {
     TrieNode *cur = root, *tmp;
     for (auto c: prefix) {
         if ((tmp = cur->find(c)) == NULL)
             return false;
         cur = tmp;
     }
     return true;
 }
 // Inserts a word into the trie.
 void insert(string word) {
     TrieNode *cur = root, *tmp;
     for (auto c: word) {
         if ((tmp = cur->find(c)) == NULL) {
             tmp = cur->insert(c);
         }
         cur = tmp;
     }
     cur->insert('\0');
 }
Esempio n. 6
0
		// Returns if there is any word in the trie
		// that starts with the given prefix.
		bool startsWith(string prefix) {
			TrieNode* node = root;
			for(int i = 0; i < prefix.length(); i++)
			{
				TrieNode* nextNode = node->find(prefix[i]);
				if(!nextNode)
					return false;
				node = nextNode;
			}
			return true;
		}
Esempio n. 7
0
int main() {
  TrieNode* ptn = new TrieNode();
  std::string s = "BANANAS";
  for (int i = 0; i < s.size(); ++i) {
    // printf("%s\n", s.substr(i).c_str());
    ptn->insert(s.substr(i).c_str());
  }

  TrieNode* ptn0  = ptn->find("NAS");
  if (ptn0)
    printf("%s is in", "NAS");
  delete ptn;
}
Esempio n. 8
0
		// Inserts a word into the trie.
		void insert(string word) {
			TrieNode* node = root;
			for(int i = 0; i < word.length(); i++)
			{
				TrieNode* nextNode = node->find(word[i]);

				if(!nextNode)
				{
					TrieNode* newNode =  new TrieNode();
					newNode->letter = word[i];
					node->next.push_back(newNode);
					nextNode = newNode;
				}

				node = nextNode;
			}

			TrieNode* newNode =  new TrieNode();
			newNode->letter = '\0';
			node->next.push_back(newNode);
		}