-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbst.rb
104 lines (85 loc) · 1.92 KB
/
bst.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
require_relative '../trees/tree_node'
class BST
attr_reader :root
def initialize(values = [])
values.each { |value| insert(value) } if values.any?
end
def print
puts inorder(@root).to_s
end
def insert(value, node = @root)
if node.nil?
node = TreeNode.new(value)
@root = node if @root.nil?
return node
end
if value < node.val
node.left = insert(value, node.left)
elsif value > node.val
node.right = insert(value, node.right)
end
node
end
def search(val, node = @root)
return false if node.nil?
case val <=> node.val
when 1 then search(val, node.right)
when -1 then search(val, node.left)
when 0 then true
end
end
def inorder(node, output = [])
return [] if node.nil?
inorder(node.left, output)
output << node.val
inorder(node.right, output)
output
end
def min(node = @root)
return nil if node.nil?
current = node
while current
min = current.val
current = current.left
end
min
end
def max(node = @root)
return nil if node.nil?
current = node
while current
max = current.val
current = current.right
end
max
end
def delete(val, node = @root)
return if node.nil?
if val < node.val
node.left = delete(val, node.left)
elsif val > node.val
node.right = delete(val, node.right)
elsif node.left.nil? && node.right.nil?
# leaf node
return nil
elsif node.left.nil? # only left child
return node.right
elsif node.right.nil? # only right child
return node.left
else
# both left and right subtree
replacement = min(node.right)
node.val = replacement
node.right = delete(replacement, node.right)
return node
end
node
end
end
bst = BST.new([10, 2, 33, 4, 5, 3, 42])
bst.print
# puts bst.search(4)
# puts bst.min
# puts bst.max
puts bst.delete(4)
puts bst.print