Class: PrefixTree
- Inherits:
-
Object
- Object
- PrefixTree
- Defined in:
- lib/pretrie.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
Returns the value of attribute container.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #add(word) ⇒ Object
- #delete(word) ⇒ Object
- #find(prefix) ⇒ Object
- #include?(prefix) ⇒ Boolean
-
#initialize ⇒ PrefixTree
constructor
A new instance of PrefixTree.
- #list(prefix = nil) ⇒ Object
- #read_all_from_csv ⇒ Object
- #save_all_to_csv ⇒ Object
Constructor Details
#initialize ⇒ PrefixTree
10 11 12 13 14 |
# File 'lib/pretrie.rb', line 10 def initialize @root = Node.new @container = [] @csv_container = FileWriter.new end |
Instance Attribute Details
#container ⇒ Object
Returns the value of attribute container.
8 9 10 |
# File 'lib/pretrie.rb', line 8 def container @container end |
#root ⇒ Object
Returns the value of attribute root.
8 9 10 |
# File 'lib/pretrie.rb', line 8 def root @root end |
Instance Method Details
#add(word) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/pretrie.rb', line 16 def add(word) container << word current = root word.each_char do |char| current = adder_logic(current, char) end current.is_word = true current end |
#delete(word) ⇒ Object
49 50 51 52 |
# File 'lib/pretrie.rb', line 49 def delete(word) container.delete(word) delete_recursively(word, 0, root) end |
#find(prefix) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/pretrie.rb', line 35 def find(prefix) current = root prefix.each_char do |char| return false unless current.data.include?(char) current = current.data[char] end current.is_word end |
#include?(prefix) ⇒ Boolean
26 27 28 29 30 31 32 33 |
# File 'lib/pretrie.rb', line 26 def include?(prefix) current = root prefix.each_char do |char| return false unless current.data.include?(char) current = current.data[char] end true end |
#list(prefix = nil) ⇒ Object
44 45 46 47 |
# File 'lib/pretrie.rb', line 44 def list(prefix = nil) return container if prefix.nil? verified_words(container, prefix) end |
#read_all_from_csv ⇒ Object
58 59 60 |
# File 'lib/pretrie.rb', line 58 def read_all_from_csv @csv_container.read_from_csv end |
#save_all_to_csv ⇒ Object
54 55 56 |
# File 'lib/pretrie.rb', line 54 def save_all_to_csv @csv_container.save_to_csv(container) end |