Question

Concerning Trie from Wikipedia:

[Compared to HashTable] Tries support ordered iteration

I am not sure what is meant here first of all. Is it the same as sorted iteration?
Additionally is this supposed to be an inherent feature of this datastructure?
I mean if one uses e.g. a HashSet for the children of each node in a Trie we can get O(1) access trying to find the children to branch or by using a LinkedList you save space on the nodes.
Perhaps I am way wrong but from my point of view the only way to support ordered iteration would be to keep array of all keys per node even unused.
Isn't this approach bad?
And one last point:
If the ordered here is related to insertion order (and not sorted) how would we get that since we insert each word (using the chars as keys) to the corresponding node but I can't see how this gives us information on the insertion order?
Could someone help me clear these things in my mind out?
Thank you.

Was it helpful?

Solution

What they mean is that a depth-first search of the trie will yield a lexicographically ordered output of the strings in the trie.

But yes, you are right, this assumes that all sibling nodes on a given level are visited in lexicographical order and this is far from given, especially with large alphabets, where it makes sense to implement the child node table via a hash table.

In summary, I think your doubts are justified and that the Wikipedia article is wrong.

But it’s worth noting that a lexicographically ordered iteration over a trie is affordable even if the child nodes aren’t ordered, since ordering them in while iterating is expected to be relatively cheap – there will be few child nodes in each trie node so the overall performance of iterating the trie will still be in O(n) expected time – in contrast with a hash table, where ordered iteration effectively implies sorting all elements, an O(n log n) operation.

OTHER TIPS

It means sorted. You can't get the insertion order out a trie without extra effort. Not exactly sure what you mean by inherent feature. The implementation needs to provide it (or provide access to the internals), but it is straight-forward to do so.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top