From 308417fb40ff2883349e283304a253b0e505423d Mon Sep 17 00:00:00 2001 From: lfenzo Date: Sat, 16 Apr 2022 18:18:33 -0300 Subject: [PATCH 1/3] implemented maximum/minimum for AVL tree --- src/avl_tree.jl | 27 +++++++++++++++++++++++++++ test/test_avl_tree.jl | 35 +++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/avl_tree.jl b/src/avl_tree.jl index f9f439849..e56bb42c4 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{K}) where K + +Return biggest key in `tree` AVL Tree. +""" +function Base.maximum(tree::AVLTree{K}) where K + node = tree.root + while node.rightChild != nothing + node = node.rightChild + end + return node.data +end + +""" + minimum(tree::AVLTree{K}) where K + +Return smallest key in `tree` AVL Tree. +""" +function Base.minimum(tree::AVLTree{K}) where K + 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..c0b9d7ab0 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(tree) == 1 + @test maximum(tree) == 32 + delete!(tree, 32) + @test maximum(tree) == 31 + delete!(tree, 1) + @test minimum(tree) == 2 + delete!(tree, 20) + delete!(tree, 18) + delete!(tree, 5) + delete!(tree, 25) + @test maximum(tree) == 31 + @test minimum(tree) == 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 From 9c00b837057b8708f71575581dfb58c59203c3a9 Mon Sep 17 00:00:00 2001 From: lfenzo Date: Sat, 16 Apr 2022 21:44:03 -0300 Subject: [PATCH 2/3] Fix avl maximum/minimum broken test --- test/test_avl_tree.jl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/test_avl_tree.jl b/test/test_avl_tree.jl index c0b9d7ab0..11d9adc5f 100644 --- a/test/test_avl_tree.jl +++ b/test/test_avl_tree.jl @@ -121,18 +121,18 @@ for i in 1:32 push!(t8, i) end - @test minimum(tree) == 1 - @test maximum(tree) == 32 - delete!(tree, 32) - @test maximum(tree) == 31 - delete!(tree, 1) - @test minimum(tree) == 2 - delete!(tree, 20) - delete!(tree, 18) - delete!(tree, 5) - delete!(tree, 25) - @test maximum(tree) == 31 - @test minimum(tree) == 2 + @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 From e19ff6ee1e8dffdaf73e49bf2ae16ac9e64f7e6a Mon Sep 17 00:00:00 2001 From: lfenzo Date: Sat, 16 Apr 2022 21:45:31 -0300 Subject: [PATCH 3/3] Remove unnecessary type information in avl tree maximum/minimum functions --- src/avl_tree.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/avl_tree.jl b/src/avl_tree.jl index e56bb42c4..3aa5e8217 100644 --- a/src/avl_tree.jl +++ b/src/avl_tree.jl @@ -270,11 +270,11 @@ function Base.getindex(tree::AVLTree{K}, ind::Integer) where K end """ - maximum(tree::AVLTree{K}) where K + maximum(tree::AVLTree) Return biggest key in `tree` AVL Tree. """ -function Base.maximum(tree::AVLTree{K}) where K +function Base.maximum(tree::AVLTree) node = tree.root while node.rightChild != nothing node = node.rightChild @@ -283,11 +283,11 @@ function Base.maximum(tree::AVLTree{K}) where K end """ - minimum(tree::AVLTree{K}) where K + minimum(tree::AVLTree) Return smallest key in `tree` AVL Tree. """ -function Base.minimum(tree::AVLTree{K}) where K +function Base.minimum(tree::AVLTree) node = tree.root while node.leftChild != nothing node = node.leftChild