Skip to content

Commit f05f43f

Browse files
committed
updating abovebelow to prevnext to be more generally descriptive. Also accepts the key, functions like an advanced search now. fixed tests using @inferred Nothing.
1 parent 9d0a0bc commit f05f43f

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

src/binarytree.jl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,44 +166,40 @@ end
166166

167167
maxnode(node::Nothing) = nothing
168168

169-
function abovebelow(tree::BinaryNode, x::BinaryNode)
170-
above, below = nothing, nothing
171-
current = tree
169+
function prevnext(tree::BinaryTree, k)
170+
prev, next = nothing, nothing
171+
current = root(tree)
172172
# Traverse from the root to the target node, updating candidates.
173-
while !isnothing(current) && key(current) != key(x)
174-
if key(x) < key(current)
175-
# current is a potential above (successor)
176-
above = current
173+
while !isnothing(current) && key(current) != k
174+
if k < key(current)
175+
# current is a potential next (successor)
176+
next = current
177177
current = left(current)
178-
else # x.key > current.key
179-
# current is a potential below (predecessor)
180-
below = current
178+
else # k.key > current.key
179+
# current is a potential previous (predecessor)
180+
prev = current
181181
current = right(current)
182182
end
183183
end
184184

185185
# If the node wasn't found, return the best candidate values
186186
if isnothing(current)
187-
return (above, below)
187+
return (prev, next)
188188
end
189189

190190
# Found the node with key equal to x.key.
191-
# Now, if there is a left subtree, the true below (predecessor) is the maximum in that subtree.
191+
# Now, if there is a left subtree, the true previous (predecessor) is the maximum in that subtree.
192192
if !isnothing(left(current))
193-
below = maxnode(left(current))
193+
prev = maxnode(left(current))
194194
end
195-
# Similarly, if there is a right subtree, the true above (successor) is the minimum in that subtree.
195+
# Similarly, if there is a right subtree, the true next (successor) is the minimum in that subtree.
196196
if !isnothing(right(current))
197-
above = minnode(right(current))
197+
next = minnode(right(current))
198198
end
199199

200-
(above, below)
200+
(prev, next)
201201
end
202202

203-
function abovebelow(tree::BinaryTree, x::BinaryNode)
204-
abovebelow(root(tree), x)
205-
end
206-
207-
function abovebelow(tree, x::Nothing)
203+
function prevnext(tree::BinaryTree, k::Nothing)
208204
(nothing, nothing)
209205
end

test/runtests.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,18 @@ const BT = BinaryTrees
230230
BT.insert!(tree, 6, 40)
231231
@test BT.key(BT.minnode(tree)) == 0
232232
@test BT.key(BT.maxnode(tree)) == 6
233-
@test BT.abovebelow(tree, BT.AVLNode(0, 5))[2] == nothing
234-
@test BT.key.(BT.abovebelow(tree, BT.AVLNode(2, 10))) == (3, 1)
235-
@test BT.key.(BT.abovebelow(BT.root(tree), BT.AVLNode(2, 10))) == (3, 1)
236-
@test BT.key.(BT.abovebelow(tree, BT.AVLNode(5, 30))) == (6, 4)
237-
@test BT.abovebelow(tree, nothing) == (nothing, nothing)
238-
@test BT.abovebelow(BT.root(tree), nothing) == (nothing, nothing)
233+
@test BT.prevnext(tree, 0)[1] == nothing
234+
@test BT.key.(BT.prevnext(tree, 2)) == (1, 3)
235+
@test BT.key.(BT.prevnext(tree, 5)) == (4, 6)
236+
@test BT.prevnext(tree, nothing) == (nothing, nothing)
239237

240238
# type stability
241239
tree = AVLTree{Int,Int}()
242240
@inferred BT.insert!(tree, 2, 20)
243241
@inferred BT.insert!(tree, 1, 10)
244242
@inferred BT.insert!(tree, 3, 30)
245-
@inferred BT.minnode(tree)
246-
@inferred BT.maxnode(tree)
247-
@inferred BT.abovebelow(tree, BT.AVLNode(2, 20))
243+
@inferred Nothing BT.minnode(tree)
244+
@inferred Nothing BT.maxnode(tree)
248245
@inferred Nothing BT.search(tree, 2)
249246
@inferred Nothing BT.search(tree, 1)
250247
@inferred Nothing BT.search(tree, 3)
@@ -255,6 +252,8 @@ const BT = BinaryTrees
255252
@inferred BT.insert!(tree, 2)
256253
@inferred BT.insert!(tree, 1)
257254
@inferred BT.insert!(tree, 3)
255+
@inferred Nothing BT.minnode(tree)
256+
@inferred Nothing BT.maxnode(tree)
258257
@inferred Nothing BT.search(tree, 2)
259258
@inferred Nothing BT.search(tree, 1)
260259
@inferred Nothing BT.search(tree, 3)
@@ -265,6 +264,8 @@ const BT = BinaryTrees
265264
@inferred BT.insert!(tree, "key2", 2)
266265
@inferred BT.insert!(tree, "key1", 1)
267266
@inferred BT.insert!(tree, "key3", 3)
267+
@inferred Nothing BT.minnode(tree)
268+
@inferred Nothing BT.maxnode(tree)
268269
@inferred Nothing BT.search(tree, "key2")
269270
@inferred Nothing BT.search(tree, "key1")
270271
@inferred Nothing BT.search(tree, "key3")
@@ -275,6 +276,8 @@ const BT = BinaryTrees
275276
@inferred BT.insert!(tree, (0, 1, 0), 2)
276277
@inferred BT.insert!(tree, (0, 0, 1), 1)
277278
@inferred BT.insert!(tree, (1, 0, 0), 3)
279+
@inferred Nothing BT.minnode(tree)
280+
@inferred Nothing BT.maxnode(tree)
278281
@inferred Nothing BT.search(tree, (0, 1, 0))
279282
@inferred Nothing BT.search(tree, (0, 0, 1))
280283
@inferred Nothing BT.search(tree, (1, 0, 0))

0 commit comments

Comments
 (0)