Skip to content

Commit 58f6656

Browse files
authored
Add support for trees without values (#7)
1 parent f8c0240 commit 58f6656

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ BinaryTrees.search(tree, 3) # right node
4343
# delete nodes from the tree
4444
BinaryTrees.delete!(tree, 1)
4545
BinaryTrees.delete!(tree, 3)
46+
47+
# tree that only has keys
48+
tree = AVLTree{Int}()
49+
BinaryTrees.insert!(tree, 2) # root node
50+
BinaryTrees.insert!(tree, 1) # left node
51+
BinaryTrees.insert!(tree, 3) # right node
4652
```

src/avl.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ Base.convert(::Type{AVLNode{K,V}}, node::AVLNode) where {K,V} =
2525
Construct an empty AVL Tree with keys of type `K`
2626
and values of type `V`.
2727
28-
AVLTree()
28+
AVLTree{K}()
2929
30-
Construct an empty AVL Tree that stores keys and values
31-
of any type, alias for `AVLTree{Any,Any}()`.
30+
Construct an empty AVL tree with no values and keys of type `K`.
3231
3332
The keys of AVL Tree must implement sorting operators (`>`, `<`)
3433
and comparison operators (`=`, `≠`)
@@ -54,14 +53,20 @@ BinaryTrees.search(tree, 3) # right node
5453
# delete nodes from the tree
5554
BinaryTrees.delete!(tree, 1)
5655
BinaryTrees.delete!(tree, 3)
56+
57+
# tree that only has keys
58+
tree = AVLTree{Int}()
59+
BinaryTrees.insert!(tree, 2) # root node
60+
BinaryTrees.insert!(tree, 1) # left node
61+
BinaryTrees.insert!(tree, 3) # right node
5762
```
5863
"""
5964
mutable struct AVLTree{K,V} <: BinaryTree
6065
root::Union{AVLNode{K,V},Nothing}
6166
end
6267

6368
AVLTree{K,V}() where {K,V} = AVLTree{K,V}(nothing)
64-
AVLTree() = AVLTree{Any,Any}()
69+
AVLTree{K}() where {K} = AVLTree{K,Nothing}()
6570

6671
search(tree::AVLTree{K}, key::K) where {K} = _search(tree, key)
6772

@@ -70,6 +75,8 @@ function insert!(tree::AVLTree{K}, key::K, value) where {K}
7075
tree
7176
end
7277

78+
insert!(tree::AVLTree{K,Nothing}, key::K) where {K} = insert!(tree, key, nothing)
79+
7380
function delete!(tree::AVLTree{K}, key::K) where {K}
7481
tree.root = _delete!(tree.root, key)
7582
tree

src/binarytree.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
BinaryNode
77
8-
Binary tree node with key, value and optional left and right children.
8+
Binary tree node with key and optional value, left and right children.
99
"""
1010
abstract type BinaryNode end
1111

@@ -19,7 +19,7 @@ key(node::BinaryNode) = node.key
1919
"""
2020
BinaryTrees.value(node)
2121
22-
Value of the `node`.
22+
Value of the `node`, if it does not exist, `nothing` is returned.
2323
"""
2424
value(node::BinaryNode) = node.value
2525

@@ -69,6 +69,10 @@ function search end
6969
Insert a node into the `tree` with `key` and `value`.
7070
If a node with `key` already exists, the value
7171
of the node will be updated.
72+
73+
BinaryTrees.insert!(tree, key)
74+
75+
Insert a node into the `tree` with `key` and no value.
7276
"""
7377
function insert! end
7478

@@ -117,7 +121,12 @@ AbstractTrees.nodetype(T::Type{<:BinaryNode}) = T
117121

118122
function AbstractTrees.printnode(io::IO, node::BinaryNode)
119123
ioctx = IOContext(io, :compact => true, :limit => true)
120-
show(ioctx, key(node))
121-
print(ioctx, " => ")
122-
show(ioctx, value(node))
124+
val = value(node)
125+
if isnothing(val)
126+
show(ioctx, key(node))
127+
else
128+
show(ioctx, key(node))
129+
print(ioctx, " => ")
130+
show(ioctx, val)
131+
end
123132
end

test/runtests.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ const BT = BinaryTrees
156156
@test tree |> BT.root |> BT.right |> BT.left |> BT.key == 3
157157

158158
# tree that accepts any types
159-
tree = AVLTree()
159+
tree = AVLTree{Int,Any}()
160160
BT.insert!(tree, 2, 'A')
161161
BT.insert!(tree, 1, 1.1)
162162
BT.insert!(tree, 3, "test")
@@ -174,6 +174,25 @@ const BT = BinaryTrees
174174
BT.delete!(tree, 2)
175175
@test isnothing(BT.root(tree))
176176

177+
# tree without values
178+
tree = AVLTree{Int}()
179+
BT.insert!(tree, 2)
180+
BT.insert!(tree, 1)
181+
BT.insert!(tree, 3)
182+
@test isnothing(BT.value(BT.search(tree, 2)))
183+
@test isnothing(BT.value(BT.search(tree, 1)))
184+
@test isnothing(BT.value(BT.search(tree, 3)))
185+
BT.delete!(tree, 3)
186+
@test !isnothing(BT.root(tree))
187+
@test !isnothing(BT.left(BT.root(tree)))
188+
@test isnothing(BT.right(BT.root(tree)))
189+
BT.delete!(tree, 1)
190+
@test !isnothing(BT.root(tree))
191+
@test isnothing(BT.left(BT.root(tree)))
192+
@test isnothing(BT.right(BT.root(tree)))
193+
BT.delete!(tree, 2)
194+
@test isnothing(BT.root(tree))
195+
177196
# AbstractTrees interface
178197
tree = AVLTree{Int,Int}()
179198
BT.insert!(tree, 3, 30)
@@ -210,5 +229,15 @@ const BT = BinaryTrees
210229
│ └─ 1 => 10
211230
└─ 4 => 40
212231
└─ 5 => 50"""
232+
233+
tree = AVLTree{Int}()
234+
BT.insert!(tree, 2)
235+
BT.insert!(tree, 1)
236+
BT.insert!(tree, 3)
237+
@test sprint(show, MIME("text/plain"), tree) == """
238+
AVLTree
239+
2
240+
├─ 1
241+
└─ 3"""
213242
end
214243
end

0 commit comments

Comments
 (0)