@@ -8,14 +8,11 @@ mutable struct SplayTreeNode{K}
8
8
SplayTreeNode {K} (d:: K ) where K = new {K} (nothing , nothing , nothing , d)
9
9
end
10
10
11
- SplayTreeNode_or_null{K} = Union{SplayTreeNode{K}, Nothing}
12
-
13
11
SplayTreeNode (d) = SplayTreeNode {Any} (d)
14
12
SplayTreeNode () = SplayTreeNode {Any} ()
15
13
16
-
17
14
mutable struct SplayTree{K}
18
- root:: SplayTreeNode_or_null{K }
15
+ root:: Union{SplayTreeNode{K}, Nothing }
19
16
count:: Int
20
17
21
18
SplayTree {K} () where K = new {K} (nothing , 0 )
@@ -64,13 +61,12 @@ function right_rotate!(tree::SplayTree, node_x::SplayTreeNode)
64
61
node_x. parent = node_y
65
62
end
66
63
67
-
64
+ # The splaying operation moves node_x to the root of the tree using the series of rotations.
68
65
function splay! (tree:: SplayTree , node_x:: SplayTreeNode )
69
- while ! isa ( node_x. parent, Nothing)
66
+ while node_x. parent != = nothing
70
67
parent = node_x. parent
71
68
grand_parent = node_x. parent. parent
72
- # grand-parent is Null
73
- if isa (grand_parent, Nothing)
69
+ if grand_parent === nothing
74
70
# single rotation
75
71
if node_x == parent. leftChild
76
72
# zig rotation
@@ -100,18 +96,23 @@ function splay!(tree::SplayTree, node_x::SplayTreeNode)
100
96
end
101
97
end
102
98
103
- function maximum_node (node:: SplayTreeNode_or_null )
99
+ function maximum_node (node:: Union{SplayTreeNode, Nothing} )
104
100
(node == nothing ) && return node
105
101
while node. rightChild != nothing
106
102
node = node. rightChild
107
103
end
108
104
return node
109
105
end
110
106
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
113
114
return t
114
- elseif isa (t, Nothing)
115
+ elseif t === nothing
115
116
return s
116
117
else
117
118
x = maximum_node (s)
@@ -136,40 +137,39 @@ function search_node(tree::SplayTree{K}, d::K) where K
136
137
return (node == nothing ) ? prev : node
137
138
end
138
139
139
- function haskey (tree:: SplayTree{K} , d:: K ) where K
140
+ function Base . haskey (tree:: SplayTree{K} , d:: K ) where K
140
141
node = tree. root
141
- if isa ( node, Nothing)
142
+ if node === nothing
142
143
return false
143
144
else
144
145
node = search_node (tree, d)
145
- isa (node, Nothing ) && return false
146
+ (node === nothing ) && return false
146
147
is_found = (node. data == d)
147
148
is_found && splay! (tree, node)
148
149
return is_found
149
150
end
150
151
end
151
152
152
-
153
153
Base. in (key, tree:: SplayTree ) = haskey (tree, key)
154
154
155
155
function Base. delete! (tree:: SplayTree{K} , d:: K ) where K
156
156
node = tree. root
157
157
x = search_node (tree, d)
158
- (x == nothing ) && return tree
158
+ (x == nothing ) && return tree
159
159
t = nothing
160
160
s = nothing
161
161
162
162
splay! (tree, x)
163
163
164
- if ! isa ( x. rightChild, Nothing)
164
+ if x. rightChild != = nothing
165
165
t = x. rightChild
166
166
t. parent = nothing
167
167
end
168
168
169
169
s = x
170
170
s. rightChild = nothing
171
171
172
- if ! isa ( s. leftChild, Nothing)
172
+ if s. leftChild != = nothing
173
173
s. leftChild. parent = nothing
174
174
end
175
175
@@ -178,17 +178,17 @@ function Base.delete!(tree::SplayTree{K}, d::K) where K
178
178
return tree
179
179
end
180
180
181
- function Base . insert! (tree:: SplayTree{K} , d:: K ) where K
181
+ function insert! (tree:: SplayTree{K} , d:: K ) where K
182
182
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)
184
184
return tree
185
185
end
186
186
# only unique keys are inserted
187
187
node = SplayTreeNode {K} (d)
188
188
y = nothing
189
189
x = tree. root
190
190
191
- while ! isa (x, Nothing)
191
+ while x != = nothing
192
192
y = x
193
193
if node. data > x. data
194
194
x = x. rightChild
@@ -198,7 +198,7 @@ function Base.insert!(tree::SplayTree{K}, d::K) where K
198
198
end
199
199
node. parent = y
200
200
201
- if isa (y, Nothing)
201
+ if y === nothing
202
202
tree. root = node
203
203
elseif node. data < y. data
204
204
y. leftChild = node
217
217
218
218
function Base. getindex (tree:: SplayTree{K} , ind) where K
219
219
@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} )
221
221
if (node != nothing )
222
222
left = traverse_tree_inorder (node. leftChild)
223
223
right = traverse_tree_inorder (node. rightChild)
0 commit comments