looking for some solutions? You are welcome.

SOLVED: recursively search for the values associated with a given string in Trie C++

JerkJep12:

I am currently trying to search for the values associated with a given string in Trie. For example, let consider this trie enter image description here

here are my code so far:

template<typename ValueType>
std::vector<ValueType>Trie<ValueType>::find(const std::string& key, bool exactMatchOnly) const{
if(key.length() == 0){
    return v;
}
if(exactMatchOnly == true){
    if(children[key.at(0)] != NULL)
        return (children[key.at(0)])->find(key.substr(1), exactMatchOnly);
}
else{
    ????????
}
return std::vector<ValueType>();

}

If I want to search for "hit" with an exactMatchOnly of true, it would return {1,2} or {2,1}

std::vector<int> result1 = trie.find("hit", true); // returns {1, 2} or {2, 1}

but, if i would like to search for “hit” with an exactMatchOnly of false, it would return values of {1, 2, 10, 20, 7, 8, 9}.

std::vector<int> result2 = trie.find("hit", false); // returns {1, 2, 10, 20, 7, 8, 9}

The reason is that the {1, 2} are associated with the exact match, “hit”, the {10, 20} are associated with the SNiP of “hip”, and the {7, 8, 9} are associated with the SNiP of “hat”. (If instead of {7, 8, 9} “hat” were associated with {10, 2, 5, 10}, then the resulting vector would be {1, 2, 10, 20, 10, 2, 5, 10} in any order.

So my problem is that how can I search for all values if I pass "false" for exactMatchOnly?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent