You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 10, 2023. It is now read-only.
Given a Trie, I'd like to be able to search for a key k, and get either k or the closest thing on either side of k.
More precisely, the query would yield one of these:
k and its value, if k is in the trie,
the immediate lexicographical predecessor (or successor) of k if one exists, or
the fact that the trie has no values before (or after) k.
(When I say "lexicographical" I'm talking about the ordering given by the structure of the trie.)
Currently I can see how to implement it by iteratively calling has_node, or iteratively calling iterkeys (or iteritems), building up a larger and larger prefix and maybe handling KeyError exceptions to decide when to go forward. Or it could be implemented naively by traversing over all keys linearly (but that wouldn’t take advantage of the convenient structure of a trie). Finally, I’m half-sure it could be implemented by using traverse, but it’s not obvious how. The traverse function seems to work like foldr on the path to a key—which should be enough, as long as you also implement a "rightmost child" or "leftmost child" query.
All of these options (except the naive one) involve non-trivial work that deals with the structure of the tree, so I think these queries should be handled internally by the class.
Given a Trie, I'd like to be able to search for a key
k, and get either k or the closest thing on either side ofk.More precisely, the query would yield one of these:
kand its value, ifkis in the trie,kif one exists, ork.(When I say "lexicographical" I'm talking about the ordering given by the structure of the trie.)
Currently I can see how to implement it by iteratively calling
has_node, or iteratively callingiterkeys(oriteritems), building up a larger and larger prefix and maybe handling KeyError exceptions to decide when to go forward. Or it could be implemented naively by traversing over all keys linearly (but that wouldn’t take advantage of the convenient structure of a trie). Finally, I’m half-sure it could be implemented by usingtraverse, but it’s not obvious how. Thetraversefunction seems to work likefoldron the path to a key—which should be enough, as long as you also implement a "rightmost child" or "leftmost child" query.All of these options (except the naive one) involve non-trivial work that deals with the structure of the tree, so I think these queries should be handled internally by the class.