Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented maximum/minimum for AVL Trees #805

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/avl_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,30 @@ function Base.getindex(tree::AVLTree{K}, ind::Integer) where K
value = traverse_tree(tree.root, ind)
return value
end

"""
maximum(tree::AVLTree)

Return biggest key in `tree` AVL Tree.
"""
function Base.maximum(tree::AVLTree)
node = tree.root
while node.rightChild != nothing
node = node.rightChild
end
return node.data
end

"""
minimum(tree::AVLTree)

Return smallest key in `tree` AVL Tree.
"""
function Base.minimum(tree::AVLTree)
node = tree.root
while node.leftChild != nothing
node = node.leftChild
end
return node.data
end

35 changes: 27 additions & 8 deletions test/test_avl_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,34 @@
@test haskey(t7, 1)
end

@testset "minimum_node" begin
@testset "maximum and minimum" begin
t8 = AVLTree{Int}()
@test minimum_node(t8.root) == nothing
for i in 1:32
push!(t8, i)
end
m1 = minimum_node(t8.root)
@test minimum(t8) == 1
@test maximum(t8) == 32
delete!(t8, 32)
@test maximum(t8) == 31
delete!(t8, 1)
@test minimum(t8) == 2
delete!(t8, 20)
delete!(t8, 18)
delete!(t8, 5)
delete!(t8, 25)
@test maximum(t8) == 31
@test minimum(t8) == 2
end

@testset "minimum_node" begin
t9 = AVLTree{Int}()
@test minimum_node(t9.root) == nothing
for i in 1:32
push!(t9, i)
end
m1 = minimum_node(t9.root)
@test m1.data == 1
node = t8.root
node = t9.root
while node.leftChild != nothing
m = minimum_node(node.leftChild)
@test m == m1
Expand All @@ -133,13 +152,13 @@
end

@testset "rank" begin
t9 = AVLTree{Int}()
t10 = AVLTree{Int}()
for i in 1:20
push!(t9, i)
push!(t10, i)
end
for i in 1:20
@test sorted_rank(t9, i) == i
@test sorted_rank(t10, i) == i
end
@test_throws KeyError sorted_rank(t9, 21)
@test_throws KeyError sorted_rank(t10, 21)
end
end