23
23
24
24
Base.:(== )(x:: RBTreeNode{K} , y:: RBTreeNode{K} ) where K = isequal (x, y)
25
25
26
- Base.:( != )( x:: RBTreeNode{K} , y :: RBTreeNode{K} ) where K = ! isequal (x, y )
26
+ Base. hash ( x:: RBTreeNode ) = hash (x . data, hash (x . color) )
27
27
28
28
function create_null_node (K:: Type )
29
29
node = RBTreeNode {K} ()
33
33
34
34
mutable struct RBTree{K}
35
35
root:: RBTreeNode{K}
36
- Nil :: RBTreeNode{K}
36
+ nil :: RBTreeNode{K}
37
37
38
38
function RBTree {K} () where K
39
39
new {K} (create_null_node (K), create_null_node (K))
42
42
43
43
RBTree () = RBTree {Any} ()
44
44
45
+ """
46
+ search_node(tree, key)
47
+
48
+ Returns the last visited node, while traversing through in binary-search-tree fashion looking for `key`.
49
+ """
50
+ search_node (tree, key)
51
+
45
52
function search_node (tree:: RBTree{K} , d:: K ) where K
46
53
node = tree. root
47
- while node != tree. Nil && d != node. data
54
+ while node != tree. nil && d != node. data
48
55
if d < node. data
49
56
node = node. leftChild
50
57
else
@@ -54,16 +61,28 @@ function search_node(tree::RBTree{K}, d::K) where K
54
61
return node
55
62
end
56
63
64
+ """
65
+ search_key(tree, key)
66
+
67
+ Returns true if `key` is present in the `tree`, else returns false.
68
+ """
69
+ search_key (tree, key)
70
+
57
71
function search_key (tree:: RBTree{K} , d:: K ) where K
58
72
node = search_node (tree, d)
59
73
return (node. data == d)
60
74
end
61
75
76
+ """
77
+ insert_node!(tree::RBTree, node::RBTreeNode)
78
+
79
+ Inserts `node` at proper location by traversing through the `tree` in a binary-search-tree fashion.
80
+ """
62
81
function insert_node! (tree:: RBTree , node:: RBTreeNode )
63
82
node_y = nothing
64
83
node_x = tree. root
65
84
66
- while node_x != tree. Nil
85
+ while node_x != tree. nil
67
86
node_y = node_x
68
87
if node. data < node_x. data
69
88
node_x = node_x. leftChild
@@ -82,10 +101,15 @@ function insert_node!(tree::RBTree, node::RBTreeNode)
82
101
end
83
102
end
84
103
104
+ """
105
+ left_rotate!(tree::RBTree, node_x::RBTreeNode)
106
+
107
+ Performs a left-rotation on `node_x` and updates `tree.root`, if required.
108
+ """
85
109
function left_rotate! (tree:: RBTree , node_x:: RBTreeNode )
86
110
node_y = node_x. rightChild
87
111
node_x. rightChild = node_y. leftChild
88
- if node_y. leftChild != tree. Nil
112
+ if node_y. leftChild != tree. nil
89
113
node_y. leftChild. parent = node_x
90
114
end
91
115
node_y. parent = node_x. parent
@@ -100,10 +124,15 @@ function left_rotate!(tree::RBTree, node_x::RBTreeNode)
100
124
node_x. parent = node_y
101
125
end
102
126
127
+ """
128
+ right_rotate!(tree::RBTree, node_x::RBTreeNode)
129
+
130
+ Performs a right-rotation on `node_x` and updates `tree.root`, if required.
131
+ """
103
132
function right_rotate! (tree:: RBTree , node_x:: RBTreeNode )
104
133
node_y = node_x. leftChild
105
134
node_x. leftChild = node_y. rightChild
106
- if node_y. rightChild != tree. Nil
135
+ if node_y. rightChild != tree. nil
107
136
node_y. rightChild. parent = node_x
108
137
end
109
138
node_y. parent = node_x. parent
@@ -118,6 +147,11 @@ function right_rotate!(tree::RBTree, node_x::RBTreeNode)
118
147
node_x. parent = node_y
119
148
end
120
149
150
+ """
151
+ fix_insert!(tree::RBTree, node::RBTreeNode)
152
+
153
+ This method is called to fix the property of having no two adjacent nodes of red color in the `tree`.
154
+ """
121
155
function fix_insert! (tree:: RBTree , node:: RBTreeNode )
122
156
parent = nothing
123
157
grand_parent = nothing
@@ -128,20 +162,16 @@ function fix_insert!(tree::RBTree, node::RBTreeNode)
128
162
parent = node. parent
129
163
grand_parent = parent. parent
130
164
131
- # parent is the leftChild of grand_parent
132
- if (parent == grand_parent. leftChild)
165
+ if (parent == grand_parent. leftChild) # parent is the leftChild of grand_parent
133
166
uncle = grand_parent. rightChild
134
167
135
- # uncle is red in color
136
- if (uncle. color)
168
+ if (uncle. color) # uncle is red in color
137
169
grand_parent. color = true
138
170
parent. color = false
139
171
uncle. color = false
140
172
node = grand_parent
141
- # uncle is black in color
142
- else
143
- # node is rightChild of it's parent
144
- if (node == parent. rightChild)
173
+ else # uncle is black in color
174
+ if (node == parent. rightChild) # node is rightChild of it's parent
145
175
node = parent
146
176
left_rotate! (tree, node)
147
177
end
@@ -150,20 +180,16 @@ function fix_insert!(tree::RBTree, node::RBTreeNode)
150
180
node. parent. parent. color = true
151
181
right_rotate! (tree, node. parent. parent)
152
182
end
153
- # parent is the rightChild of grand_parent
154
- else
183
+ else # parent is the rightChild of grand_parent
155
184
uncle = grand_parent. leftChild
156
185
157
- # uncle is red in color
158
- if (uncle. color)
186
+ if (uncle. color) # uncle is red in color
159
187
grand_parent. color = true
160
188
parent. color = false
161
189
uncle. color = false
162
190
node = grand_parent
163
- # uncle is black in color
164
- else
165
- # node is leftChild of it's parent
166
- if (node == parent. leftChild)
191
+ else # uncle is black in color
192
+ if (node == parent. leftChild) # node is leftChild of it's parent
167
193
node = parent
168
194
right_rotate! (tree, node)
169
195
end
@@ -177,14 +203,20 @@ function fix_insert!(tree::RBTree, node::RBTreeNode)
177
203
tree. root. color = false
178
204
end
179
205
206
+ """
207
+ insert!(tree, key)
208
+
209
+ Inserts `key` in the `tree` if it is not present.
210
+ """
211
+ insert! (tree, key)
180
212
181
213
function Base. insert! (tree:: RBTree{K} , d:: K ) where K
182
214
# if the key exists in the tree, no need to insert
183
215
search_key (tree, d) && return tree
184
216
# search_key(tree, d) && return tree
185
217
# insert, if not present in the tree
186
218
node = RBTreeNode {K} (d)
187
- node. leftChild = node. rightChild = tree. Nil
219
+ node. leftChild = node. rightChild = tree. nil
188
220
189
221
insert_node! (tree, node)
190
222
@@ -198,6 +230,11 @@ function Base.insert!(tree::RBTree{K}, d::K) where K
198
230
return tree
199
231
end
200
232
233
+ """
234
+ delete_fix(tree::RBTree, node::Union{RBTreeNode, Nothing})
235
+
236
+ This method is called when a black node is deleted because it violates the black depth property of the RBTree.
237
+ """
201
238
function delete_fix (tree:: RBTree , node:: Union{RBTreeNode, Nothing} )
202
239
while node != tree. root && ! node. color
203
240
if node == node. parent. leftChild
@@ -255,10 +292,14 @@ function delete_fix(tree::RBTree, node::Union{RBTreeNode, Nothing})
255
292
end
256
293
end
257
294
node. color = false
258
- return tree
295
+ return nothing
259
296
end
260
297
261
- # transplant u in the tree by v
298
+ """
299
+ rb_transplant(tree::RBTree, u::Union{RBTreeNode, Nothing}, v::Union{RBTreeNode, Nothing})
300
+
301
+ Replaces `u` by `v` in the `tree` and updates the `tree` accordingly.
302
+ """
262
303
function rb_transplant (tree:: RBTree , u:: Union{RBTreeNode, Nothing} , v:: Union{RBTreeNode, Nothing} )
263
304
if u. parent == nothing
264
305
tree. root = v
@@ -270,18 +311,30 @@ function rb_transplant(tree::RBTree, u::Union{RBTreeNode, Nothing}, v::Union{RBT
270
311
v. parent = u. parent
271
312
end
272
313
314
+ """
315
+ minimum_node(tree::RBTree, node::RBTreeNode)
316
+
317
+ Returns the RBTreeNode with minimum value in subtree of `node`.
318
+ """
273
319
function minimum_node (tree:: RBTree , node:: RBTreeNode )
274
- while node. leftChild != tree. Nil
320
+ while node. leftChild != tree. nil
275
321
node = node. leftChild
276
322
end
277
323
return node
278
324
end
279
325
326
+ """
327
+ delete!(tree::RBTree, key)
328
+
329
+ Deletes `key` from `tree`, if present, else throws a KeyError.
330
+ """
331
+ delete! (tree, key)
332
+
280
333
function Base. delete! (tree:: RBTree{K} , d:: K ) where K
281
- z = tree. Nil
334
+ z = tree. nil
282
335
node = tree. root
283
336
284
- while node != tree. Nil
337
+ while node != tree. nil
285
338
if node. data == d
286
339
z = node
287
340
end
@@ -293,15 +346,15 @@ function Base.delete!(tree::RBTree{K}, d::K) where K
293
346
end
294
347
end
295
348
296
- (z == tree. Nil ) && throw (KeyError (d))
349
+ (z == tree. nil ) && throw (KeyError (d))
297
350
298
351
y = z
299
352
y_original_color = y. color
300
353
x = RBTreeNode {K} ()
301
- if z. leftChild == tree. Nil
354
+ if z. leftChild == tree. nil
302
355
x = z. rightChild
303
356
rb_transplant (tree, z, z. rightChild)
304
- elseif z. rightChild == tree. Nil
357
+ elseif z. rightChild == tree. nil
305
358
x = z. leftChild
306
359
rb_transplant (tree, z, z. leftChild)
307
360
else
0 commit comments