Skip to content

Commit 326e812

Browse files
committed
add size method to TeeMap class
1 parent 9523292 commit 326e812

File tree

4 files changed

+152
-5
lines changed

4 files changed

+152
-5
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Map/TreeMap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
class TreeMap implements Map
2424
{
2525
private ?TreeMapNode $root = null;
26+
private int $size = 0;
2627

2728
public function put(mixed $key, mixed $value): void
2829
{
2930
$newNode = new TreeMapNode($key, $value);
3031
if (null === $this->root) {
3132
$this->root = $newNode;
3233
$this->root->setBlack();
34+
$this->size++;
3335
} else {
3436
$this->insertNode($newNode);
3537
$this->balanceAfterInsertion($newNode);
@@ -48,10 +50,16 @@ public function remove(mixed $key): bool
4850
return false;
4951
}
5052
$this->deleteNode($node);
53+
$this->size--;
5154

5255
return true;
5356
}
5457

58+
public function size(): int
59+
{
60+
return $this->size;
61+
}
62+
5563
private function insertNode(TreeMapNode $newNode): void
5664
{
5765
$current = $this->root;
@@ -76,6 +84,8 @@ private function insertNode(TreeMapNode $newNode): void
7684
} else {
7785
$parent->right = $newNode;
7886
}
87+
88+
$this->size++;
7989
$this->balanceAfterInsertion($newNode);
8090
}
8191

src/Tree/TreeSet.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\DataStructure\Set;
6+
7+
use KaririCode\Contract\DataStructure\Set;
8+
use KaririCode\DataStructure\Map\TreeMap;
9+
10+
/**
11+
* TreeSet implementation.
12+
*
13+
* This class implements a set using a TreeMap to store elements. It provides O(log n) time complexity
14+
* for add, remove, and contains operations.
15+
*
16+
* @category Sets
17+
*
18+
* @package KaririCode\DataStructure\Set
19+
*
20+
* @author Walmir Silva <walmir.silva@kariricode.org>
21+
* @license MIT
22+
*
23+
* @see https://kariricode.org/
24+
*/
25+
class TreeSet implements Set
26+
{
27+
private TreeMap $map;
28+
29+
public function __construct()
30+
{
31+
$this->map = new TreeMap();
32+
}
33+
34+
/**
35+
* Adds an element to the set.
36+
*
37+
* @param mixed $element The element to add
38+
*
39+
* @return bool True if the element was added, false if it was already present
40+
*/
41+
public function add(mixed $element): bool
42+
{
43+
if ($this->map->get($element) !== null) {
44+
return false;
45+
}
46+
$this->map->put($element, true);
47+
return true;
48+
}
49+
50+
/**
51+
* Removes an element from the set.
52+
*
53+
* @param mixed $element The element to remove
54+
*
55+
* @return bool True if the element was removed, false if it was not present
56+
*/
57+
public function remove(mixed $element): bool
58+
{
59+
return $this->map->remove($element);
60+
}
61+
62+
/**
63+
* Checks if the set contains a specific element.
64+
*
65+
* @param mixed $element The element to check for
66+
*
67+
* @return bool True if the element is present, false otherwise
68+
*/
69+
public function contains(mixed $element): bool
70+
{
71+
return $this->map->get($element) !== null;
72+
}
73+
74+
/**
75+
* Removes all elements from the set.
76+
*/
77+
public function clear(): void
78+
{
79+
$this->map = new TreeMap();
80+
}
81+
82+
/**
83+
* Returns the number of elements in the set.
84+
*
85+
* @return int The number of elements in the set
86+
*/
87+
public function size(): int
88+
{
89+
return $this->map->size();
90+
}
91+
92+
/**
93+
* Checks if the set is empty.
94+
*
95+
* @return bool True if the set is empty, false otherwise
96+
*/
97+
public function isEmpty(): bool
98+
{
99+
return $this->size() === 0;
100+
}
101+
102+
/**
103+
* Returns an array containing all elements in the set.
104+
*
105+
* @return array The elements in the set
106+
*/
107+
public function toArray(): array
108+
{
109+
$elements = [];
110+
foreach ($this->map as $key => $value) {
111+
$elements[] = $key;
112+
}
113+
return $elements;
114+
}
115+
}

tests/Map/TreeMapTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ public function testBalancingAfterRemoval(): void
137137
$this->assertStringContainsString('15:fifteen [RED]', $treeStructure);
138138
}
139139

140+
// test size
141+
public function testSize(): void
142+
{
143+
$this->assertSame(0, $this->treeMap->size(), 'Initial size should be 0');
144+
145+
$this->treeMap->put(1, 'one');
146+
$this->assertSame(1, $this->treeMap->size(), 'Size should be 1 after adding one element');
147+
148+
$this->treeMap->put(2, 'two');
149+
$this->assertSame(2, $this->treeMap->size(), 'Size should be 2 after adding another element');
150+
151+
$this->treeMap->put(1, 'one updated');
152+
$this->assertSame(2, $this->treeMap->size(), 'Size should remain 2 after updating an existing element');
153+
154+
$this->treeMap->remove(1);
155+
$this->assertSame(1, $this->treeMap->size(), 'Size should be 1 after removing one element');
156+
157+
$this->treeMap->remove(2);
158+
$this->assertSame(0, $this->treeMap->size(), 'Size should be 0 after removing all elements');
159+
}
160+
161+
140162
// Test complex operations on the tree.
141163
public function testComplexOperations(): void
142164
{

0 commit comments

Comments
 (0)