1
1
# it has unique keys
2
2
# leftChild has keys which are less than the node
3
3
# rightChild has keys which are greater than the node
4
- # color is true if it's a Red Node, else it's false
4
+ # color is true if its a Red Node, else its false
5
5
mutable struct RBTreeNode{K}
6
6
color:: Bool
7
7
data:: Union{K, Nothing}
17
17
RBTreeNode () = RBTreeNode {Any} ()
18
18
RBTreeNode (d) = RBTreeNode {Any} (d)
19
19
20
- function Base. isequal (x:: RBTreeNode{K} , y:: RBTreeNode{K} )where K
21
- return (x. color == y. color && x. data == y. data)
22
- end
23
-
24
- Base.:(== )(x:: RBTreeNode{K} , y:: RBTreeNode{K} ) where K = isequal (x, y)
25
-
26
- Base. hash (x:: RBTreeNode ) = hash (x. data, hash (x. color))
27
-
28
20
function create_null_node (K:: Type )
29
21
node = RBTreeNode {K} ()
30
22
node. color = false
34
26
mutable struct RBTree{K}
35
27
root:: RBTreeNode{K}
36
28
nil:: RBTreeNode{K}
29
+ count:: Int
37
30
38
31
function RBTree {K} () where K
39
- new {K} (create_null_node (K), create_null_node (K))
32
+ rb = new ()
33
+ rb. nil = create_null_node (K)
34
+ rb. root = Ref (rb. nil)[]
35
+ rb. count = 0
36
+ return rb
40
37
end
41
38
end
42
39
43
40
RBTree () = RBTree {Any} ()
44
41
42
+ Base. length (tree:: RBTree ) = tree. count
43
+
45
44
"""
46
45
search_node(tree, key)
47
46
@@ -62,13 +61,13 @@ function search_node(tree::RBTree{K}, d::K) where K
62
61
end
63
62
64
63
"""
65
- search_key (tree, key)
64
+ haskey (tree, key)
66
65
67
66
Returns true if `key` is present in the `tree`, else returns false.
68
67
"""
69
- search_key (tree, key)
68
+ haskey (tree, key)
70
69
71
- function search_key (tree:: RBTree{K} , d:: K ) where K
70
+ function Base . haskey (tree:: RBTree{K} , d:: K ) where K
72
71
node = search_node (tree, d)
73
72
return (node. data == d)
74
73
end
@@ -171,11 +170,11 @@ function fix_insert!(tree::RBTree, node::RBTreeNode)
171
170
uncle. color = false
172
171
node = grand_parent
173
172
else # uncle is black in color
174
- if (node == parent. rightChild) # node is rightChild of it's parent
173
+ if (node == parent. rightChild) # node is rightChild of its parent
175
174
node = parent
176
175
left_rotate! (tree, node)
177
176
end
178
- # node is leftChild of it's parent
177
+ # node is leftChild of its parent
179
178
node. parent. color = false
180
179
node. parent. parent. color = true
181
180
right_rotate! (tree, node. parent. parent)
@@ -189,11 +188,11 @@ function fix_insert!(tree::RBTree, node::RBTreeNode)
189
188
uncle. color = false
190
189
node = grand_parent
191
190
else # uncle is black in color
192
- if (node == parent. leftChild) # node is leftChild of it's parent
191
+ if (node == parent. leftChild) # node is leftChild of its parent
193
192
node = parent
194
193
right_rotate! (tree, node)
195
194
end
196
- # node is rightChild of it's parent
195
+ # node is rightChild of its parent
197
196
node. parent. color = false
198
197
node. parent. parent. color = true
199
198
left_rotate! (tree, node. parent. parent)
@@ -212,8 +211,8 @@ insert!(tree, key)
212
211
213
212
function Base. insert! (tree:: RBTree{K} , d:: K ) where K
214
213
# if the key exists in the tree, no need to insert
215
- search_key (tree, d) && return tree
216
- # search_key(tree, d) && return tree
214
+ haskey (tree, d) && return tree
215
+
217
216
# insert, if not present in the tree
218
217
node = RBTreeNode {K} (d)
219
218
node. leftChild = node. rightChild = tree. nil
@@ -227,9 +226,20 @@ function Base.insert!(tree::RBTree{K}, d::K) where K
227
226
else
228
227
fix_insert! (tree, node)
229
228
end
229
+ tree. count += 1
230
230
return tree
231
231
end
232
232
233
+ """
234
+ insert!(tree, key)
235
+
236
+ Inserts `key` in the `tree` if it is not present.
237
+ """
238
+ function Base. push! (tree:: RBTree{K} , key0) where K
239
+ key = convert (K, key0)
240
+ insert! (tree, key)
241
+ end
242
+
233
243
"""
234
244
delete_fix(tree::RBTree, node::Union{RBTreeNode, Nothing})
235
245
317
327
Returns the RBTreeNode with minimum value in subtree of `node`.
318
328
"""
319
329
function minimum_node (tree:: RBTree , node:: RBTreeNode )
330
+ (node == tree. nil) && return node
320
331
while node. leftChild != tree. nil
321
332
node = node. leftChild
322
333
end
326
337
"""
327
338
delete!(tree::RBTree, key)
328
339
329
- Deletes `key` from `tree`, if present, else throws a KeyError .
340
+ Deletes `key` from `tree`, if present, else returns the unmodified tree .
330
341
"""
331
342
delete! (tree, key)
332
343
@@ -346,7 +357,7 @@ function Base.delete!(tree::RBTree{K}, d::K) where K
346
357
end
347
358
end
348
359
349
- (z == tree. nil) && throw ( KeyError (d))
360
+ (z == tree. nil) && return tree
350
361
351
362
y = z
352
363
y_original_color = y. color
@@ -377,4 +388,30 @@ function Base.delete!(tree::RBTree{K}, d::K) where K
377
388
end
378
389
379
390
! y_original_color && delete_fix (tree, x)
380
- end
391
+ tree. count -= 1
392
+ return tree
393
+ end
394
+
395
+ Base. in (key, tree:: RBTree ) = haskey (tree, key)
396
+
397
+ """
398
+ getindex(tree, ind)
399
+
400
+ Gets the key present at index `ind` of the tree. Indexing is done in increasing order of key.
401
+ """
402
+ getindex (tree, ind)
403
+
404
+ function Base. getindex (tree:: RBTree{K} , ind) where K
405
+ @boundscheck (1 <= ind <= tree. count) || throw (ArgumentError (" $ind should be in between 1 and $(tree. count) " ))
406
+ function traverse_tree_inorder (node:: RBTreeNode{K} ) where K
407
+ if (node != tree. nil)
408
+ left = traverse_tree_inorder (node. leftChild)
409
+ right = traverse_tree_inorder (node. rightChild)
410
+ append! (push! (left, node. data), right)
411
+ else
412
+ return K[]
413
+ end
414
+ end
415
+ arr = traverse_tree_inorder (tree. root)
416
+ return @inbounds arr[ind]
417
+ end
0 commit comments