-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_tree.rb
114 lines (90 loc) · 1.79 KB
/
bin_tree.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require_relative './bin_tree_bfs.rb'
module FixNumMax
def maximum(x, y)
return x < y ? y : x
end
end
class BinTree
include BinTreeBreadthFirstSearchHelpers, FixNumMax
attr_accessor :value
attr_reader :left, :right
# @height = 0 # start at 0
# class << self; attr_accessor :height; end;
def initialize(value=nil)
@value = value
@left = nil
@right = nil
end
def insert(v)
if v == @value
return; #return if v already in tree
end
if v < @value
insert_left(v)
else
insert_right(v)
end
self
end
def height
lh = 0
if @left != nil
lh = @left.height
end
rh = 0
if @right != nil
rh = @right.height
end
return 1 + maximum(lh, rh)
end
def include?(v)
if @value == v
return true
elsif v < @value and @left
return @left.include?(v)
elsif v > @value and @right
return @right.include?(v)
end
return false # if we're here the value was not found
end
def bfs(v)
q = Queue.new
q.enqueue(self)
while(!q.empty?)
current_node = q.dequeue
bin_tree = current_node.bin_tree
if bin_tree.value != nil and bin_tree.value == v
return true
else
# value not found in first current_node pulled from q
if bin_tree.left != nil
left = bin_tree.left
q.enqueue(left) # queue up left node
else
end
if bin_tree.right != nil
right = bin_tree.right
q.enqueue(right)
else
end
end
end
# if we're here, the queue is now empty and the search has therefore failed to find the value we wanted
return false
end
private
def insert_left(v)
if self.left
self.left.insert(v)
else
@left = BinTree.new(v)
end
end
def insert_right(v)
if self.right
self.right.insert(v)
else
@right = BinTree.new(v)
end
end
end