Skip to content

Commit b84578a

Browse files
Merge branch 'features/bstree-implementation'
2 parents 160a25b + 1e48262 commit b84578a

File tree

1 file changed

+68
-44
lines changed

1 file changed

+68
-44
lines changed

tests/DataStructures/BSTreeTest.php

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ protected function setUp(): void
3232

3333
public function testTreeInitialization()
3434
{
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.");
4038
}
4139

4240
/**
@@ -45,24 +43,27 @@ public function testTreeInitialization()
4543
public function testInsertSingleNode(): void
4644
{
4745
$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+
);
5153
}
5254

5355
/**
54-
* Test: Insert multiple nodes and validate structure
56+
* Test: Insert multiple nodes and validate small structure
5557
*/
5658
public function testInsertMultipleNodes(): void
5759
{
5860
$this->tree->insert(20, 'value20');
5961
$this->tree->insert(10, 'value10');
6062
$this->tree->insert(30, 'value30');
6163

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");
6667
}
6768

6869
/**
@@ -107,6 +108,25 @@ public function testIsEmpty()
107108
$this->assertFalse($this->tree->isEmpty(), "Tree should not be empty.");
108109
}
109110

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+
*/
110130
private function populateTree(): void
111131
{
112132
$this->tree->insert(200, "Value 200");
@@ -156,7 +176,7 @@ public function testSearchNodeNotExists(): void
156176
$node = $this->tree->search(444);
157177
$isFound = $this->tree->isFound($this->tree->getRoot(), 1500);
158178

159-
$this->assertNull($node, "Node with key 40 does not exist");
179+
$this->assertNull($node, "Node with key 444 does not exist");
160180
$this->assertFalse($isFound, "Node with key 1500 does not exist.");
161181
}
162182

@@ -238,7 +258,11 @@ public function testRemoveNodeWithTwoChildren(): void
238258
{
239259
$this->populateTree();
240260

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+
);
242266

243267
$parentNode = $this->tree->search(230)->parent->key; // 220
244268
$leftNode = $this->tree->search(230)->left->key; // 225
@@ -282,6 +306,32 @@ public function testRemoveNonExistingNode(): void
282306
$this->assertNull($removedNode, "Node not found, Null should be returned.");
283307
}
284308

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+
285335
/**
286336
* Test: Check tree size
287337
*/
@@ -320,7 +370,7 @@ public function testGetDepth(): void
320370
$this->assertEquals(0, $this->tree->getdepth($root), "The root node should have a depth of 0.");
321371
$this->assertEquals(1, $this->tree->getdepth($node150), "Node 150 should have a depth of 1.");
322372
$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.");
324374
}
325375

326376
/**
@@ -470,32 +520,6 @@ public function testSerializationAndDeserialization(): void
470520
);
471521
}
472522

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-
499523
/**
500524
* Provides traversal types and expected results for the iterator test.
501525
*/
@@ -560,12 +584,12 @@ public function testIteratorWithTraversalTypes(string $traversalType, array $exp
560584
$this->assertEquals(
561585
$expectedKeys[$index],
562586
$node->key,
563-
"Did not match the expected {$traversalType} key. Failed tree iteration."
587+
"Did not match the expected $traversalType key. Failed tree iteration."
564588
);
565589
$this->assertEquals(
566590
$expectedValues[$index],
567591
$node->value,
568-
"Did not match the expected {$traversalType} value. Failed tree iteration."
592+
"Did not match the expected $traversalType value. Failed tree iteration."
569593
);
570594
$index++;
571595
}

0 commit comments

Comments
 (0)