diff --git a/src/avl_tree.jl b/src/avl_tree.jl index f9f439849..3aa5e8217 100644 --- a/src/avl_tree.jl +++ b/src/avl_tree.jl @@ -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 + diff --git a/test/test_avl_tree.jl b/test/test_avl_tree.jl index 0f4c64b38..11d9adc5f 100644 --- a/test/test_avl_tree.jl +++ b/test/test_avl_tree.jl @@ -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 @@ -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