Exemple #1
0
	// Returns if the word is in the trie.
	bool search(string word) {
		TrieNode * cur = root;
		for (int i = 0; i < word.length(); i++)
		{
			cur = cur->next_node(word[i]);
			if (cur == NULL)
				return false;
		}
		return cur->isend;
	}
Exemple #2
0
	// Returns if there is any word in the trie
	// that starts with the given prefix.
	bool startsWith(string prefix) {
		TrieNode * cur = root;
		for (int i = 0; i < prefix.length(); i++)
		{
			cur = cur->next_node(prefix[i]);
			if (cur == NULL)
				return false;
		}
		return true;
	}
Exemple #3
0
	// Inserts a word into the trie.
	void insert(string word) {
		TrieNode * cur = root;

		for (int i = 0; i < word.length(); i++)
		{
			TrieNode * post = cur->next_node(word[i]);
			if (post != NULL)
				cur = post;
			else
			{
				TrieNode* new_node = new TrieNode(word[i]);
				cur->child.push_back(new_node);
				cur = new_node;
			}
		}

		cur->isend = true;
	}