Skip to content

Commit af11003

Browse files
committed
Add delete method
1 parent b854b90 commit af11003

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

src/avl.jl

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ function Base.setindex!(tree::AVLTree, value, key)
5151
tree
5252
end
5353

54+
function Base.delete!(tree::AVLTree{K}, key::K) where {K}
55+
_delete!(tree.root, key)
56+
tree
57+
end
58+
5459
function Base.show(io::IO, ::MIME"text/plain", tree::AVLTree)
5560
if isnothing(tree.root)
5661
print(io, "AVLTree()")
@@ -78,11 +83,14 @@ function _insert!(root, key, value)
7883
return AVLNode(key, value)
7984
elseif key < root.key
8085
root.left = _insert!(root.left, key, value)
81-
else
86+
elseif key > root.key
8287
root.right = _insert!(root.right, key, value)
88+
else
89+
root.value = value
90+
return root
8391
end
8492

85-
root.height = 1 + max(_height(root.left), _height(root.right))
93+
_updateheight!(root)
8694

8795
bf = _balancefactor(root)
8896

@@ -99,14 +107,44 @@ function _insert!(root, key, value)
99107
end
100108
end
101109

102-
_height(::Nothing) = 0
103-
_height(node::AVLNode) = node.height
110+
function _delete!(root, key)
111+
if isnothing(root)
112+
return root
113+
elseif key < root.key
114+
root.left = _delete!(root.left, key)
115+
elseif key > root.key
116+
root.right = _delete!(root.right, key)
117+
else
118+
if isnothing(root.left)
119+
return root.right
120+
elseif isnothing(root.right)
121+
return root.left
122+
else
123+
temp = _minnode(root.right)
124+
root.key = temp.key
125+
root.value = temp.value
126+
root.right = _delete!(root.right, temp.key)
127+
end
128+
end
104129

105-
_balancefactor(::Nothing) = 0
106-
_balancefactor(node::AVLNode) = _height(node.left) - _height(node.right)
130+
_updateheight!(root)
107131

108-
_minnode(::Nothing) = nothing
109-
_minnode(node::AVLNode) = isnothing(node.left) ? node : _minnode(node.left)
132+
bf = _balancefactor(root)
133+
134+
if bf > 1 && _balancefactor(root.left) 0
135+
_rightrotate!(root)
136+
elseif bf < -1 && _balancefactor(root.right) 0
137+
_leftrotate!(root)
138+
elseif bf > 1 && _balancefactor(root.left) < 0
139+
root.left = _leftrotate!(root.left)
140+
_rightrotate!(root)
141+
elseif bf < -1 && _balancefactor(root.right) > 0
142+
root.right = _rightrotate!(root.right)
143+
_leftrotate!(root)
144+
else
145+
root
146+
end
147+
end
110148

111149
function _leftrotate!(node)
112150
B = node.right
@@ -115,8 +153,8 @@ function _leftrotate!(node)
115153
B.left = node
116154
node.right = Y
117155

118-
node.height = 1 + max(_height(node.left), _height(node.right))
119-
B.height = 1 + max(_height(B.left), _height(B.right))
156+
_updateheight!(node)
157+
_updateheight!(B)
120158

121159
B
122160
end
@@ -128,8 +166,22 @@ function _rightrotate!(node)
128166
A.right = node
129167
node.left = Y
130168

131-
node.height = 1 + max(_height(node.left), _height(node.right))
132-
A.height = 1 + max(_height(A.left), _height(A.right))
169+
_updateheight!(node)
170+
_updateheight!(A)
133171

134172
A
135173
end
174+
175+
function _updateheight!(node)
176+
node.height = 1 + max(_height(node.left), _height(node.right))
177+
node
178+
end
179+
180+
_height(::Nothing) = 0
181+
_height(node::AVLNode) = node.height
182+
183+
_balancefactor(::Nothing) = 0
184+
_balancefactor(node::AVLNode) = _height(node.left) - _height(node.right)
185+
186+
_minnode(::Nothing) = nothing
187+
_minnode(node::AVLNode) = isnothing(node.left) ? node : _minnode(node.left)

0 commit comments

Comments
 (0)