@@ -37,10 +37,12 @@ function get_balance(node::Union{AVLTreeNode, Nothing})
37
37
end
38
38
end
39
39
40
- fix_height (node:: AVLTreeNode ) = 1 + max (get_height (node. leftChild), get_height (node. rightChild))
40
+ # computes the height of the subtree, which basically is
41
+ # one added the maximum of the height of the left subtree and right subtree
42
+ compute_height (node:: AVLTreeNode ) = 1 + max (get_height (node. leftChild), get_height (node. rightChild))
41
43
42
44
"""
43
- left_rotate! (node_x::AVLTreeNode)
45
+ left_rotate(node_x::AVLTreeNode)
44
46
45
47
Performs a left-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
46
48
"""
@@ -49,13 +51,13 @@ function left_rotate(z::AVLTreeNode)
49
51
α = y. leftChild
50
52
y. leftChild = z
51
53
z. rightChild = α
52
- z. height = fix_height (z)
53
- y. height = fix_height (y)
54
+ z. height = compute_height (z)
55
+ y. height = compute_height (y)
54
56
return y
55
57
end
56
58
57
59
"""
58
- right_rotate! (node_x::AVLTreeNode)
60
+ right_rotate(node_x::AVLTreeNode)
59
61
60
62
Performs a right-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
61
63
"""
@@ -64,8 +66,8 @@ function right_rotate(z::AVLTreeNode)
64
66
α = y. rightChild
65
67
y. rightChild = z
66
68
z. leftChild = α
67
- z. height = fix_height (z)
68
- y. height = fix_height (y)
69
+ z. height = compute_height (z)
70
+ y. height = compute_height (y)
69
71
return y
70
72
end
71
73
@@ -81,13 +83,6 @@ function minimum_node(node::Union{AVLTreeNode, Nothing})
81
83
return node
82
84
end
83
85
84
- """
85
- search_node(tree, key)
86
-
87
- Returns the last visited node, while traversing through in binary-search-tree fashion looking for `key`.
88
- """
89
- search_node (tree, key)
90
-
91
86
function search_node (tree:: AVLTree{K} , d:: K ) where K
92
87
prev = nothing
93
88
node = tree. root
@@ -104,13 +99,6 @@ function search_node(tree::AVLTree{K}, d::K) where K
104
99
return (node == nothing ) ? prev : node
105
100
end
106
101
107
- """
108
- haskey(tree, key)
109
-
110
- Returns true if `key` is present in the `tree`, else returns false.
111
- """
112
- haskey (tree, key)
113
-
114
102
function Base. haskey (tree:: AVLTree{K} , d:: K ) where K
115
103
(tree. root == nothing ) && return false
116
104
node = search_node (tree, d)
119
107
120
108
Base. in (key, tree:: AVLTree ) = haskey (tree, key)
121
109
122
- """
123
- insert!(tree, key)
124
-
125
- Inserts `key` in the `tree` if it is not present.
126
- """
127
- insert! (tree, key)
128
-
129
110
function Base. insert! (tree:: AVLTree{K} , d:: K ) where K
130
111
131
112
function insert_node (node:: Union{AVLTreeNode, Nothing} , key)
@@ -137,7 +118,7 @@ function Base.insert!(tree::AVLTree{K}, d::K) where K
137
118
node. rightChild = insert_node (node. rightChild, key)
138
119
end
139
120
140
- node. height = fix_height (node)
121
+ node. height = compute_height (node)
141
122
balance = get_balance (node)
142
123
143
124
if balance > 1
@@ -168,42 +149,35 @@ function Base.insert!(tree::AVLTree{K}, d::K) where K
168
149
return tree
169
150
end
170
151
171
- """
172
- push!(tree, key)
173
-
174
- Inserts `key` in the `tree` if it is not present.
175
- """
176
152
function Base. push! (tree:: AVLTree{K} , key0) where K
177
153
key = convert (K, key0)
178
154
insert! (tree, key)
179
155
end
180
156
181
157
function Base. delete! (tree:: AVLTree{K} , d:: K ) where K
182
158
183
- function delete_node (node:: Union{AVLTreeNode, Nothing} , key)
159
+ function delete_node! (node:: Union{AVLTreeNode, Nothing} , key)
184
160
if node == nothing || node. data == nothing
185
161
return nothing
186
162
elseif key < node. data
187
- node. leftChild = delete_node (node. leftChild, key)
163
+ node. leftChild = delete_node! (node. leftChild, key)
188
164
elseif key > node. data
189
- node. rightChild = delete_node (node. rightChild, key)
165
+ node. rightChild = delete_node! (node. rightChild, key)
190
166
else
191
167
if node. leftChild == nothing
192
- temp = node. rightChild
193
- node = nothing
194
- return temp
168
+ result = node. rightChild
169
+ return result
195
170
elseif node. rightChild == nothing
196
- temp = node. leftChild
197
- node = nothing
198
- return temp
171
+ result = node. leftChild
172
+ return result
199
173
else
200
- temp = minimum_node (node. rightChild)
201
- node. data = temp . data
202
- node. rightChild = delete_node (node. rightChild, temp . data)
174
+ result = minimum_node (node. rightChild)
175
+ node. data = result . data
176
+ node. rightChild = delete_node! (node. rightChild, result . data)
203
177
end
204
178
end
205
179
206
- node. height = fix_height (node)
180
+ node. height = compute_height (node)
207
181
balance = get_balance (node)
208
182
209
183
if balance > 1
@@ -231,20 +205,13 @@ function Base.delete!(tree::AVLTree{K}, d::K) where K
231
205
! haskey (tree, d) && return tree
232
206
233
207
# if the key is present, delete it from the tree
234
- tree. root = delete_node (tree. root, d)
208
+ tree. root = delete_node! (tree. root, d)
235
209
tree. count -= 1
236
210
return tree
237
211
end
238
212
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
213
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) " ))
214
+ @boundscheck (1 <= ind <= tree. count) || throw (BoundsError (" $ind should be in between 1 and $(tree. count) " ))
248
215
function traverse_tree_inorder (node:: Union{AVLTreeNode, Nothing} )
249
216
if (node != nothing )
250
217
left = traverse_tree_inorder (node. leftChild)
0 commit comments