-
Notifications
You must be signed in to change notification settings - Fork 0
/
00_tree_node.rb
51 lines (44 loc) · 1 KB
/
00_tree_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
class PolyTreeNode
attr_reader :children, :parent, :value
def initialize(value)
@value = value
@parent = nil
@children = []
end
def parent=(parent)
@parent.children.delete(self) if @parent
@parent = parent
unless parent == nil || parent.children.include?(self)
parent.children << self
end
end
def add_child(child)
child.parent = self
end
def remove_child(child)
raise "That is not my child!" unless @children.include?(child)
child.parent = nil
end
def dfs(target_value)
return self if @value == target_value
return nil if @children.empty?
result_node = nil
@children.each do |child|
result_node = child.dfs(target_value)
break if result_node
end
result_node
end
def bfs(target_value)
queue = [self]
until queue.empty?
node = queue.shift
return node if node.value == target_value
node.children.each {|child| queue << child }
end
nil
end
def inspect
@value.inspect
end
end