-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.rb
146 lines (114 loc) · 2.93 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require_relative './bst_node'
class BinarySearchTree
attr_reader :root
def initialize
@root = nil
end
def insert(val)
@root = insert_tree_node(@root, val)
end
def find(node, val)
return node if node.nil? || val == node.val
return find(node.left, val) if val < node.val
find(node.right, val)
end
def delete(val)
@root = delete_from_tree(@root, val)
end
# O(n): must check every node (stops at first detected violation).
def valid_bst?(node, min = nil, max = nil)
return true if node.nil?
return false if (min && (min > node.val)) || (max && (max < node.val))
valid_bst?(node.left, min, node.val) && valid_bst?(node.right, node.val, max)
end
private
def insert_tree_node(node, val)
return BSTNode.new(val) unless node
if val <= node.val
node.left = insert_tree_node(node.left, val)
else
node.right = insert_tree_node(node.right, val)
end
node
end
def delete_from_tree(node, val)
return nil if node.nil?
if node.val == val
node = remove(node)
elsif val <= node.val
node.left = delete_from_tree(node.left, val)
else
node.right = delete_from_tree(node.right, val)
end
node
end
def change_parent(node)
replacement_node = maximum(node.left)
promote_child(node.left) if replacement_node.left
replacement_node.left = node.left
replacement_node.right = node.right
replacement_node
end
def maximum(node = @root)
return node if node.right.nil?
maximum(node.right)
end
def promote_child(node)
return node unless node.right
current_parent = node
maximum_node = maximum(node.right)
current_parent.right = maximum_node.left
end
def remove(node)
if node.left.nil? && node.right.nil?
node = nil
elsif node.left && node.right.nil?
node = node.left
elsif node.left.nil? && node.right
node = node.right
else
node = change_parent(node)
end
end
end
if $PROGRAM_NAME == __FILE__
tree = BinarySearchTree.new
tree.insert(5)
tree.insert(2)
tree.insert(7)
tree.insert(9)
tree.insert(1)
tree.insert(8)
tree.insert(3)
tree.insert(10)
tree.insert(13)
tree.insert(6)
# Creates the tree
#
# 5
# / \
# 2 7
# / \ / \
# 1 3 6 9
# / \
# 8 10
# \
# 13
#
found = tree.find(tree.root, 7)
puts "Node: #{found.val}" # 7
puts "\tLeft Child: #{found.left}" # 6
puts "\tRight Child: #{found.right}" # 9
found = tree.find(tree.root, 10)
puts "Node: #{found}" # 10
puts "\tLeft Child: #{found.left}" # nil
puts "\tRight Child: #{found.right}" # 13
tree.delete(10)
found = tree.find(tree.root, 10)
puts "Node: #{found}" # nil
found = tree.find(tree.root, 9)
puts "Node: #{found.right}" # 13
puts "Count: #{tree.root.count}" # 9
tree.insert(6)
puts "Count: #{tree.root.count}" # 10
end