Skip to content

Commit 9523292

Browse files
committed
Implement TreeMap and TreeMapNode with comprehensive test coverage
- Create TreeMap implementation using a self-balancing binary search tree (Red-Black Tree) - Create TreeMapNode to support TreeMap structure with methods for managing node properties - Implement put, get, remove, and balancing methods in TreeMap - Add comprehensive unit tests for TreeMapNode ensuring 100% coverage - Add extensive test cases for TreeMap including insertion, deletion, balancing, and edge cases - Ensure proper behavior of TreeMap operations through detailed test scenarios - Verify usage of TreeMapNode methods within TreeMap implementation
1 parent 01d07f3 commit 9523292

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/TreeMapNode.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
namespace KaririCode\DataStructure;
66

7+
/**
8+
* TreeMapNode class.
9+
*
10+
* This class represents a node in a Red-Black Tree used by TreeMap.
11+
* Each node contains a key, a value, a color (red or black), and references to its left, right, and parent nodes.
12+
*
13+
* @category Data Structures
14+
*
15+
* @author Walmir Silva <walmir.silva@kariricode.org>
16+
* @license MIT
17+
*
18+
* @see https://kariricode.org/
19+
*/
720
class TreeMapNode
821
{
922
public const RED = true;

tests/Map/TreeMapTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function testComplexOperations(): void
147147
[1, 'one'],
148148
[3, 'three'],
149149
[6, 'six'],
150-
[8, 'eight']
150+
[8, 'eight'],
151151
];
152152

153153
foreach ($operations as [$key, $value]) {
@@ -297,6 +297,7 @@ private function getRootNode(): ?TreeMapNode
297297
$reflection = new \ReflectionClass($this->treeMap);
298298
$rootProperty = $reflection->getProperty('root');
299299
$rootProperty->setAccessible(true);
300+
300301
return $rootProperty->getValue($this->treeMap);
301302
}
302303

@@ -314,9 +315,10 @@ private function countNodes(): int
314315
// Recursive method to count nodes in the tree.
315316
private function countNodesRecursive(?TreeMapNode $node): int
316317
{
317-
if ($node === null) {
318+
if (null === $node) {
318319
return 0;
319320
}
321+
320322
return 1 + $this->countNodesRecursive($node->left) + $this->countNodesRecursive($node->right);
321323
}
322324

@@ -334,13 +336,13 @@ private function getTreeStructure(): string
334336
// Recursive method to print the tree structure.
335337
private function printTree(?TreeMapNode $node, string $prefix = '', bool $isLeft = true): string
336338
{
337-
if ($node === null) {
339+
if (null === $node) {
338340
return '';
339341
}
340342

341343
$result = $prefix;
342344

343-
if ($prefix === '') {
345+
if ('' === $prefix) {
344346
$result .= '├── ';
345347
} else {
346348
$result .= $isLeft ? '├── ' : '└── ';

0 commit comments

Comments
 (0)