Open ghost opened 5 years ago
I found an implementation here. https://www.geeksforgeeks.org/trie-insert-and-search/ what do you think about it ?? @Ercadio
This is low priority. Should be after v0.3
@Mograbi This implementation looks good. But it needs improvement and to follow the requirements listed above
A Patricia Trie is a data structure that is optimal for string autocompletion. Cytopia could greatly benefit from such a data structure. Notably:
Interface
For the Trie, we would like the following API:
::Add(String Key, ValueType value)
: This will add a value node to the Key by traversing down the Trie. If the key already exists, an exception should be thrown.::operator[](String Key)
: This will search for the exact Key and return a reference to the Value. If the Key doesn't exist, it should throw an exception::Find(String key)
: This will return a Cursor object that represents a position in the Trie::begin()
: Returns an iterator object to the first "leaf" Cursor that exists in the subtree.::end()
: Returns an iterator to a Key that doesn't exist (the "null" cursor)::advance(String)
: Should set the Cursor object to the result of adding more characters to the current Cursor To understand better the mentioned API, here is a simple scenario on how we could use it:Implementation
I have a couple of recommendations to implement this.
std::unique_ptr
to represent child nodesstd::allocator
and use that allocator overnew
std::unordered_map
or juststd::array
since characters are integer types. Either way, your key must be a singlechar
. There are advantages and drawbacks for both.size_t
to the first unverified character. For example with the previous tree, the Cursor at "root" would hold "p" with unverified index = 0 because the transition "po" required more than a single characterquery.begin()
was "police" but holds a stack containing the "po" node. If there were more subparents in the query, then the stack would be used the same way we need a stack in Depth-first search