Skip to content

Instantly share code, notes, and snippets.

@Pet3ris
Created May 21, 2011 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pet3ris/984490 to your computer and use it in GitHub Desktop.
Save Pet3ris/984490 to your computer and use it in GitHub Desktop.
A simple trie
module Spellchecker
class Trie
attr_reader :kids
attr_accessor :terminal
def initialize()
@kids = Hash.new
@terminal = false
end
# Add word to the trie
def add(word)
if word != ""
cur = self
word.downcase.each_char do |character|
cur.kids[character] = Trie.new if not cur.kids.has_key? character
cur = cur.kids[character]
end
cur.terminal = true
end
end
# Check if word is in trie
def has_word?(word)
cur = self
word.each_char do |character|
return false if not cur.kids.has_key? character
cur = cur.kids[character]
end
cur.terminal
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment