Beispiel #1
0
void Dictionary::addWord( const std::string &word )
{
    TrieNode *node = root_;

    int tmp = 0;
    for ( auto it = word.rbegin(); it != word.rend(); ++it )
    {
        char c = *it;
        if ( !isdigit(c) && !isalpha(c) )
        {
            ++tmp;
            continue;
        }
        TrieNode *next_node = node->contain(c); 
        if ( next_node ) 
        {
            node = next_node;    
            continue;
        }
        node = node->addNode(c);
    }
    
    max_length_ = std::max( max_length_, word.size() - tmp );
    if ( node != root_ )
    {
        node->setEndWord(true);
    }
}
Beispiel #2
0
void Trie::insert(const std::string& stringToInsert)
{
	TrieNode* currentNode = this->root.get();
	for(auto stringIt = stringToInsert.begin(); stringIt != stringToInsert.end(); ++stringIt)
	{
		char currentChar = *stringIt;

		bool done = false;
		// 2. if there is a node with value == c, set that node as current node. Repeat.
		for (auto it = currentNode->children.begin(); it != currentNode->children.end() && !done; ++it)
		{
			if ((*it)->value == currentChar)
			{
				currentNode = *it;
				done = true;
			}
		}

		if(done)
			continue;

		// 3. if not, insert a node and go to 2.
		currentNode = currentNode->addNode(currentChar);
	}

	currentNode->hasString = true;
}
Beispiel #3
0
 // Inserts a word into the trie.
 void insert(string word) {
     TrieNode* itr = root;
     for (int i = 0; i < word.length(); ++i) {
         itr = itr->addNode(word[i]);
     }
     if (itr != root) {
         itr->setWordEnd();
     }
 }