Skip to content

Commit 6a81a56

Browse files
authored
Merge pull request #14 from JuliaCollections/isempty
Add isempty to BinaryTree interface
2 parents b416a52 + 33aec23 commit 6a81a56

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BinaryTrees"
22
uuid = "289e92be-c05a-437b-9e67-5b0c799891f8"
33
authors = ["Elias Carvalho <[email protected]> and contributors"]
4-
version = "0.1.1"
4+
version = "0.1.2"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ using BinaryTrees
2929

3030
tree = AVLTree{Int,Float64}()
3131

32+
# check if tree is empty
33+
BinaryTree.isempty(tree)
34+
3235
# insert nodes into the tree
3336
BinaryTrees.insert!(tree, 2, 2.2) # root node
3437
BinaryTrees.insert!(tree, 1, 1.1) # left node
@@ -42,6 +45,9 @@ BinaryTrees.search(tree, 2) # root node
4245
BinaryTrees.search(tree, 1) # left node
4346
BinaryTrees.search(tree, 3) # right node
4447

48+
# search previous and next node using keys
49+
BinaryTrees.prevnext(tree, 2)
50+
4551
# delete nodes from the tree
4652
BinaryTrees.delete!(tree, 1)
4753
BinaryTrees.delete!(tree, 3)

src/binarytree.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ Root node of the `tree`.
5555
"""
5656
root(tree::BinaryTree) = tree.root
5757

58+
"""
59+
BinaryTrees.isempty(tree)
60+
61+
Tell whether or not the `tree` is empty.
62+
"""
63+
isempty(tree::BinaryTree) = isnothing(root(tree))
64+
5865
"""
5966
BinaryTrees.search(tree, key)
6067

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,15 @@ const BT = BinaryTrees
235235
@test BT.key.(BT.prevnext(tree, 5)) == (4, 6)
236236
@test BT.prevnext(tree, nothing) == (nothing, nothing)
237237

238+
# checking for emptiness
239+
tree = AVLTree{Int,Int}()
240+
@test BT.isempty(tree)
241+
tree = AVLTree{Int,Float64}()
242+
@test BT.isempty(tree)
243+
238244
# type stability
239245
tree = AVLTree{Int,Int}()
246+
@inferred BT.isempty(tree)
240247
@inferred BT.insert!(tree, 2, 20)
241248
@inferred BT.insert!(tree, 1, 10)
242249
@inferred BT.insert!(tree, 3, 30)

0 commit comments

Comments
 (0)