Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
643dfa2
Added Disjoint Sets Data structure
Ramy-Badr-Ahmed Aug 24, 2024
7cfc859
Moved DisjointSetTest.php to tests/DataStructures
Ramy-Badr-Ahmed Sep 8, 2024
b3a8d7a
Update DataStructures/DisjointSets/DisjointSet.php
Ramy-Badr-Ahmed Sep 11, 2024
b6ae103
Update DataStructures/DisjointSets/DisjointSetNode.php
Ramy-Badr-Ahmed Sep 11, 2024
70d981a
Update DataStructures/DisjointSets/DisjointSetNode.php
Ramy-Badr-Ahmed Sep 11, 2024
dc93e41
Update tests/DataStructures/DisjointSetTest.php
Ramy-Badr-Ahmed Sep 11, 2024
35aa7d0
Update tests/DataStructures/DisjointSetTest.php
Ramy-Badr-Ahmed Sep 11, 2024
0591a6f
Update tests/DataStructures/DisjointSetTest.php
Ramy-Badr-Ahmed Sep 11, 2024
b0086fd
Considered PHPCS remarks. Unit Testing is now working.
Ramy-Badr-Ahmed Sep 11, 2024
b2d1160
Remove data type mixed. Considered annotations for php7.4.
Ramy-Badr-Ahmed Sep 11, 2024
926126a
Remove data type mixed. Considered annotations for php7.4.
Ramy-Badr-Ahmed Sep 11, 2024
355c5ba
updating DIRECTORY.md
Ramy-Badr-Ahmed Sep 11, 2024
99e489e
Merge branch 'TheAlgorithms:master' into master
Ramy-Badr-Ahmed Sep 11, 2024
db5bfb2
Merge branch 'TheAlgorithms:master' into master
Ramy-Badr-Ahmed Sep 14, 2024
bc44199
Implemented Trie DataStructure
Ramy-Badr-Ahmed Sep 14, 2024
b056238
Added Trie to DIRECTORY.md
Ramy-Badr-Ahmed Sep 14, 2024
5a02302
updating DIRECTORY.md
Ramy-Badr-Ahmed Sep 14, 2024
9c76b4b
Implemented AVLTree DataStructure
Ramy-Badr-Ahmed Sep 14, 2024
5330978
Merge branch 'features/avl-tree-implementation'
Ramy-Badr-Ahmed Sep 14, 2024
d258e77
updating DIRECTORY.md
Ramy-Badr-Ahmed Sep 14, 2024
9766ed8
Implemented AVLTree DataStructure
Ramy-Badr-Ahmed Sep 14, 2024
f701b1b
Merge branch 'features/avl-tree-implementation'
Ramy-Badr-Ahmed Sep 14, 2024
5f74e0e
Merge branch 'TheAlgorithms:master' into master
Ramy-Badr-Ahmed Sep 18, 2024
a8ca30f
Implemented SegmentTreeNode.php
Ramy-Badr-Ahmed Sep 21, 2024
ba43a0d
Implementing SegmentTree
Ramy-Badr-Ahmed Sep 22, 2024
ee1c6f6
Implementing SegmentTree with updateTree
Ramy-Badr-Ahmed Sep 22, 2024
6912710
Implementing SegmentTree with rangeUpdateTree
Ramy-Badr-Ahmed Sep 22, 2024
4d951d1
Implementing SegmentTree with query and queryTree
Ramy-Badr-Ahmed Sep 22, 2024
f741301
Added serializing and deserializing of the SegmentTree
Ramy-Badr-Ahmed Sep 22, 2024
4cef497
Adding unit tests SegmentTree implementation
Ramy-Badr-Ahmed Sep 22, 2024
be41540
Added unit tests for SegmentTree updates and range updates
Ramy-Badr-Ahmed Sep 22, 2024
0fed0ad
considering PHPCS for Added unit tests for SegmentTree updates and ra…
Ramy-Badr-Ahmed Sep 22, 2024
850fc4b
Added unit tests for SegmentTree serialization/deserialization and ar…
Ramy-Badr-Ahmed Sep 22, 2024
eca9709
Added unit tests for SegmentTree Edge Cases
Ramy-Badr-Ahmed Sep 22, 2024
628fc88
Added unit tests for SegmentTree Exceptions (OutOfBoundsException, In…
Ramy-Badr-Ahmed Sep 22, 2024
7d0c07d
Added SegmentTree to DIRECTORY.md
Ramy-Badr-Ahmed Sep 22, 2024
d61d023
Implemented Segment Tree Data Structure
Ramy-Badr-Ahmed Sep 22, 2024
eaa6bb7
updating DIRECTORY.md
Ramy-Badr-Ahmed Sep 22, 2024
f0acc06
Added some comments to my files in: #160, #162, #163, #166. Implement…
Ramy-Badr-Ahmed Sep 28, 2024
4eb689c
Merge branch 'features/segment-tree-implementation'
Ramy-Badr-Ahmed Sep 28, 2024
8957dd3
Added some comments to my files in: #160, #162, #163, #166. Implement…
Ramy-Badr-Ahmed Sep 28, 2024
e4cadc8
Merge branch 'features/segment-tree-implementation'
Ramy-Badr-Ahmed Sep 28, 2024
98a41ee
Added comments time complexity for query(), update() and buildTree()
Ramy-Badr-Ahmed Sep 30, 2024
6c64c35
Merge branch 'features/segment-tree-implementation'
Ramy-Badr-Ahmed Sep 30, 2024
ff94feb
Merge branch 'TheAlgorithms:master' into master
Ramy-Badr-Ahmed Oct 1, 2024
65c4849
Implemented Splay Tree Data Structure
Ramy-Badr-Ahmed Oct 3, 2024
5bb6777
Update tests/DataStructures/SplayTreeTest.php
Ramy-Badr-Ahmed Oct 3, 2024
8c6dd2c
Merge branch 'TheAlgorithms:master' into features/splay-tree-implemen…
Ramy-Badr-Ahmed Oct 3, 2024
d11e757
Merge branch 'TheAlgorithms:master' into master
Ramy-Badr-Ahmed Oct 7, 2024
e145894
Implemented AVL Tree Data Structure. Added serialization/deserializat…
Ramy-Badr-Ahmed Oct 12, 2024
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
Prev Previous commit
Next Next commit
Implemented Trie DataStructure
  • Loading branch information
Ramy-Badr-Ahmed committed Sep 14, 2024
commit bc441995ae335dd80fe6ae7c08417f4421685733
163 changes: 163 additions & 0 deletions DataStructures/Trie/Trie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace DataStructures\Trie;

class Trie
{
private TrieNode $root;

public function __construct()
{
$this->root = new TrieNode();
}

/**
* Get the root node of the Trie.
*/
public function getRoot(): TrieNode
{
return $this->root;
}

/**
* Insert a word into the Trie.
*/
public function insert(string $word): void
{
$node = $this->root;
for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
$node = $node->addChild($char);
}
$node->isEndOfWord = true;
}

/**
* Search for a word in the Trie.
*/
public function search(string $word): bool
{
$node = $this->root;
for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
if (!$node->hasChild($char)) {
return false;
}
$node = $node->getChild($char);
}
return $node->isEndOfWord;
}

