Skip to content

Commit e95887d

Browse files
committed
address review
1 parent 57d4403 commit e95887d

File tree

2 files changed

+25
-58
lines changed

2 files changed

+25
-58
lines changed

src/avl_tree.jl

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ function get_balance(node::Union{AVLTreeNode, Nothing})
3737
end
3838
end
3939

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))
4143

4244
"""
43-
left_rotate!(node_x::AVLTreeNode)
45+
left_rotate(node_x::AVLTreeNode)
4446
4547
Performs a left-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
4648
"""
@@ -49,13 +51,13 @@ function left_rotate(z::AVLTreeNode)
4951
α = y.leftChild
5052
y.leftChild = z
5153
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)
5456
return y
5557
end
5658

5759
"""
58-
right_rotate!(node_x::AVLTreeNode)
60+
right_rotate(node_x::AVLTreeNode)
5961
6062
Performs a right-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
6163
"""
@@ -64,8 +66,8 @@ function right_rotate(z::AVLTreeNode)
6466
α = y.rightChild
6567
y.rightChild = z
6668
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)
6971
return y
7072
end
7173

@@ -81,13 +83,6 @@ function minimum_node(node::Union{AVLTreeNode, Nothing})
8183
return node
8284
end
8385

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-
9186
function search_node(tree::AVLTree{K}, d::K) where K
9287
prev = nothing
9388
node = tree.root
@@ -104,13 +99,6 @@ function search_node(tree::AVLTree{K}, d::K) where K
10499
return (node == nothing) ? prev : node
105100
end
106101

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-
114102
function Base.haskey(tree::AVLTree{K}, d::K) where K
115103
(tree.root == nothing) && return false
116104
node = search_node(tree, d)
@@ -119,13 +107,6 @@ end
119107

120108
Base.in(key, tree::AVLTree) = haskey(tree, key)
121109

122-
"""
123-
insert!(tree, key)
124-
125-
Inserts `key` in the `tree` if it is not present.
126-
"""
127-
insert!(tree, key)
128-
129110
function Base.insert!(tree::AVLTree{K}, d::K) where K
130111

131112
function insert_node(node::Union{AVLTreeNode, Nothing}, key)
@@ -137,7 +118,7 @@ function Base.insert!(tree::AVLTree{K}, d::K) where K
137118
node.rightChild = insert_node(node.rightChild, key)
138119
end
139120

140-
node.height = fix_height(node)
121+
node.height = compute_height(node)
141122
balance = get_balance(node)
142123

143124
if balance > 1
@@ -168,42 +149,35 @@ function Base.insert!(tree::AVLTree{K}, d::K) where K
168149
return tree
169150
end
170151

171-
"""
172-
push!(tree, key)
173-
174-
Inserts `key` in the `tree` if it is not present.
175-
"""
176152
function Base.push!(tree::AVLTree{K}, key0) where K
177153
key = convert(K, key0)
178154
insert!(tree, key)
179155
end
180156

181157
function Base.delete!(tree::AVLTree{K}, d::K) where K
182158

183-
function delete_node(node::Union{AVLTreeNode, Nothing}, key)
159+
function delete_node!(node::Union{AVLTreeNode, Nothing}, key)
184160
if node == nothing || node.data == nothing
185161
return nothing
186162
elseif key < node.data
187-
node.leftChild = delete_node(node.leftChild, key)
163+
node.leftChild = delete_node!(node.leftChild, key)
188164
elseif key > node.data
189-
node.rightChild = delete_node(node.rightChild, key)
165+
node.rightChild = delete_node!(node.rightChild, key)
190166
else
191167
if node.leftChild == nothing
192-
temp = node.rightChild
193-
node = nothing
194-
return temp
168+
result = node.rightChild
169+
return result
195170
elseif node.rightChild == nothing
196-
temp = node.leftChild
197-
node = nothing
198-
return temp
171+
result = node.leftChild
172+
return result
199173
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)
203177
end
204178
end
205179

206-
node.height = fix_height(node)
180+
node.height = compute_height(node)
207181
balance = get_balance(node)
208182

209183
if balance > 1
@@ -231,20 +205,13 @@ function Base.delete!(tree::AVLTree{K}, d::K) where K
231205
!haskey(tree, d) && return tree
232206

233207
# 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)
235209
tree.count -= 1
236210
return tree
237211
end
238212

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-
246213
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)"))
248215
function traverse_tree_inorder(node::Union{AVLTreeNode, Nothing})
249216
if (node != nothing)
250217
left = traverse_tree_inorder(node.leftChild)

test/test_avl_tree.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
for i in 1:10
106106
@test getindex(t6, i) == i
107107
end
108-
@test_throws ArgumentError getindex(t6, 0)
109-
@test_throws ArgumentError getindex(t6, 11)
108+
@test_throws BoundsError getindex(t6, 0)
109+
@test_throws BoundsError getindex(t6, 11)
110110
end
111111

112112
@testset "key conversion in push!" begin

0 commit comments

Comments
 (0)