1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use DataStructures\Trees\AVLTree; |
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
|
6
|
|
View Code Duplication |
class AVLTreeTest extends TestCase { |
|
|
|
|
7
|
|
|
private $tree; |
8
|
|
|
|
9
|
|
|
public function setUp() { |
10
|
|
|
$this->tree = new AVLTree(); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function testPut() { |
14
|
|
|
$this->tree->put(24, "Siro"); |
15
|
|
|
$this->tree->put(19, "Clara"); |
16
|
|
|
$this->tree->put(51, "Elisa"); |
17
|
|
|
$this->assertEquals(3, $this->tree->size()); |
18
|
|
|
$this->assertEquals("Siro", $this->tree->get(24)); |
19
|
|
|
$this->assertEquals("Clara", $this->tree->get(19)); |
20
|
|
|
$this->assertEquals("Elisa", $this->tree->get(51)); |
21
|
|
|
$this->assertNull($this->tree->get(100)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testPutWithUpdate() { |
25
|
|
|
$this->tree->put(24, "Siro"); |
26
|
|
|
$this->tree->put(19, "Clara"); |
27
|
|
|
$this->tree->put(51, "Elisa"); |
28
|
|
|
$this->assertEquals(3, $this->tree->size()); |
29
|
|
|
$this->tree->put(51, "Pepe", true); |
30
|
|
|
$this->tree->put(24, "John", true); |
31
|
|
|
$this->assertEquals("John", $this->tree->get(24)); |
32
|
|
|
$this->assertEquals("Clara", $this->tree->get(19)); |
33
|
|
|
$this->assertEquals("Pepe", $this->tree->get(51)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testPutOrUpdate() { |
37
|
|
|
$this->tree->put(24, "Siro"); |
38
|
|
|
$this->tree->put(19, "Clara"); |
39
|
|
|
$this->tree->put(51, "Elisa"); |
40
|
|
|
$this->assertEquals(3, $this->tree->size()); |
41
|
|
|
$this->tree->putOrUpdate(24, "John", true); |
|
|
|
|
42
|
|
|
$this->assertEquals(3, $this->tree->size()); |
43
|
|
|
$this->tree->putOrUpdate(34, "Ana"); |
44
|
|
|
$this->assertEquals(4, $this->tree->size()); |
45
|
|
|
$this->assertEquals("John", $this->tree->get(24)); |
46
|
|
|
$this->assertEquals("Clara", $this->tree->get(19)); |
47
|
|
|
$this->assertEquals("Elisa", $this->tree->get(51)); |
48
|
|
|
$this->assertEquals("Ana", $this->tree->get(34)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testEmpty() { |
52
|
|
|
$this->assertTrue($this->tree->empty()); |
53
|
|
|
$this->tree->put(24, "Siro"); |
54
|
|
|
$this->tree->put(19, "Clara"); |
55
|
|
|
$this->tree->put(51, "Elisa"); |
56
|
|
|
$this->assertFalse($this->tree->empty()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSize() { |
60
|
|
|
$this->assertEquals(0, $this->tree->size()); |
61
|
|
|
$this->tree->put(24, "Siro"); |
62
|
|
|
$this->assertEquals(1, $this->tree->size()); |
63
|
|
|
$this->tree->put(19, "Clara"); |
64
|
|
|
$this->assertEquals(2, $this->tree->size()); |
65
|
|
|
$this->tree->put(51, "Elisa"); |
66
|
|
|
$this->assertEquals(3, $this->tree->size()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testExists() { |
70
|
|
|
$this->assertFalse($this->tree->exists("greet")); |
71
|
|
|
$this->tree->put("greet", "saludo"); |
72
|
|
|
$this->tree->put("tree", "árbol"); |
73
|
|
|
$this->tree->put("water", "agua"); |
74
|
|
|
$this->assertTrue($this->tree->exists("greet")); |
75
|
|
|
$this->assertTrue($this->tree->exists("tree")); |
76
|
|
|
$this->assertTrue($this->tree->exists("water")); |
77
|
|
|
$this->assertFalse($this->tree->exists("sun")); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testSearch() { |
81
|
|
|
$this->tree->put("greet", "saludo"); |
82
|
|
|
$this->tree->put("tree", "árbol"); |
83
|
|
|
$this->tree->put("water", "agua"); |
84
|
|
|
$this->assertEquals("greet", $this->tree->search("greet")->key); |
85
|
|
|
$this->assertEquals("árbol", $this->tree->search("tree")->data); |
86
|
|
|
$this->assertEquals("water", $this->tree->search("water")->key); |
87
|
|
|
$this->assertNull($this->tree->search(100)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testMin() { |
91
|
|
|
$this->tree->put(2, "two"); |
92
|
|
|
$this->tree->put(3, "three"); |
93
|
|
|
$this->assertEquals(2, $this->tree->min()->key); |
94
|
|
|
$this->tree->put(1, "one"); |
95
|
|
|
$this->assertEquals(1, $this->tree->min()->key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testMax() { |
99
|
|
|
$this->tree->put(1, "one"); |
100
|
|
|
$this->assertEquals(1, $this->tree->max()->key); |
101
|
|
|
$this->tree->put(2, "two"); |
102
|
|
|
$this->assertEquals(2, $this->tree->max()->key); |
103
|
|
|
$this->tree->put(3, "three"); |
104
|
|
|
$this->assertEquals(3, $this->tree->max()->key); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testDelete() { |
108
|
|
|
$this->tree->put(1, "one"); |
109
|
|
|
$this->tree->put(2, "two"); |
110
|
|
|
$this->tree->put(3, "three"); |
111
|
|
|
$this->assertEquals(1, $this->tree->delete(1)->key); |
|
|
|
|
112
|
|
|
$this->assertEquals(2, $this->tree->size()); |
113
|
|
|
$this->assertEquals("three", $this->tree->delete(3)->data); |
|
|
|
|
114
|
|
|
$this->assertEquals(1, $this->tree->size()); |
115
|
|
|
$this->assertEquals(2, $this->tree->delete(2)->key); |
|
|
|
|
116
|
|
|
$this->assertEquals(0, $this->tree->size()); |
117
|
|
|
$this->assertNull($this->tree->delete(2)); |
118
|
|
|
$this->assertTrue($this->tree->empty()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testDeleteMin() { |
122
|
|
|
$this->tree->put(1, "one"); |
123
|
|
|
$this->tree->put(2, "two"); |
124
|
|
|
$this->tree->put(3, "three"); |
125
|
|
|
$this->assertEquals(1, $this->tree->deleteMin()->key); |
|
|
|
|
126
|
|
|
$this->assertEquals(2, $this->tree->size()); |
127
|
|
|
$this->assertEquals(2, $this->tree->deleteMin()->key); |
|
|
|
|
128
|
|
|
$this->assertEquals(1, $this->tree->size()); |
129
|
|
|
$this->tree->put(100, "hundred"); |
130
|
|
|
$this->assertEquals(2, $this->tree->size()); |
131
|
|
|
$this->assertEquals(3, $this->tree->deleteMin()->key); |
|
|
|
|
132
|
|
|
$this->assertEquals(1, $this->tree->size()); |
133
|
|
|
$this->assertEquals(100, $this->tree->deleteMin()->key); |
|
|
|
|
134
|
|
|
$this->assertEquals(0, $this->tree->size()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testDeleteMax() { |
138
|
|
|
$this->tree->put(1, "one"); |
139
|
|
|
$this->tree->put(2, "two"); |
140
|
|
|
$this->tree->put(3, "three"); |
141
|
|
|
$this->assertEquals(3, $this->tree->deleteMax()->key); |
|
|
|
|
142
|
|
|
$this->assertEquals(2, $this->tree->size()); |
143
|
|
|
$this->assertEquals(2, $this->tree->deleteMax()->key); |
|
|
|
|
144
|
|
|
$this->assertEquals(1, $this->tree->size()); |
145
|
|
|
$this->tree->put(100, "hundred"); |
146
|
|
|
$this->assertEquals(2, $this->tree->size()); |
147
|
|
|
$this->assertEquals(100, $this->tree->deleteMax()->key); |
|
|
|
|
148
|
|
|
$this->assertEquals(1, $this->tree->size()); |
149
|
|
|
$this->assertEquals(1, $this->tree->deleteMax()->key); |
|
|
|
|
150
|
|
|
$this->assertEquals(0, $this->tree->size()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testPreorder() { |
154
|
|
|
$this->tree->put(2, "two"); |
155
|
|
|
$this->tree->put(1, "one"); |
156
|
|
|
$this->tree->put(3, "three"); |
157
|
|
|
$this->tree->preorder(function ($node) { |
|
|
|
|
158
|
|
|
// do something with the node |
159
|
|
|
// echo $node->key . PHP_EOL; |
|
|
|
|
160
|
|
|
}); |
161
|
|
|
$this->assertTrue(true); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testInorder() { |
165
|
|
|
$this->tree->put(2, "two"); |
166
|
|
|
$this->tree->put(1, "one"); |
167
|
|
|
$this->tree->put(3, "three"); |
168
|
|
|
$this->tree->inorder(function ($node) { |
|
|
|
|
169
|
|
|
// do something with the node |
170
|
|
|
// echo $node->key . PHP_EOL; |
|
|
|
|
171
|
|
|
}); |
172
|
|
|
$this->assertTrue(true); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testPostorder() { |
176
|
|
|
$this->tree->put(2, "two"); |
177
|
|
|
$this->tree->put(1, "one"); |
178
|
|
|
$this->tree->put(3, "three"); |
179
|
|
|
$this->tree->postorder(function ($node) { |
|
|
|
|
180
|
|
|
// do something with the node |
181
|
|
|
// echo $node->key . PHP_EOL; |
|
|
|
|
182
|
|
|
}); |
183
|
|
|
$this->assertTrue(true); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testIsLeaf() { |
187
|
|
|
$this->tree->put(2, "two"); |
188
|
|
|
$this->tree->put(1, "one"); |
189
|
|
|
$this->tree->put(3, "three"); |
190
|
|
|
|
191
|
|
|
$this->assertTrue($this->tree->isLeaf($this->tree->search(3))); |
192
|
|
|
$this->assertFalse($this->tree->isLeaf($this->tree->search(2))); |
193
|
|
|
$this->assertTrue($this->tree->isLeaf($this->tree->search(1))); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testIsRoot() { |
197
|
|
|
$this->tree->put(2, "two"); |
198
|
|
|
$this->tree->put(1, "one"); |
199
|
|
|
$this->tree->put(3, "three"); |
200
|
|
|
|
201
|
|
|
$this->assertTrue($this->tree->isRoot($this->tree->search(2))); |
202
|
|
|
$this->assertFalse($this->tree->isRoot($this->tree->search(3))); |
203
|
|
|
$this->assertFalse($this->tree->isRoot($this->tree->search(1))); |
204
|
|
|
} |
205
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.