Skip to content

Commit b2002b6

Browse files
committed
address review, fix tests
1 parent fefb22f commit b2002b6

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

src/splay_tree.jl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ mutable struct SplayTreeNode{K}
88
SplayTreeNode{K}(d::K) where K = new{K}(nothing, nothing, nothing, d)
99
end
1010

11-
SplayTreeNode_or_null{K} = Union{SplayTreeNode{K}, Nothing}
12-
1311
SplayTreeNode(d) = SplayTreeNode{Any}(d)
1412
SplayTreeNode() = SplayTreeNode{Any}()
1513

16-
1714
mutable struct SplayTree{K}
18-
root::SplayTreeNode_or_null{K}
15+
root::Union{SplayTreeNode{K}, Nothing}
1916
count::Int
2017

2118
SplayTree{K}() where K = new{K}(nothing, 0)
@@ -64,13 +61,12 @@ function right_rotate!(tree::SplayTree, node_x::SplayTreeNode)
6461
node_x.parent = node_y
6562
end
6663

67-
64+
# The splaying operation moves node_x to the root of the tree using the series of rotations.
6865
function splay!(tree::SplayTree, node_x::SplayTreeNode)
69-
while !isa(node_x.parent, Nothing)
66+
while node_x.parent !== nothing
7067
parent = node_x.parent
7168
grand_parent = node_x.parent.parent
72-
# grand-parent is Null
73-
if isa(grand_parent, Nothing)
69+
if grand_parent === nothing
7470
# single rotation
7571
if node_x == parent.leftChild
7672
# zig rotation
@@ -100,18 +96,23 @@ function splay!(tree::SplayTree, node_x::SplayTreeNode)
10096
end
10197
end
10298

103-
function maximum_node(node::SplayTreeNode_or_null)
99+
function maximum_node(node::Union{SplayTreeNode, Nothing})
104100
(node == nothing) && return node
105101
while node.rightChild != nothing
106102
node = node.rightChild
107103
end
108104
return node
109105
end
110106

111-
function _join(tree::SplayTree ,s::SplayTreeNode_or_null, t::SplayTreeNode_or_null)
112-
if isa(s, Nothing)
107+
# Join operations joins two trees S and T
108+
# All the items in S are smaller than the items in T.
109+
# This is a two-step process.
110+
# In the first step, splay the largest node in S. This moves the largest node to the root node.
111+
# In the second step, set the right child of the new root of S to T.
112+
function _join(tree::SplayTree, s::Union{SplayTreeNode, Nothing}, t::Union{SplayTreeNode, Nothing})
113+
if s === nothing
113114
return t
114-
elseif isa(t, Nothing)
115+
elseif t === nothing
115116
return s
116117
else
117118
x = maximum_node(s)
@@ -136,40 +137,39 @@ function search_node(tree::SplayTree{K}, d::K) where K
136137
return (node == nothing) ? prev : node
137138
end
138139

139-
function haskey(tree::SplayTree{K}, d::K) where K
140+
function Base.haskey(tree::SplayTree{K}, d::K) where K
140141
node = tree.root
141-
if isa(node, Nothing)
142+
if node === nothing
142143
return false
143144
else
144145
node = search_node(tree, d)
145-
isa(node, Nothing) && return false
146+
(node === nothing) && return false
146147
is_found = (node.data == d)
147148
is_found && splay!(tree, node)
148149
return is_found
149150
end
150151
end
151152

152-
153153
Base.in(key, tree::SplayTree) = haskey(tree, key)
154154

155155
function Base.delete!(tree::SplayTree{K}, d::K) where K
156156
node = tree.root
157157
x = search_node(tree, d)
158-
(x == nothing) && return tree
158+
(x == nothing) && return tree
159159
t = nothing
160160
s = nothing
161161

162162
splay!(tree, x)
163163

164-
if !isa(x.rightChild, Nothing)
164+
if x.rightChild !== nothing
165165
t = x.rightChild
166166
t.parent = nothing
167167
end
168168

169169
s = x
170170
s.rightChild = nothing
171171

172-
if !isa(s.leftChild, Nothing)
172+
if s.leftChild !== nothing
173173
s.leftChild.parent = nothing
174174
end
175175

@@ -178,17 +178,17 @@ function Base.delete!(tree::SplayTree{K}, d::K) where K
178178
return tree
179179
end
180180

181-
function Base.insert!(tree::SplayTree{K}, d::K) where K
181+
function insert!(tree::SplayTree{K}, d::K) where K
182182
is_present = search_node(tree, d)
183-
if !isa(is_present, Nothing) && (is_present.data == d)
183+
if (is_present !== nothing) && (is_present.data == d)
184184
return tree
185185
end
186186
# only unique keys are inserted
187187
node = SplayTreeNode{K}(d)
188188
y = nothing
189189
x = tree.root
190190

191-
while !isa(x, Nothing)
191+
while x !== nothing
192192
y = x
193193
if node.data > x.data
194194
x = x.rightChild
@@ -198,7 +198,7 @@ function Base.insert!(tree::SplayTree{K}, d::K) where K
198198
end
199199
node.parent = y
200200

201-
if isa(y, Nothing)
201+
if y === nothing
202202
tree.root = node
203203
elseif node.data < y.data
204204
y.leftChild = node
@@ -217,7 +217,7 @@ end
217217

218218
function Base.getindex(tree::SplayTree{K}, ind) where K
219219
@boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)"))
220-
function traverse_tree_inorder(node::SplayTreeNode_or_null)
220+
function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing})
221221
if (node != nothing)
222222
left = traverse_tree_inorder(node.leftChild)
223223
right = traverse_tree_inorder(node.rightChild)

test/test_splay_tree.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@testset "SplayTree" begin
2-
@testset "inserting values" begin
2+
@testset "pushing values" begin
33
t = SplayTree{Int}()
44
for i in 1:100
5-
insert!(t, i)
5+
push!(t, i)
66
end
77

88
@test length(t) == 100
@@ -19,7 +19,7 @@
1919
@testset "deleting values" begin
2020
t = SplayTree{Int}()
2121
for i in 1:100
22-
insert!(t, i)
22+
push!(t, i)
2323
end
2424
for i in 1:2:100
2525
delete!(t, i)
@@ -36,7 +36,7 @@
3636
end
3737

3838
for i in 1:2:100
39-
insert!(t, i)
39+
push!(t, i)
4040
end
4141

4242
@test length(t) == 100
@@ -45,7 +45,7 @@
4545
@testset "handling different cases of delete!" begin
4646
t2 = SplayTree()
4747
for i in 1:100000
48-
insert!(t2, i)
48+
push!(t2, i)
4949
end
5050

5151
@test length(t2) == 100000
@@ -65,12 +65,12 @@
6565
@test (length(t2) + length(visited)) == 100000
6666
end
6767

68-
@testset "handling different cases of insert!" begin
68+
@testset "handling different cases of push!" begin
6969
nums = rand(1:100000, 1000)
7070
t3 = SplayTree()
7171
uniq_nums = Set(nums)
7272
for num in nums
73-
insert!(t3, num)
73+
push!(t3, num)
7474
end
7575
@test length(t3) == length(uniq_nums)
7676
end

0 commit comments

Comments
 (0)