示例#1
0
void Trie::insert(string s) {
    TrieNode* curr = root;
    for(int i=0; i<s.length(); i++){
        if(curr->inChildren(s[i])) curr = curr->getChild(s[i]);
        else curr=curr->addChild(s[i]);
    }
    curr->addChild('#');
}
	// Inserts a word into the trie.
	void insert(string s) 
	{
		if (s.empty())
			return;

		TrieNode* p = root;

		int i = 0;
		while (i < s.size())
		{
			TrieNode* tmp = p->findCh(s[i]);
			if (nullptr == tmp)
			{
				p->addChild(s[i]);
				++i;
				p = p->getLastChild();
			}
			else
			{
				++i;
				p = tmp;
			}
		}

		p->isAWord(true);

		return;
	}
 // Inserts a word into the trie.
 void insert(string s) {
     if (s.size() == 0) return;
     TrieNode* node = root;
     TrieNode* current = NULL;
     int i = 0;
     for (; i < s.size(); i++){
         current = node->dic[s[i] - 'a'];
         if (!current){
             break;
         }
         node = current;
     }
     
     for (; i < s.size(); i++){
         TrieNode* tmp = new TrieNode();
         node->addChild(s[i] - 'a', tmp);
         node = tmp;
     }
     node->isWord = true;
 }