/
left_leaning_redblack_BST.rb
227 lines (201 loc) · 5.42 KB
/
left_leaning_redblack_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
class BST
attr_accessor :root, :answer_keys
def initialize
@root = nil
@answer_keys = []
end
def range_search(start, finish, current=@root)
return if !current || current.val < start || current.val > finish
range_search(start, finish, current.left)
@answer_keys << current.val if current.val >= start && current.val <= finish
range_search(start, finish, current.right)
end
def is_tree?(current=@root)
return true if !current
x = is_tree?(current.left)
return false if !x
if current.left && current.left.val >= current.val
return false
end
if current.right && current.val >= current.right.val
return false
end
is_tree?(current.right)
end
def insertion(val)
current = @root
@root = insert(current, val)
end
def size(node)
return 0 if !node
return node.count
end
def is_red(node)
return false if !node
return node.red
end
def flip_colors(n0, n1, n2)
n1.red = false
n2.red = false
n0.red = true
end
def rotate_left(n1, n2)
n1.right = n2.left
n1.count = size(n1.left) + size(n2.left) + 1
n2.left = n1
n2.count = size(n1) + size(n2.right) + 1
n1.red = n2.red
n2.red = false
return n2
end
def rotate_right(n1, n2)
n1.left = n2.right
n1.count = size(n2.right) + size(n1.right) + 1
n2.right = n1
n2.count = size(n1.left) + size(n1) + 1
n1.red = n2.red
n2.red = false
return n2
end
def insert(current, val)
if !current
return Node.new(val)
end
if val < current.val
current.left = insert(current.left, val)
elsif val > current.val
current.right = insert(current.right, val)
else
current.val = val
end
if is_red(current.right)
current = rotate_left(current, current.right)
end
if is_red(current.left) && is_red(current.left.left)
current = rotate_right(current, current.left)
end
if is_red(current.left) && is_red(current.right)
flip_colors(current, current.left, current.right)
end
current.count = size(current.left) + size(current.right) + 1
return current
end
def find(val)
current = @root
while current
if val < current.val
current = current.left
elsif val > current.val
current = current.right
else
return current
end
end
return nil
end
def rank(val, current=@root)
throw "Not found" if !current
if val === current.val
return 1+ size(current.left)
elsif val < current.val
return rank(val, current.left)
else
return 1 + size(current.left) + rank(val, current.right)
end
end
def index(idx, current=@root)
current_index = size(current.left)
while current
if idx < current_index
current = current.left
current_index = size(current.left)
elsif idx > current_index
current = current.right
current_index = current_index + 1 + size(current.left)
else
return current.val
end
end
return nil
end
def delete_min
current = @root
@root = min_delete(current)
end
def min_delete(current)
return current.right if !current.left
current.left = min_delete(current.left)
return current
end
def delete_val(val)
current = @root
@root = remove(current, val)
end
def get_min(current)
current = current.left until !current.left
return current
end
def remove(current, val)
return nil if !current
if val < current.val
current.left = remove(current.left, val)
elsif val > current.val
current.right = remove(current.right, val)
elsif val == current.val
if !current.left
return current.right
elsif !current.right
return current.left
else
t = current
min = get_min(current.right)
min.right = min_delete(current.right)
min.left = t.left
return min
end
end
return current
end
end
class Node
attr_accessor :val, :right, :left, :count, :red
def initialize(val)
@val = val
@right = nil
@left = nil
@count = 1
@red = true
end
end
tree = BST.new
tree.insertion(1)
tree.insertion(2)
tree.insertion(3)
tree.insertion(4)
tree.insertion(5)
# tree.insertion(6)
# tree.insertion(7)
p tree.root
tree.delete_val(2)
p tree.is_tree?
tree.range_search(2,4)
p tree.answer_keys
wrong_tree = BST.new
wrong_tree.root = Node.new(3)
wrong_tree.root.left = Node.new(1)
wrong_tree.root.right = Node.new(4)
p wrong_tree.is_tree?
# tree.insertion(10)
# tree.insertion(15)
# tree.insertion(3)
# tree.insertion(20)
# tree.insertion(1)
# tree.insertion(-3)
# tree.insertion(2)
# tree.insertion(8)
# tree.insertion(12)
# tree.insertion(16)
# tree.insertion(23)
# p tree.rank(3)
# p tree.index(5)
p tree.root