rambling-trie 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/invalid_trie_operation.rb +4 -0
- data/lib/rambling-trie.rb +1 -0
- data/lib/trie_node.rb +19 -2
- metadata +5 -4
data/lib/rambling-trie.rb
CHANGED
data/lib/trie_node.rb
CHANGED
@@ -2,14 +2,17 @@ module Rambling
|
|
2
2
|
class TrieNode
|
3
3
|
attr_reader :letter, :children
|
4
4
|
|
5
|
-
def initialize(word)
|
5
|
+
def initialize(word, parent = nil)
|
6
6
|
@letter = nil
|
7
|
+
@word = ''
|
8
|
+
@parent = parent
|
7
9
|
@is_terminal = false
|
8
10
|
@children = {}
|
9
11
|
|
10
12
|
unless word.nil?
|
11
13
|
@letter = word.slice!(0)
|
12
14
|
@is_terminal = word.empty?
|
15
|
+
@word = get_parent_letter_string if terminal?
|
13
16
|
add_branch_from(word)
|
14
17
|
end
|
15
18
|
|
@@ -31,6 +34,11 @@ module Rambling
|
|
31
34
|
@children.has_key?(key)
|
32
35
|
end
|
33
36
|
|
37
|
+
def as_word
|
38
|
+
raise InvalidTrieOperation.new() unless @letter.nil? or terminal?
|
39
|
+
@word
|
40
|
+
end
|
41
|
+
|
34
42
|
def add_branch_from(word)
|
35
43
|
unless word.empty?
|
36
44
|
first_letter = word.slice(0)
|
@@ -39,7 +47,7 @@ module Rambling
|
|
39
47
|
word.slice!(0)
|
40
48
|
@children[first_letter].add_branch_from(word)
|
41
49
|
else
|
42
|
-
@children[first_letter] = TrieNode.new(word)
|
50
|
+
@children[first_letter] = TrieNode.new(word, self)
|
43
51
|
end
|
44
52
|
end
|
45
53
|
end
|
@@ -54,6 +62,15 @@ module Rambling
|
|
54
62
|
passes_condition(word) { |node, sliced_word| node.is_word?(sliced_word) }
|
55
63
|
end
|
56
64
|
|
65
|
+
protected
|
66
|
+
def get_parent_letter_string
|
67
|
+
if @parent.nil?
|
68
|
+
@letter or ''
|
69
|
+
else
|
70
|
+
@parent.get_parent_letter_string + @letter
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
57
74
|
private
|
58
75
|
def passes_condition(word, &block)
|
59
76
|
first_letter = word.slice!(0)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rambling-trie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -17,9 +17,10 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- lib/
|
21
|
-
- lib/trie.rb
|
22
|
-
- lib/
|
20
|
+
- ./lib/invalid_trie_operation.rb
|
21
|
+
- ./lib/rambling-trie.rb
|
22
|
+
- ./lib/trie.rb
|
23
|
+
- ./lib/trie_node.rb
|
23
24
|
homepage: http://rubygems.org/gems/rambling-trie
|
24
25
|
licenses: []
|
25
26
|
post_install_message:
|