44
55namespace KaririCode \DataStructure \Map ;
66
7+ use KaririCode \Contract \DataStructure \Behavioral \IterableCollection ;
78use KaririCode \Contract \DataStructure \Map ;
89use KaririCode \DataStructure \TreeMapNode ;
910
2021 *
2122 * @see https://kariricode.org/
2223 */
23- class TreeMap implements Map
24+ class TreeMap implements Map, IterableCollection, \IteratorAggregate
2425{
2526 private ?TreeMapNode $ root = null ;
27+ private int $ size = 0 ;
2628
2729 public function put (mixed $ key , mixed $ value ): void
2830 {
2931 $ newNode = new TreeMapNode ($ key , $ value );
3032 if (null === $ this ->root ) {
3133 $ this ->root = $ newNode ;
3234 $ this ->root ->setBlack ();
35+ ++$ this ->size ;
3336 } else {
3437 $ this ->insertNode ($ newNode );
3538 $ this ->balanceAfterInsertion ($ newNode );
@@ -41,17 +44,90 @@ public function get(mixed $key): mixed
4144 return $ this ->findNode ($ key )?->value;
4245 }
4346
47+ public function keys (): array
48+ {
49+ $ keys = [];
50+ $ this ->inOrderTraversalKeys ($ this ->root , $ keys );
51+
52+ return $ keys ;
53+ }
54+
55+ public function values (): array
56+ {
57+ $ values = [];
58+ $ this ->inOrderTraversalValues ($ this ->root , $ values );
59+
60+ return $ values ;
61+ }
62+
4463 public function remove (mixed $ key ): bool
4564 {
4665 $ node = $ this ->findNode ($ key );
4766 if (null === $ node ) {
4867 return false ;
4968 }
5069 $ this ->deleteNode ($ node );
70+ --$ this ->size ;
5171
5272 return true ;
5373 }
5474
75+ public function size (): int
76+ {
77+ return $ this ->size ;
78+ }
79+
80+ public function clear (): void
81+ {
82+ $ this ->root = null ;
83+ $ this ->size = 0 ;
84+ }
85+
86+ public function containsKey (mixed $ key ): bool
87+ {
88+ return null !== $ this ->findNode ($ key );
89+ }
90+
91+ public function getItems (): array
92+ {
93+ $ items = [];
94+ $ this ->inOrderTraversal ($ this ->root , $ items );
95+
96+ return $ items ;
97+ }
98+
99+ public function getIterator (): \Iterator
100+ {
101+ return new \ArrayIterator ($ this ->getItems ());
102+ }
103+
104+ private function inOrderTraversalKeys (?TreeMapNode $ node , array &$ keys ): void
105+ {
106+ if (null !== $ node ) {
107+ $ this ->inOrderTraversalKeys ($ node ->left , $ keys );
108+ $ keys [] = $ node ->key ;
109+ $ this ->inOrderTraversalKeys ($ node ->right , $ keys );
110+ }
111+ }
112+
113+ private function inOrderTraversalValues (?TreeMapNode $ node , array &$ values ): void
114+ {
115+ if (null !== $ node ) {
116+ $ this ->inOrderTraversalValues ($ node ->left , $ values );
117+ $ values [] = $ node ->value ;
118+ $ this ->inOrderTraversalValues ($ node ->right , $ values );
119+ }
120+ }
121+
122+ private function inOrderTraversal (?TreeMapNode $ node , array &$ items ): void
123+ {
124+ if (null !== $ node ) {
125+ $ this ->inOrderTraversal ($ node ->left , $ items );
126+ $ items [$ node ->key ] = $ node ->value ;
127+ $ this ->inOrderTraversal ($ node ->right , $ items );
128+ }
129+ }
130+
55131 private function insertNode (TreeMapNode $ newNode ): void
56132 {
57133 $ current = $ this ->root ;
@@ -76,6 +152,8 @@ private function insertNode(TreeMapNode $newNode): void
76152 } else {
77153 $ parent ->right = $ newNode ;
78154 }
155+
156+ ++$ this ->size ;
79157 $ this ->balanceAfterInsertion ($ newNode );
80158 }
81159
0 commit comments