Skip to content

Commit 054d38c

Browse files
authored
Merge pull request #2 from eliascarv/tests
Add more tests
2 parents 3ddf199 + 70e7364 commit 054d38c

File tree

3 files changed

+155
-20
lines changed

3 files changed

+155
-20
lines changed

src/avl.jl

+15-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ end
2929

3030
AbstractTrees.nodevalue(node::AVLNode) = node.value
3131

32-
AbstractTrees.NodeType(::Type{<:AVLNode}) = HasNodeType()
32+
AbstractTrees.NodeType(::Type{<:AVLNode}) = AbstractTrees.HasNodeType()
3333
AbstractTrees.nodetype(T::Type{<:AVLNode}) = T
3434

3535
function AbstractTrees.printnode(io::IO, node::AVLNode)
@@ -109,9 +109,9 @@ function _insert!(root, key, value)
109109
elseif bf < -1 && key > root.right.key
110110
_leftrotate!(root)
111111
elseif bf > 1 && key > root.left.key
112-
_rightrotate!(root)
112+
_leftrightrotate!(root)
113113
elseif bf < -1 && key < root.right.key
114-
_leftrotate!(root)
114+
_rightleftrotate!(root)
115115
else
116116
root
117117
end
@@ -146,11 +146,9 @@ function _delete!(root, key)
146146
elseif bf < -1 && _balancefactor(root.right) 0
147147
_leftrotate!(root)
148148
elseif bf > 1 && _balancefactor(root.left) < 0
149-
root.left = _leftrotate!(root.left)
150-
_rightrotate!(root)
149+
_leftrightrotate!(root)
151150
elseif bf < -1 && _balancefactor(root.right) > 0
152-
root.right = _rightrotate!(root.right)
153-
_leftrotate!(root)
151+
_rightleftrotate!(root)
154152
else
155153
root
156154
end
@@ -182,6 +180,16 @@ function _rightrotate!(node)
182180
A
183181
end
184182

183+
function _leftrightrotate!(node)
184+
node.left = _leftrotate!(node.left)
185+
_rightrotate!(node)
186+
end
187+
188+
function _rightleftrotate!(node)
189+
node.right = _rightrotate!(node.right)
190+
_leftrotate!(node)
191+
end
192+
185193
function _updateheight!(node)
186194
node.height = 1 + max(_height(node.left), _height(node.right))
187195
node

test/Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[deps]
2+
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
23
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/runtests.jl

+139-13
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,173 @@
11
using TreeDataStructures
2+
using AbstractTrees
23
using Test
34

45
@testset "TreeDataStructures.jl" begin
56
@testset "AVLTree" begin
7+
# insert
68
tree = AVLTree{Int,Int}()
7-
tree[1] = 10
89
tree[2] = 20
10+
tree[1] = 10
911
tree[3] = 30
10-
@test tree[1] == 10
1112
@test tree[2] == 20
13+
@test tree[1] == 10
1214
@test tree[3] == 30
1315

1416
# value conversion
1517
tree = AVLTree{Int,Float64}()
16-
tree[1] = 10
1718
tree[2] = 20
19+
tree[1] = 10
1820
tree[3] = 30
19-
@test tree[1] isa Float64
20-
@test tree[1] == 10.0
2121
@test tree[2] isa Float64
2222
@test tree[2] == 20.0
23+
@test tree[1] isa Float64
24+
@test tree[1] == 10.0
2325
@test tree[3] isa Float64
2426
@test tree[3] == 30.0
2527