/**
* Find all words that start with a given prefix.
*/
public function startsWith(string $prefix): array
{
$node = $this->root;
for ($i = 0; $i < strlen($prefix); $i++) {
$char = $prefix[$i];
if (!$node->hasChild($char)) {
return [];
}
$node = $node->getChild($char);
}
return $this->findWordsFromNode($node, $prefix);
}

/**
* Helper function to find all words from a given node.
*/
private function findWordsFromNode(TrieNode $node, string $prefix): array
{
$words = [];
if ($node->isEndOfWord) {
$words[] = $prefix;
}

foreach ($node->children as $char => $childNode) {
$words = array_merge($words, $this->findWordsFromNode($childNode, $prefix . $char));
}

return $words;
}

/**
* Delete a word from the Trie.
* Recursively traverses the Trie and removes nodes
*
* @param string $word The word to delete.
* @return bool Returns true if the word was successfully deleted, otherwise false.
*/
public function delete(string $word): bool
{
return $this->deleteHelper($this->root, $word, 0);
}

/**
* Helper function for deleting a word.
* Recursively traverse the Trie and removes nodes.
*
* @param TrieNode $node The current node in the Trie.
* @param string $word The word being deleted.
* @param int $index The current index in the word.
* @return bool Returns true if the current node should be deleted, otherwise false.
*/
private function deleteHelper(TrieNode $node, string &$word, int $index): bool
{
if ($index === strlen($word)) {
if (!$node->isEndOfWord) {
return false;
}
$node->isEndOfWord = false;
return empty($node->children);
}

$char = $word[$index];
$childNode = $node->getChild($char);
if ($childNode === null) {
return false;
}

// Recursively delete the child node
$shouldDeleteCurrentNode = $this->deleteHelper($childNode, $word, $index + 1);

if ($shouldDeleteCurrentNode) {
unset($node->children[$char]);
return !$node->isEndOfWord; // true if current node is not the end of another word
}

return false;
}

/**
* Recursively traverses the Trie starting from the given node and collects all words.
*
* @param TrieNode $node The starting node for traversal.
* @param string $prefix The prefix of the current path in the Trie.
* @return array An array of words found in the Trie starting from the given node.
*/
public function traverseTrieNode(TrieNode $node, string $prefix = ''): array
{
$words = [];

if ($node->isEndOfWord) {
$words[] = $prefix;
}

foreach ($node->children as $char => $childNode) {
$words = array_merge($words, $this->traverseTrieNode($childNode, $prefix . $char));
}

return $words;
}

/**
* Gets all words stored in the Trie.
*
* @return array An array of all words in the Trie.
*/
public function getWords(): array
{
return $this->traverseTrieNode($this->root);
}
}
43 changes: 43 additions & 0 deletions DataStructures/Trie/TrieNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace DataStructures\Trie;

class TrieNode
{
/** @var array<string, TrieNode> */
public array $children;
public bool $isEndOfWord;

public function __construct()
{
$this->children = []; // Associative array where [ char => TrieNode ]
$this->isEndOfWord = false;
}

/**
* Add a child node for a character.
*/
public function addChild(string $char): TrieNode
{
if (!isset($this->children[$char])) {
$this->children[$char] = new TrieNode();
}
return $this->children[$char];
}

/**
* Check if a character has a child node.
*/
public function hasChild(string $char): bool
{
return isset($this->children[$char]);
}

/**
* Get the child node corresponding to a character.
*/
public function getChild(string $char): ?TrieNode
{
return $this->children[$char] ?? null;
}
}
137 changes: 137 additions & 0 deletions tests/DataStructures/TrieTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace DataStructures;

require_once __DIR__ . '/../../DataStructures/Trie/Trie.php';
require_once __DIR__ . '/../../DataStructures/Trie/TrieNode.php';

use DataStructures\Trie\Trie;
use PHPUnit\Framework\TestCase;

class TrieTest extends TestCase
{
private Trie $trie;

protected function setUp(): void
{
$this->trie = new Trie();
}

public function testInsertAndSearch()
{
$this->trie->insert('the');
$this->trie->insert('universe');
$this->trie->insert('is');
$this->trie->insert('vast');

$this->assertTrue($this->trie->search('the'), 'Expected "the" to be found in the Trie.');
$this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found in the Trie.');
$this->assertTrue($this->trie->search('is'), 'Expected "is" to be found in the Trie.');
$this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found in the Trie.');
$this->assertFalse(
$this->trie->search('the universe'),
'Expected "the universe" not to be found in the Trie.'
);
}

public function testStartsWith()
{
$this->trie->insert('hello');
$this->assertEquals(['hello'], $this->trie->startsWith('he'), 'Expected words starting with "he" to be found.');
$this->assertEquals(
['hello'],
$this->trie->startsWith('hello'),
'Expected words starting with "hello" to be found.'
);
$this->assertEquals(
[],
$this->trie->startsWith('world'),
'Expected no words starting with "world" to be found.'
);
}

public function testDelete()
{
// Insert words into the Trie
$this->trie->insert('the');
$this->trie->insert('universe');
$this->trie->insert('is');
$this->trie->insert('vast');
$this->trie->insert('big');
$this->trie->insert('rather');

// Test deleting an existing word
$this->trie->delete('the');
$this->assertFalse($this->trie->search('the'), 'Expected "the" not to be found after deletion.');

// Test that other words are still present
$this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found.');
$this->assertTrue($this->trie->search('is'), 'Expected "is" to be found.');
$this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found.');
$this->assertTrue($this->trie->search('big'), 'Expected "big" to be found.');
$this->assertTrue($this->trie->search('rather'), 'Expected "rather" to be found.');
}

public function testDeleteNonExistentWord()
{
$this->trie->delete('nonexistent');
$this->assertFalse($this->trie->search('nonexistent'), 'Expected "nonexistent" to not be found.');
}

public function testTraverseTrieNode()
{
$this->trie->insert('hello');
$this->trie->insert('helium');
$this->trie->insert('helicopter');

$words = $this->trie->getWords();
$this->assertContains('hello', $words, 'Expected "hello" to be found in the Trie.');
$this->assertContains('helium', $words, 'Expected "helium" to be found in the Trie.');
$this->assertContains('helicopter', $words, 'Expected "helicopter" to be found in the Trie.');
$this->assertCount(3, $words, 'Expected 3 words in the Trie.');
}

public function testEmptyTrie()
{
$this->assertEquals([], $this->trie->getWords(), 'Expected an empty Trie to return an empty array.');
}

public function testGetWords()
{
$this->trie->insert('apple');
$this->trie->insert('app');
$this->trie->insert('applet');

$words = $this->trie->getWords();
$this->assertContains('apple', $words, 'Expected "apple" to be found in the Trie.');
$this->assertContains('app', $words, 'Expected "app" to be found in the Trie.');
$this->assertContains('applet', $words, 'Expected "applet" to be found in the Trie.');
$this->assertCount(3, $words, 'Expected 3 words in the Trie.');
}

public function testInsertEmptyString()
{
$this->trie->insert('');
$this->assertTrue($this->trie->search(''), 'Expected empty string to be found in the Trie.');
}

public function testDeleteEmptyString()
{
$this->trie->insert('');
$this->trie->delete('');
$this->assertFalse($this->trie->search(''), 'Expected empty string not to be found after deletion.');
}

public function testStartsWithWithCommonPrefix()
{
$this->trie->insert('trie');
$this->trie->insert('tried');
$this->trie->insert('trier');

$words = $this->trie->startsWith('tri');
$this->assertContains('trie', $words, 'Expected "trie" to be found with prefix "tri".');
$this->assertContains('tried', $words, 'Expected "tried" to be found with prefix "tri".');
$this->assertContains('trier', $words, 'Expected "trier" to be found with prefix "tri".');
$this->assertCount(3, $words, 'Expected 3 words with prefix "tri".');
}
}