-
Notifications
You must be signed in to change notification settings - Fork 132
Iterator over internal node structure (feature request) #20
Comments
To be honest I’m wary of exposing internal representation of the data structure to the customers. Perhaps in the future a compressed representation is desired? Having said that, perhaps this would work:
I’m not sure when I’ll have time to polish this though. |
The internal structure is already accessible (albeit through a private method) via the The only reason I'd like
but is seems uglier than an That being said, something that sets the value of all internal nodes to something that is not the sentinel value, would also work for my purposes. Something like this:
This wouldn't expose the internal structure, and it would allow all prefixes to become visible, thus letting me solve the problem. Not quite sure how the |
On Thu, Oct 12 2017, Jon Crall wrote:
Not quite sure how the `Step` class and `walk_towards` would help me
here. Maybe it would be cleaner than using `trie._get_node`?
I’m imagining:
```
for item in items:
trie[item] = 0
for step in trie.walk_towards(item):
step.set(step.get(0) + 1)
for item in items:
prev = None
for step in trie.walk_towards(item):
if step.value > 1:
break
prev = step
assert prev is not None
unique.append(prev.key)
```
…--
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»
|
Ah yes, the get method would work perfectly and remove the need for me to be accessing internal structure. |
if we could also add a postorder traversal to the mix that would be very nice ! |
Do you mean for the method which goes straight to particular node? Or rather for the |
Provide a walk_towards method which generalises prefixes method and allows walking towards particular node and accessing each step of the path. Compared to prefixes method, steps for nodes without assigned values are returned and the method raises KeyError if trying to walk towards non-existent node. Furthermore, at each step value of a node can be manipulated with set and setdefault methods. Fixes: google#20
Could you test whether mina86@f2df4da fixes this issue? |
It would be nice if there was a builtin way to iterate over the internal nodes in the trie like this:
That would make it much easier to write the Shortest Unique Prefix algorithm:
The text was updated successfully, but these errors were encountered: