Class: DS::Trie

Inherits:
Tree
  • Object
show all
Defined in:
lib/ds/trees/trie.rb

Constant Summary collapse

ALPHABET =
%w{- a b c d e f g h i j k l m n o p q r s t u v w x y z}

Instance Attribute Summary

Attributes inherited from Tree

#children, #data

Instance Method Summary collapse

Methods inherited from Tree

#<<, #each, #get_leaves, h, #height, #izometric?, #leaf?, #leaf_count, #levels, #lowest_height, #mirror!, #to_a, #width

Constructor Details

#initialize(value = nil) ⇒ Trie

Returns a new instance of Trie.



10
11
12
13
# File 'lib/ds/trees/trie.rb', line 10

def initialize(value=nil)
  @children = Array.new(alphabet.size)
  @data = value
end

Instance Method Details

#alphabetObject



6
7
8
# File 'lib/ds/trees/trie.rb', line 6

def alphabet
  self.class::ALPHABET
end

#find(s) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/ds/trees/trie.rb', line 21

def find(s)
  letters = s.scan(/./)
  raise ArgumentError, "Not allowed symbol." unless (letters - alphabet).empty?
  priv_find(letters)
end

#insert(s, value = true) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/ds/trees/trie.rb', line 15

def insert(s, value=true)
  letters = s.scan(/./)
  raise ArgumentError, "Not allowed symbol." unless (letters - alphabet).empty?
  priv_insert(letters, value)
end