diff --git a/src/binarytree.jl b/src/binarytree.jl index b6e06e3..d5b40da 100644 --- a/src/binarytree.jl +++ b/src/binarytree.jl @@ -87,7 +87,14 @@ function delete! end # IO METHODS # ----------- -function Base.show(io::IO, ::MIME"text/plain", tree::BinaryTree) +function Base.show(io::IO, node::BinaryNode) + name = nameof(typeof(node)) + print(io, "$name(") + _printkeyvalue(io, node) + print(io, ")") +end + +function Base.show(io::IO, tree::BinaryTree) name = nameof(typeof(tree)) if isnothing(tree.root) print(io, "$name()") @@ -119,7 +126,13 @@ end AbstractTrees.NodeType(::Type{<:BinaryNode}) = AbstractTrees.HasNodeType() AbstractTrees.nodetype(T::Type{<:BinaryNode}) = T -function AbstractTrees.printnode(io::IO, node::BinaryNode) +AbstractTrees.printnode(io::IO, node::BinaryNode) = _printkeyvalue(io, node) + +# ----------------- +# HELPER FUNCTIONS +# ----------------- + +function _printkeyvalue(io::IO, node::BinaryNode) ioctx = IOContext(io, :compact => true, :limit => true) val = value(node) if isnothing(val) diff --git a/test/runtests.jl b/test/runtests.jl index 35f81a5..4ef66a6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -216,13 +216,13 @@ const BT = BinaryTrees # show tree = AVLTree{Int,Int}() - @test sprint(show, MIME("text/plain"), tree) == "AVLTree()" + @test sprint(show, tree) == "AVLTree()" BT.insert!(tree, 3, 30) BT.insert!(tree, 2, 20) BT.insert!(tree, 4, 40) BT.insert!(tree, 1, 10) BT.insert!(tree, 5, 50) - @test sprint(show, MIME("text/plain"), tree) == """ + @test sprint(show, tree) == """ AVLTree 3 => 30 ├─ 2 => 20 @@ -230,14 +230,20 @@ const BT = BinaryTrees └─ 4 => 40 └─ 5 => 50""" + node = BT.search(tree, 1) + @test sprint(show, node) == "AVLNode(1 => 10)" + tree = AVLTree{Int}() BT.insert!(tree, 2) BT.insert!(tree, 1) BT.insert!(tree, 3) - @test sprint(show, MIME("text/plain"), tree) == """ + @test sprint(show, tree) == """ AVLTree 2 ├─ 1 └─ 3""" + + node = BT.search(tree, 1) + @test sprint(show, node) == "AVLNode(1)" end end