1
+ # it has unique keys
2
+ # leftChild has keys which are less than the node
3
+ # rightChild has keys which are greater than the node
4
+ # height stores the height of the subtree.
1
5
mutable struct AVLTreeNode{K}
2
6
height:: Int8
3
7
leftChild:: Union{AVLTreeNode{K}, Nothing}
4
8
rightChild:: Union{AVLTreeNode{K}, Nothing}
5
9
data:: K
6
10
7
- AVLTreeNode {K} () where K = new {K} (0 , nothing , nothing )
8
11
AVLTreeNode {K} (d:: K ) where K = new {K} (1 , nothing , nothing , d)
9
12
end
10
13
37
40
fix_height (node:: AVLTreeNode ) = 1 + max (get_height (node. leftChild), get_height (node. rightChild))
38
41
39
42
"""
40
- left_rotate!(node_x::RBTreeNode )
43
+ left_rotate!(node_x::AVLTreeNode )
41
44
42
45
Performs a left-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
43
46
"""
@@ -52,7 +55,7 @@ function left_rotate(z::AVLTreeNode)
52
55
end
53
56
54
57
"""
55
- right_rotate!(node_x::RBTreeNode )
58
+ right_rotate!(node_x::AVLTreeNode )
56
59
57
60
Performs a right-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
58
61
"""
@@ -67,9 +70,9 @@ function right_rotate(z::AVLTreeNode)
67
70
end
68
71
69
72
"""
70
- minimum_node(tree::RBTree , node::RBTreeNode )
73
+ minimum_node(tree::AVLTree , node::AVLTreeNode )
71
74
72
- Returns the RBTreeNode with minimum value in subtree of `node`.
75
+ Returns the AVLTreeNode with minimum value in subtree of `node`.
73
76
"""
74
77
function minimum_node (node:: Union{AVLTreeNode, Nothing} )
75
78
while node != nothing && node. leftChild != nothing
@@ -114,6 +117,8 @@ function haskey(tree::AVLTree{K}, d::K) where K
114
117
return (node. data == d)
115
118
end
116
119
120
+ Base. in (key, tree:: AVLTree ) = haskey (tree, key)
121
+
117
122
"""
118
123
insert!(tree, key)
119
124
@@ -138,7 +143,7 @@ function Base.insert!(tree::AVLTree{K}, d::K) where K
138
143
if balance > 1
139
144
if key < node. leftChild. data
140
145
return right_rotate (node)
141
-
146
+ else
142
147
node. leftChild = left_rotate (node. leftChild)
143
148
return right_rotate (node)
144
149
end
@@ -198,10 +203,6 @@ function Base.delete!(tree::AVLTree{K}, d::K) where K
198
203
end
199
204
end
200
205
201
- if node == nothing
202
- return node
203
- end
204
-
205
206
node. height = fix_height (node)
206
207
balance = get_balance (node)
207
208
@@ -234,3 +235,25 @@ function Base.delete!(tree::AVLTree{K}, d::K) where K
234
235
tree. count -= 1
235
236
return tree
236
237
end
238
+
239
+ """
240
+ getindex(tree, ind)
241
+
242
+ Gets the key present at index `ind` of the tree. Indexing is done in increasing order of key.
243
+ """
244
+ getindex (tree, ind)
245
+
246
+ function Base. getindex (tree:: AVLTree{K} , ind) where K
247
+ @boundscheck (1 <= ind <= tree. count) || throw (ArgumentError (" $ind should be in between 1 and $(tree. count) " ))
248
+ function traverse_tree_inorder (node:: Union{AVLTreeNode, Nothing} )
249
+ if (node != nothing )
250
+ left = traverse_tree_inorder (node. leftChild)
251
+ right = traverse_tree_inorder (node. rightChild)
252
+ append! (push! (left, node. data), right)
253
+ else
254
+ return K[]
255
+ end
256
+ end
257
+ arr = traverse_tree_inorder (tree. root)
258
+ return @inbounds arr[ind]
259
+ end
0 commit comments