Class: Boggle::Trie
- Inherits:
-
Containers::Trie
- Object
- Containers::Trie
- Boggle::Trie
- Defined in:
- lib/boggle/trie.rb
Instance Method Summary collapse
-
#match(string) ⇒ Object
returns either nil if there is nothing along that path, true if that path exists in the tree and the word itself if it is an endpoint.
- #match_recursive(node, string, index) ⇒ Object
Instance Method Details
#match(string) ⇒ Object
returns either nil if there is nothing along that path, true if that path exists in the tree and the word itself if it is an endpoint
8 9 10 11 12 |
# File 'lib/boggle/trie.rb', line 8 def match(string) string = string.to_s return nil if string.empty? match_recursive(@root, string, 0) end |
#match_recursive(node, string, index) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/boggle/trie.rb', line 14 def match_recursive(node, string, index) return nil if node.nil? char = string[index] if (char < node.char) match_recursive(node.left, string, index) elsif (char > node.char) match_recursive(node.right, string, index) else return nil if node.nil? if index == (string.length - 1) if node.last? return node.value else return true end end match_recursive(node.mid, string, index+1) end end |