Exemplo 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;
}
Exemplo n.º 2
0
 // 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;
 }
Exemplo 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;
		}
Exemplo n.º 4
0
 // 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;
 }
Exemplo n.º 5
0
 // 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');
 }
Exemplo 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;
		}
Exemplo 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;
}
Exemplo 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);
		}