Skip to content

Commit e8f175f

Browse files
committed
remove un-necessary code, add tests to improve coverage
1 parent 91de356 commit e8f175f

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

src/red_black_tree.jl

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
# color is true if it's a Red Node, else it's false
55
mutable struct RBTreeNode{K}
66
color::Bool
7-
data::K
87
leftChild::Union{Nothing, RBTreeNode{K}}
98
rightChild::Union{Nothing, RBTreeNode{K}}
109
parent::Union{Nothing, RBTreeNode{K}}
10+
data::K
1111

12-
RBTreeNode{K}() where K = new{K}(true, nothing, nothing, nothing, nothing)
13-
14-
RBTreeNode{K}(d::K) where K = new{K}(true, d, nothing, nothing, nothing)
12+
RBTreeNode{K}() where K = new{K}(true, nothing, nothing, nothing)
13+
RBTreeNode{Any}() = new{Any}(true, nothing, nothing, nothing, nothing)
14+
RBTreeNode{K}(d::K) where K = new{K}(true, nothing, nothing, nothing, d)
1515
end
1616

1717
RBTreeNode() = RBTreeNode{Any}()
@@ -118,9 +118,6 @@ function right_rotate!(tree::RBTree, node_x::RBTreeNode)
118118
node_x.parent = node_y
119119
end
120120

121-
node_color(color::Bool) = (color) ? "RED" : "BLACK"
122-
desc_node(relation, node::RBTreeNode) = println("$relation is ", node.data, " color is ", node_color(node.color))
123-
124121
function fix_insert!(tree::RBTree, node::RBTreeNode)
125122
parent = nothing
126123
grand_parent = nothing
@@ -327,42 +324,4 @@ function Base.delete!(tree::RBTree{K}, d::K) where K
327324
end
328325

329326
!y_original_color && delete_fix(tree, x)
330-
end
331-
332-
function print_tree_inorder(tree::RBTree)
333-
function traverse_tree(node::RBTreeNode)
334-
if (node != tree.Nil)
335-
traverse_tree(node.leftChild)
336-
print(node.data, " ")
337-
traverse_tree(node.rightChild)
338-
end
339-
end
340-
traverse_tree(tree.root)
341-
end
342-
343-
function print_tree(tree::RBTree)
344-
function print_tree_helper(node::RBTreeNode, indent, isright)
345-
if node != tree.Nil
346-
print(indent)
347-
if isright
348-
print("R--")
349-
indent *= " "
350-
else
351-
if (node == tree.root)
352-
print("-->")
353-
indent *= " "
354-
else
355-
print("L--")
356-
indent *= "| "
357-
end
358-
359-
end
360-
println(node.data, " (", node_color(node.color)[1], ")")
361-
print_tree_helper(node.leftChild, indent, false)
362-
print_tree_helper(node.rightChild, indent, true)
363-
end
364-
end
365-
print_tree_helper(tree.root, "", false)
366-
end
367-
368-
Base.show(io::IO, t::RBTree) = print_tree(t)
327+
end

test/test_red_black_tree.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,28 @@ include("../src/red_black_tree.jl")
3333
@test search_key(t, i)
3434
end
3535

36+
t2 = RBTree()
37+
for i = 1:10000
38+
insert!(t2, i)
39+
end
40+
41+
nums = rand(1:10000, 100)
42+
visited = Set{Int}()
43+
for num in nums
44+
if num in visited
45+
@test_throws KeyError delete!(t2, num)
46+
else
47+
delete!(t2, num)
48+
push!(visited, num)
49+
end
50+
end
51+
52+
for i = 1:10000
53+
if i in visited
54+
@test !search_key(t2, i)
55+
else
56+
@test search_key(t2, i)
57+
end
58+
end
59+
3660
end

0 commit comments

Comments
 (0)