@@ -32,11 +32,9 @@ protected function setUp(): void
32
32
33
33
public function testTreeInitialization ()
34
34
{
35
- $ tree = new BSTree ();
36
-
37
- $ this ->assertNull ($ tree ->getRoot (), "Tree root should be null upon initialization. " );
38
- $ this ->assertEquals (0 , $ tree ->size (), "Tree size should be 0 upon initialization. " );
39
- $ this ->assertTrue ($ tree ->isEmpty (), "Tree should be empty upon initialization. " );
35
+ $ this ->assertNull ($ this ->tree ->getRoot (), "Tree root should be null upon initialization. " );
36
+ $ this ->assertEquals (0 , $ this ->tree ->size (), "Tree size should be 0 upon initialization. " );
37
+ $ this ->assertTrue ($ this ->tree ->isEmpty (), "Tree should be empty upon initialization. " );
40
38
}
41
39
42
40
/**
@@ -45,24 +43,27 @@ public function testTreeInitialization()
45
43
public function testInsertSingleNode (): void
46
44
{
47
45
$ this ->tree ->insert (10 , 'value10 ' );
48
- $ this ->assertNotNull ($ this ->tree ->getRoot ());
49
- $ this ->assertEquals (10 , $ this ->tree ->getRoot ()->key );
50
- $ this ->assertEquals ('value10 ' , $ this ->tree ->getRoot ()->value );
46
+ $ this ->assertNotNull ($ this ->tree ->getRoot (), "Tree root should not be null after inserting. " );
47
+ $ this ->assertEquals (10 , $ this ->tree ->getRoot ()->key , "Node key does not match the key inserted in node " );
48
+ $ this ->assertEquals (
49
+ 'value10 ' ,
50
+ $ this ->tree ->getRoot ()->value ,
51
+ "Node value does not match the value inserted in node "
52
+ );
51
53
}
52
54
53
55
/**
54
- * Test: Insert multiple nodes and validate structure
56
+ * Test: Insert multiple nodes and validate small structure
55
57
*/
56
58
public function testInsertMultipleNodes (): void
57
59
{
58
60
$ this ->tree ->insert (20 , 'value20 ' );
59
61
$ this ->tree ->insert (10 , 'value10 ' );
60
62
$ this ->tree ->insert (30 , 'value30 ' );
61
63
62
- // Check root and children
63
- $ this ->assertEquals (20 , $ this ->tree ->getRoot ()->key );
64
- $ this ->assertEquals (10 , $ this ->tree ->getRoot ()->left ->key );
65
- $ this ->assertEquals (30 , $ this ->tree ->getRoot ()->right ->key );
64
+ $ this ->assertEquals (20 , $ this ->tree ->getRoot ()->key , "Root node was not set properly. " );
65
+ $ this ->assertEquals (10 , $ this ->tree ->getRoot ()->left ->key , "Left node was not inserted properly " );
66
+ $ this ->assertEquals (30 , $ this ->tree ->getRoot ()->right ->key , "Right node was not inserted properly " );
66
67
}
67
68
68
69
/**
@@ -107,6 +108,25 @@ public function testIsEmpty()
107
108
$ this ->assertFalse ($ this ->tree ->isEmpty (), "Tree should not be empty. " );
108
109
}
109
110
111
+ /**
112
+ * Helper to populate the initialized tree for further tests
113
+ *
114
+ * The structure of the Binary Search Tree (BST) after insertion:
115
+ * *
116
+ * * 200
117
+ * * / \
118
+ * * 150 250
119
+ * * / \ / \
120
+ * * 140 170 220 300
121
+ * * / / \ / \ \
122
+ * * 130 160 180 215 230 360
123
+ * * / \ / \
124
+ * * 110 185 225 240
125
+ * * / \
126
+ * * 50 115
127
+ * * /
128
+ * * 70
129
+ */
110
130
private function populateTree (): void
111
131
{
112
132
$ this ->tree ->insert (200 , "Value 200 " );
@@ -156,7 +176,7 @@ public function testSearchNodeNotExists(): void
156
176
$ node = $ this ->tree ->search (444 );
157
177
$ isFound = $ this ->tree ->isFound ($ this ->tree ->getRoot (), 1500 );
158
178
159
- $ this ->assertNull ($ node , "Node with key 40 does not exist " );
179
+ $ this ->assertNull ($ node , "Node with key 444 does not exist " );
160
180
$ this ->assertFalse ($ isFound , "Node with key 1500 does not exist. " );
161
181
}
162
182
@@ -238,7 +258,11 @@ public function testRemoveNodeWithTwoChildren(): void
238
258
{
239
259
$ this ->populateTree ();
240
260
241
- $ this ->assertEquals (2 , $ this ->tree ->search (230 )->getChildrenCount (), "The node with key 230 has two children. " );
261
+ $ this ->assertEquals (
262
+ 2 ,
263
+ $ this ->tree ->search (230 )->getChildrenCount (),
264
+ "The node with key 230 has two children. "
265
+ );
242
266
243
267
$ parentNode = $ this ->tree ->search (230 )->parent ->key ; // 220
244
268
$ leftNode = $ this ->tree ->search (230 )->left ->key ; // 225
@@ -282,6 +306,32 @@ public function testRemoveNonExistingNode(): void
282
306
$ this ->assertNull ($ removedNode , "Node not found, Null should be returned. " );
283
307
}
284
308
309
+ /**
310
+ * Test: Verify all operations on a large tree.
311
+ */
312
+ public function testOperationsOnLargeTree (): void
313
+ {
314
+ for ($ i = 1 ; $ i <= 1000 ; $ i ++) {
315
+ $ this ->tree ->insert ($ i , "Value $ i " );
316
+ }
317
+
318
+ for ($ i = 1 ; $ i <= 1000 ; $ i ++) {
319
+ $ this ->assertEquals ("Value $ i " , $ this ->tree ->search ($ i )->value , "Value for key $ i should be 'Value $ i' " );
320
+ }
321
+
322
+ for ($ i = 1 ; $ i <= 1000 ; $ i ++) {
323
+ $ this ->assertTrue ($ this ->tree ->isFound ($ this ->tree ->getRoot (), $ i ), "Node with key $ i should exist " );
324
+ }
325
+
326
+ for ($ i = 1 ; $ i <= 5 ; $ i ++) {
327
+ $ this ->tree ->remove ($ i );
328
+ $ this ->assertFalse (
329
+ $ this ->tree ->isFound ($ this ->tree ->getRoot (), $ i ),
330
+ "Value for key $ i should be not exist after deletion "
331
+ );
332
+ }
333
+ }
334
+
285
335
/**
286
336
* Test: Check tree size
287
337
*/
@@ -320,7 +370,7 @@ public function testGetDepth(): void
320
370
$ this ->assertEquals (0 , $ this ->tree ->getdepth ($ root ), "The root node should have a depth of 0. " );
321
371
$ this ->assertEquals (1 , $ this ->tree ->getdepth ($ node150 ), "Node 150 should have a depth of 1. " );
322
372
$ this ->assertEquals (4 , $ this ->tree ->getdepth ($ node110 ), "Node 110 should have a depth of 4. " );
323
- $ this ->assertEquals (6 , $ this ->tree ->getdepth ($ node70 ), "Node 300 should have a depth of 6. " );
373
+ $ this ->assertEquals (6 , $ this ->tree ->getdepth ($ node70 ), "Node 70 should have a depth of 6. " );
324
374
}
325
375
326
376
/**
@@ -470,32 +520,6 @@ public function testSerializationAndDeserialization(): void
470
520
);
471
521
}
472
522
473
- /**
474
- * Test: Verify all operations on a large tree.
475
- */
476
- public function testLargeTree (): void
477
- {
478
- for ($ i = 1 ; $ i <= 1000 ; $ i ++) {
479
- $ this ->tree ->insert ($ i , "Value $ i " );
480
- }
481
-
482
- for ($ i = 1 ; $ i <= 1000 ; $ i ++) {
483
- $ this ->assertEquals ("Value $ i " , $ this ->tree ->search ($ i )->value , "Value for key $ i should be 'Value $ i' " );
484
- }
485
-
486
- for ($ i = 1 ; $ i <= 1000 ; $ i ++) {
487
- $ this ->assertTrue ($ this ->tree ->isFound ($ this ->tree ->getRoot (), $ i ), "Node with key $ i should exist " );
488
- }
489
-
490
- for ($ i = 1 ; $ i <= 5 ; $ i ++) {
491
- $ this ->tree ->remove ($ i );
492
- $ this ->assertFalse (
493
- $ this ->tree ->isFound ($ this ->tree ->getRoot (), $ i ),
494
- "Value for key $ i should be not exist after deletion "
495
- );
496
- }
497
- }
498
-
499
523
/**
500
524
* Provides traversal types and expected results for the iterator test.
501
525
*/
@@ -560,12 +584,12 @@ public function testIteratorWithTraversalTypes(string $traversalType, array $exp
560
584
$ this ->assertEquals (
561
585
$ expectedKeys [$ index ],
562
586
$ node ->key ,
563
- "Did not match the expected { $ traversalType} key. Failed tree iteration. "
587
+ "Did not match the expected $ traversalType key. Failed tree iteration. "
564
588
);
565
589
$ this ->assertEquals (
566
590
$ expectedValues [$ index ],
567
591
$ node ->value ,
568
- "Did not match the expected { $ traversalType} value. Failed tree iteration. "
592
+ "Did not match the expected $ traversalType value. Failed tree iteration. "
569
593
);
570
594
$ index ++;
571
595
}
0 commit comments