-
Notifications
You must be signed in to change notification settings - Fork 0
/
hippy_node.rb
94 lines (70 loc) · 1.6 KB
/
hippy_node.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class HippyNode
attr_accessor :character, :parent
def initialize(options={})
@character = options[:character]
@parent = options[:parent]
end
def root?
@parent.nil?
end
def word?
@isWord
end
def addArray(array, start)
if start == array.length
unless @isWord
# puts printWord
HippyTree.step_word_count
@isWord = true
end
else
index = array[start]
@children ||= []
@children[index] ||= HippyNode.new(:character => index, :parent => self)
@children[index].addArray(array, start + 1)
end
end
def addString(string, start)
if start == string.length
unless @isWord
HippyTree.step_word_count
@isWord = true
end
else
char = string[start, 1]
index = char.to_i
@children ||= []
@children[index] ||= HippyNode.new(:character => char, :parent => self)
@children[index].addString(string, start + 1)
end
end
def printWord
word = ""
h = @parent
while !h.root?
word << h.character.to_s
h = h.parent
end
puts word
end
def printAllWords
if word?
printWord
end
unless @children.nil?
@children.each do |child|
child.printAllWords unless child.nil?
end
end
end
def include?(board, start)
return word? if start == board.length
return false if @children.nil?
child = @children[board[start]]
if child
child.include?(board, start + 1)
else
false
end
end
end