Skip to content

Allow iteration over keys with #each (and include Enumerable) #19

Description

@roryokane

I would like to be able to iterate over all keys in a Trie, or all keys that start with a certain prefix (keys that are under a certain TrieNode.) Right now, there is no way to tell what keys are in the trie without trying to access every possible letter from every possible trie node.

You could support this by implementing #each, which should enumerate all objects in the trie, in order. Implementing #each will also allow you to include Enumerable, which provides many convenient methods.

Without this functionality, I cannot use this library to implement the word game GHOST. With my current array implementation, I print a random sample of words that start with a certain prefix:

starting_word_offset = Kernel::rand(wordlist.length - NUM_OF_WORDS_TO_SHOW)
wordlist_subset = wordlist[starting_word_offset, NUM_OF_WORDS_TO_SHOW]

And I also use Enumerable methods to filter the words in the trie:

return wordlist.reject { |word| word.length < MINIMUM_WORD_LENGTH }

If providing this functionality would make Trie operations unacceptably slower, you can provide two trie classes with your library: a SimpleTrie and a Trie, where a Trie provides more methods, but is slightly slower.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions