Skip to content

Refactor and enhance data structures #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/Collection/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,27 @@ public function remove(mixed $element): bool

public function contains(mixed $element): bool
{
return in_array($element, $this->elements, true);
return null !== $this->find($element);
}

public function clear(): void
public function find(mixed $element): ?int
{
$this->elements = [];
}
$index = array_search($element, $this->elements, true);

public function isEmpty(): bool
{
return empty($this->elements);
return false !== $index ? $index : null;
}

public function getItems(): array
{
return $this->elements;
}

public function count(): int
public function clear(): void
{
$this->elements = [];
}

public function size(): int
{
return count($this->elements);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Collection/LinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,21 @@ public function remove(mixed $element): bool
}

public function contains(mixed $element): bool
{
return null !== $this->find($element);
}

public function find(mixed $element): ?Node
{
$current = $this->head;
while (null !== $current) {
if ($current->data === $element) {
return true;
return $current;
}
$current = $current->next;
}

return false;
return null;
}

public function clear(): void
Expand All @@ -107,7 +112,7 @@ public function getItems(): array
return $items;
}

public function count(): int
public function size(): int
{
return $this->size;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Map/HashMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace KaririCode\DataStructure\Map;

use KaririCode\Contract\DataStructure\Behavioral\IterableCollection;
use KaririCode\Contract\DataStructure\Map;

/**
Expand All @@ -19,7 +20,7 @@
*
* @see https://kariricode.org/
*/
class HashMap implements Map
class HashMap implements Map, IterableCollection, \IteratorAggregate
{
private array $map = [];

Expand Down Expand Up @@ -68,4 +69,9 @@ public function values(): array
{
return array_values($this->map);
}

public function getIterator(): \Iterator
{
return new \ArrayIterator($this->map);
}
}
80 changes: 79 additions & 1 deletion src/Map/TreeMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace KaririCode\DataStructure\Map;

use KaririCode\Contract\DataStructure\Behavioral\IterableCollection;
use KaririCode\Contract\DataStructure\Map;
use KaririCode\DataStructure\TreeMapNode;

Expand All @@ -20,16 +21,18 @@
*
* @see https://kariricode.org/
*/
class TreeMap implements Map
class TreeMap implements Map, IterableCollection, \IteratorAggregate
{
private ?TreeMapNode $root = null;
private int $size = 0;

public function put(mixed $key, mixed $value): void
{
$newNode = new TreeMapNode($key, $value);
if (null === $this->root) {
$this->root = $newNode;
$this->root->setBlack();
++$this->size;
} else {
$this->insertNode($newNode);
$this->balanceAfterInsertion($newNode);
Expand All @@ -41,17 +44,90 @@ public function get(mixed $key): mixed
return $this->findNode($key)?->value;
}

public function keys(): array
{
$keys = [];
$this->inOrderTraversalKeys($this->root, $keys);

return $keys;
}

public function values(): array
{
$values = [];
$this->inOrderTraversalValues($this->root, $values);

return $values;
}

public function remove(mixed $key): bool
{
$node = $this->findNode($key);
if (null === $node) {
return false;
}
$this->deleteNode($node);
--$this->size;

return true;
}

public function size(): int
{
return $this->size;
}

public function clear(): void
{
$this->root = null;
$this->size = 0;
}

public function containsKey(mixed $key): bool
{
return null !== $this->findNode($key);
}

public function getItems(): array
{
$items = [];
$this->inOrderTraversal($this->root, $items);

return $items;
}

public function getIterator(): \Iterator
{
return new \ArrayIterator($this->getItems());
}

private function inOrderTraversalKeys(?TreeMapNode $node, array &$keys): void
{
if (null !== $node) {
$this->inOrderTraversalKeys($node->left, $keys);
$keys[] = $node->key;
$this->inOrderTraversalKeys($node->right, $keys);
}
}

private function inOrderTraversalValues(?TreeMapNode $node, array &$values): void
{
if (null !== $node) {
$this->inOrderTraversalValues($node->left, $values);
$values[] = $node->value;
$this->inOrderTraversalValues($node->right, $values);
}
}

private function inOrderTraversal(?TreeMapNode $node, array &$items): void
{
if (null !== $node) {
$this->inOrderTraversal($node->left, $items);
$items[$node->key] = $node->value;
$this->inOrderTraversal($node->right, $items);
}
}

private function insertNode(TreeMapNode $newNode): void
{
$current = $this->root;
Expand All @@ -76,6 +152,8 @@ private function insertNode(TreeMapNode $newNode): void
} else {
$parent->right = $newNode;
}

++$this->size;
$this->balanceAfterInsertion($newNode);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Queue/ArrayDeque.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace KaririCode\DataStructure\Queue;

use KaririCode\Contract\DataStructure\Deque;
use KaririCode\Contract\DataStructure\Queue;

/**
* ArrayDeque implementation.
Expand Down Expand Up @@ -52,4 +51,9 @@ public function peekLast(): mixed

return $this->elements[$index];
}

public function add(mixed $element): void
{
$this->addLast($element);
}
}
5 changes: 4 additions & 1 deletion src/Queue/ArrayQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
*/
class ArrayQueue extends CircularArrayQueue implements Queue
{
// No additional methods required, uses methods from CircularArrayQueue
public function add(mixed $element): void
{
$this->enqueue($element);
}
}
29 changes: 29 additions & 0 deletions src/Queue/CircularArrayQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public function enqueue(mixed $element): void
++$this->size;
}

public function addLast(mixed $element): void
{
$this->enqueue($element);
}

public function removeFirst(): mixed
{
return $this->dequeue();
}

public function getItems(): array
{
$items = [];
Expand All @@ -97,4 +107,23 @@ public function getItems(): array

return $items;
}

public function remove(mixed $element): bool
{
for ($i = 0; $i < $this->size; ++$i) {
$index = ($this->front + $i) % $this->capacity;
if ($this->elements[$index] === $element) {
// Shift elements to the left to fill the gap
for ($j = $index; $j !== ($this->front + $this->size - 1) % $this->capacity; $j = ($j + 1) % $this->capacity) {
$this->elements[$j] = $this->elements[($j + 1) % $this->capacity];
}
$this->elements[($this->front + $this->size - 1) % $this->capacity] = null;
--$this->size;

return true;
}
}

return false;
}
}
Loading
Loading