11
11
SplayTreeNode (d) = SplayTreeNode {Any} (d)
12
12
SplayTreeNode () = SplayTreeNode {Any} ()
13
13
14
+ Base. setproperty! (x:: SplayTreeNode{K} , f:: Symbol , v) where {K} =
15
+ setfield! (x, f, v)
16
+
14
17
mutable struct SplayTree{K}
15
18
root:: Union{SplayTreeNode{K}, Nothing}
16
19
count:: Int
17
20
18
21
SplayTree {K} () where K = new {K} (nothing , 0 )
19
- end
22
+ end
20
23
21
24
Base. length (tree:: SplayTree ) = tree. count
22
25
@@ -41,7 +44,7 @@ function left_rotate!(tree::SplayTree, node_x::SplayTreeNode)
41
44
node_y. leftChild = node_x
42
45
end
43
46
node_x. parent = node_y
44
- end
47
+ end
45
48
46
49
function right_rotate! (tree:: SplayTree , node_x:: SplayTreeNode )
47
50
node_y = node_x. leftChild
@@ -59,7 +62,7 @@ function right_rotate!(tree::SplayTree, node_x::SplayTreeNode)
59
62
end
60
63
node_y. rightChild = node_x
61
64
node_x. parent = node_y
62
- end
65
+ end
63
66
64
67
# The splaying operation moves node_x to the root of the tree using the series of rotations.
65
68
function splay! (tree:: SplayTree , node_x:: SplayTreeNode )
@@ -71,7 +74,7 @@ function splay!(tree::SplayTree, node_x::SplayTreeNode)
71
74
if node_x == parent. leftChild
72
75
# zig rotation
73
76
right_rotate! (tree, node_x. parent)
74
- else
77
+ else
75
78
# zag rotation
76
79
left_rotate! (tree, node_x. parent)
77
80
end
@@ -104,7 +107,7 @@ function maximum_node(node::Union{SplayTreeNode, Nothing})
104
107
return node
105
108
end
106
109
107
- # Join operations joins two trees S and T
110
+ # Join operations joins two trees S and T
108
111
# All the items in S are smaller than the items in T.
109
112
# This is a two-step process.
110
113
# In the first step, splay the largest node in S. This moves the largest node to the root node.
@@ -157,10 +160,10 @@ function Base.delete!(tree::SplayTree{K}, d::K) where K
157
160
x = search_node (tree, d)
158
161
(x == nothing ) && return tree
159
162
t = nothing
160
- s = nothing
161
-
163
+ s = nothing
164
+
162
165
splay! (tree, x)
163
-
166
+
164
167
if x. rightChild != = nothing
165
168
t = x. rightChild
166
169
t. parent = nothing
@@ -211,7 +214,7 @@ function Base.push!(tree::SplayTree{K}, d0) where K
211
214
return tree
212
215
end
213
216
214
- function Base. getindex (tree:: SplayTree{K} , ind) where K
217
+ function Base. getindex (tree:: SplayTree{K} , ind) where K
215
218
@boundscheck (1 <= ind <= tree. count) || throw (KeyError (" $ind should be in between 1 and $(tree. count) " ))
216
219
function traverse_tree_inorder (node:: Union{SplayTreeNode, Nothing} )
217
220
if (node != nothing )
@@ -222,6 +225,6 @@ function Base.getindex(tree::SplayTree{K}, ind) where K
222
225
return K[]
223
226
end
224
227
end
225
- arr = traverse_tree_inorder (tree. root)
228
+ arr = traverse_tree_inorder (tree. root)
226
229
return @inbounds arr[ind]
227
230
end
0 commit comments