26-
# tree that accept any types
28+
# update values
29+
tree = AVLTree{Int,Int}()
30+
tree[2] = 20
31+
tree[1] = 10
32+
tree[3] = 30
33+
@test tree[2] == 20
34+
@test tree[1] == 10
35+
@test tree[3] == 30
36+
tree[2] = 22
37+
tree[1] = 11
38+
tree[3] = 33
39+
@test tree[2] == 22
40+
@test tree[1] == 11
41+
@test tree[3] == 33
42+
43+
# right rotate
44+
tree = AVLTree{Int,Int}()
45+
tree[3] = 30
46+
tree[2] = 20
47+
tree[1] = 10
48+
@test tree.root.key == 2
49+
@test tree.root.left.key == 1
50+
@test tree.root.right.key == 3
51+
52+
# left rotate
53+
tree = AVLTree{Int,Int}()
54+
tree[1] = 10
55+
tree[2] = 20
56+
tree[3] = 30
57+
@test tree.root.key == 2
58+
@test tree.root.left.key == 1
59+
@test tree.root.right.key == 3
60+
61+
# left-right rotate
62+
tree = AVLTree{Int,Int}()
63+
tree[3] = 30
64+
tree[1] = 10
65+
tree[2] = 20
66+
@test tree.root.key == 2
67+
@test tree.root.left.key == 1
68+
@test tree.root.right.key == 3
69+
70+
# right-left rotate
71+
tree = AVLTree{Int,Int}()
72+
tree[1] = 10
73+
tree[3] = 30
74+
tree[2] = 20
75+
@test tree.root.key == 2
76+
@test tree.root.left.key == 1
77+
@test tree.root.right.key == 3
78+
79+
# delete
80+
tree = AVLTree{Int,Int}()
81+
tree[2] = 20
82+
tree[1] = 10
83+
tree[3] = 30
84+
delete!(tree, 3)
85+
@test !isnothing(tree.root)
86+
@test !isnothing(tree.root.left)
87+
@test isnothing(tree.root.right)
88+
delete!(tree, 1)
89+
@test !isnothing(tree.root)
90+
@test isnothing(tree.root.left)
91+
@test isnothing(tree.root.right)
92+
delete!(tree, 2)
93+
@test isnothing(tree.root)
94+
95+
# right rotate
96+
tree = AVLTree{Int,Int}()
97+
tree[4] = 40
98+
tree[2] = 20
99+
tree[5] = 50
100+
tree[1] = 10
101+
tree[3] = 30
102+
delete!(tree, 4)
103+
@test tree.root.key == 2
104+
@test tree.root.left.key == 1
105+
@test tree.root.right.key == 5
106+
@test tree.root.right.left.key == 3
107+
108+
# left rotate
109+
# TODO
110+
111+
# left-right rotate
112+
# TODO
113+
114+
# right-left rotate
115+
# TODO
116+
117+
# tree that accepts any types
27118
tree = AVLTree()
28-
tree[1] = 1.1
29119
tree[2] = 'A'
120+
tree[1] = 1.1
30121
tree[3] = "test"
31-
@test tree[1] == 1.1
32122
@test tree[2] == 'A'
123+
@test tree[1] == 1.1
33124
@test tree[3] == "test"
125+
delete!(tree, 3)
126+
@test !isnothing(tree.root)
127+
@test !isnothing(tree.root.left)
128+
@test isnothing(tree.root.right)
129+
delete!(tree, 1)
130+
@test !isnothing(tree.root)
131+
@test isnothing(tree.root.left)
132+
@test isnothing(tree.root.right)
133+
delete!(tree, 2)
134+
@test isnothing(tree.root)
135+
136+
# AbstractTrees interface
137+
tree = AVLTree{Int,Int}()
138+
tree[3] = 30
139+
tree[2] = 20
140+
tree[4] = 40
141+
tree[1] = 10
142+
tree[5] = 50
143+
root = tree.root
144+
@test children(root) === (root.left, root.right)
145+
@test children(root.left) === (root.left.left,)
146+
@test children(root.right) === (root.right.right,)
147+
@test children(root.left.left) === ()
148+
@test children(root.right.right) === ()
149+
@test nodevalue(root) == 30
150+
@test nodevalue(root.left) == 20
151+
@test nodevalue(root.right) == 40
152+
@test nodevalue(root.left.left) == 10
153+
@test nodevalue(root.right.right) == 50
154+
@test NodeType(root) === HasNodeType()
155+
@test nodetype(root) === typeof(root)
34156

35157
# show
36158
tree = AVLTree{Int,Int}()
37159
@test sprint(show, MIME("text/plain"), tree) == "AVLTree()"
38-
tree[1] = 10
39-
tree[2] = 20
40160
tree[3] = 30
161+
tree[2] = 20
162+
tree[4] = 40
163+
tree[1] = 10
164+
tree[5] = 50
41165
@test sprint(show, MIME("text/plain"), tree) == """
42166
AVLTree
43-
2 => 20
44-
├─ 1 => 10
45-
└─ 3 => 30"""
167+
3 => 30
168+
├─ 2 => 20
169+
│ └─ 1 => 10
170+
└─ 4 => 40
171+
└─ 5 => 50"""
46172
end
47173
end

0 commit comments

Comments
 (0)