Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
c8376de
Implemented Trie Data Structure. Added case-insensitive feature to th…
Ramy-Badr-Ahmed Oct 11, 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
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* [Speedconversion](./Conversions/SpeedConversion.php)

## Datastructures
* AVLTree
* [AVLTree](./DataStructures/AVLTree/AVLTree.php)
* [AVLTreeNode](./DataStructures/AVLTree/AVLTreeNode.php)
* Disjointsets
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
Expand Down Expand Up @@ -117,6 +120,7 @@
* Conversions
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
* Datastructures
* [AVLTreeTest](./tests/DataStructures/AVLTreeTest.php)
* [Disjointsettest](./tests/DataStructures/DisjointSetTest.php)
* [Doublylinkedlisttest](./tests/DataStructures/DoublyLinkedListTest.php)
* [Queuetest](./tests/DataStructures/QueueTest.php)
Expand Down
306 changes: 306 additions & 0 deletions DataStructures/AVLTree/AVLTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
<?php

namespace DataStructures\AVLTree;

/**
* Class AVLTree
* Implements an AVL Tree data structure with self-balancing capability.
*/
class AVLTree
{
private ?AVLTreeNode $root;
private int $counter;

public function __construct()
{
$this->root = null;
$this->counter = 0;
}

/**
* Get the root node of the AVL Tree.
*/
public function getRoot(): ?AVLTreeNode
{
return $this->root;
}

/**
* Retrieve a node by its key.
*
* @param mixed $key The key of the node to retrieve.
* @return ?AVLTreeNode The node with the specified key, or null if not found.
*/
public function getNode($key): ?AVLTreeNode
{
return $this->searchNode($this->root, $key);
}

/**
* Get the number of nodes in the AVL Tree.
*/
public function size(): int
{
return $this->counter;
}

/**
* Insert a key-value pair into the AVL Tree.
*
* @param mixed $key The key to insert.
* @param mixed $value The value associated with the key.
*/
public function insert($key, $value): void
{
$this->root = $this->insertNode($this->root, $key, $value);
$this->counter++;
}

/**
* Delete a node by its key from the AVL Tree.
*
* @param mixed $key The key of the node to delete.
*/
public function delete($key): void
{
$this->root = $this->deleteNode($this->root, $key);
$this->counter--;
}

/**
* Search for a value by its key.
*
* @param mixed $key The key to search for.
* @return mixed The value associated with the key, or null if not found.
*/
public function search($key)
{
$node = $this->searchNode($this->root, $key);
return $node ? $node->value : null;
}

/**
* Perform an in-order traversal of the AVL Tree.
* Initiates the traversal on the root node directly and returns the array of key-value pairs.
*/
public function inOrderTraversal(): array
{
return TreeTraversal::inOrder($this->root);
}

/**
* Perform a pre-order traversal of the AVL Tree.
* Initiates the traversal on the root node directly and returns the array of key-value pairs.
*/
public function preOrderTraversal(): array
{
return TreeTraversal::preOrder($this->root);
}

/**
* Perform a post-order traversal of the AVL Tree.
* Initiates the traversal on the root node directly and returns the array of key-value pairs.
*/
public function postOrderTraversal(): array
{
return TreeTraversal::postOrder($this->root);
}

/**
* Perform a breadth-first traversal of the AVL Tree.
*/
public function breadthFirstTraversal(): array
{
return TreeTraversal::breadthFirst($this->root);
}

/**
* Check if the AVL Tree is balanced.
* This method check balance starting from the root node directly
*/
public function isBalanced(): bool
{
return $this->isBalancedHelper($this->root);
}

/**
* Insert a node into the AVL Tree and balance the tree.
*
* @param ?AVLTreeNode $node The current node.
* @param mixed $key The key to insert.
* @param mixed $value The value to insert.
* @return AVLTreeNode The new root of the subtree.
*/
private function insertNode(?AVLTreeNode $node, $key, $value): AVLTreeNode
{
if ($node === null) {
return new AVLTreeNode($key, $value);
}

if ($key < $node->key) {
$node->left = $this->insertNode($node->left, $key, $value);
} elseif ($key > $node->key) {
$node->right = $this->insertNode($node->right, $key, $value);
} else {
$node->value = $value; // Update existing value
}

$node->updateHeight();
return $this->balance($node);
}

/**
* Delete a node by its key and balance the tree.
*
* @param ?AVLTreeNode $node The current node.
* @param mixed $key The key of the node to delete.
* @return ?AVLTreeNode The new root of the subtree.
*/
private function deleteNode(?AVLTreeNode $node, $key): ?AVLTreeNode
{
if ($node === null) {
return null;
}

if ($key < $node->key) {
$node->left = $this->deleteNode($node->left, $key);
} elseif ($key > $node->key) {
$node->right = $this->deleteNode($node->right, $key);
} else {
if (!$node->left) {
return $node->right;
}
if (!$node->right) {
return $node->left;
}

$minNode = $this->getMinNode($node->right);
$node->key = $minNode->key;
$node->value = $minNode->value;
$node->right = $this->deleteNode($node->right, $minNode->key);
}

$node->updateHeight();
return $this->balance($node);
}

/**
* Search for a node by its key.
*
* @param ?AVLTreeNode $node The current node.
* @param mixed $key The key to search for.
* @return ?AVLTreeNode The node with the specified key, or null if not found.
*/
private function searchNode(?AVLTreeNode $node, $key): ?AVLTreeNode
{
if ($node === null) {
return null;
}

if ($key < $node->key) {
return $this->searchNode($node->left, $key);
} elseif ($key > $node->key) {
return $this->searchNode($node->right, $key);
} else {
return $node;
}
}

/**
* Helper method to check if a subtree is balanced.
*
* @param ?AVLTreeNode $node The current node.
* @return bool True if the subtree is balanced, false otherwise.
*/
private function isBalancedHelper(?AVLTreeNode $node): bool
{
if ($node === null) {
return true;
}

$leftHeight = $node->left ? $node->left->height : 0;
$rightHeight = $node->right ? $node->right->height : 0;

$balanceFactor = abs($leftHeight - $rightHeight);
if ($balanceFactor > 1) {
return false;
}

return $this->isBalancedHelper($node->left) && $this->isBalancedHelper($node->right);
}

/**
* Balance the subtree rooted at the given node.
*
* @param ?AVLTreeNode $node The current node.
* @return ?AVLTreeNode The new root of the subtree.
*/
private function balance(?AVLTreeNode $node): ?AVLTreeNode
{
if ($node->balanceFactor() > 1) {
if ($node->left && $node->left->balanceFactor() < 0) {
$node->left = $this->rotateLeft($node->left);
}
return $this->rotateRight($node);
}

if ($node->balanceFactor() < -1) {
if ($node->right && $node->right->balanceFactor() > 0) {
$node->right = $this->rotateRight($node->right);
}
return $this->rotateLeft($node);
}

return $node;
}

/**
* Perform a left rotation on the given node.
*
* @param AVLTreeNode $node The node to rotate.
* @return AVLTreeNode The new root of the rotated subtree.
*/
private function rotateLeft(AVLTreeNode $node): AVLTreeNode
{
$newRoot = $node->right;
$node->right = $newRoot->left;
$newRoot->left = $node;

$node->updateHeight();
$newRoot->updateHeight();

return $newRoot;
}

/**
* Perform a right rotation on the given node.
*
* @param AVLTreeNode $node The node to rotate.
* @return AVLTreeNode The new root of the rotated subtree.
*/
private function rotateRight(AVLTreeNode $node): AVLTreeNode
{
$newRoot = $node->left;
$node->left = $newRoot->right;
$newRoot->right = $node;

$node->updateHeight();
$newRoot->updateHeight();

return $newRoot;
}

/**
* Get the node with the minimum key in the given subtree.
*
* @param AVLTreeNode $node The root of the subtree.
* @return AVLTreeNode The node with the minimum key.
*/
private function getMinNode(AVLTreeNode $node): AVLTreeNode
{
while ($node->left) {
$node = $node->left;
}
return $node;
}
}
41 changes: 41 additions & 0 deletions DataStructures/AVLTree/AVLTreeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace DataStructures\AVLTree;

class AVLTreeNode
{
/**
* @var int|string
*/
public $key;
/**
* @var mixed
*/
public $value;
public ?AVLTreeNode $left;
public ?AVLTreeNode $right;
public int $height;

public function __construct($key, $value, ?AVLTreeNode $left = null, ?AVLTreeNode $right = null)
{
$this->key = $key;
$this->value = $value;
$this->left = $left;
$this->right = $right;
$this->height = 1; // New node is initially at height 1
}

public function updateHeight(): void
{
$leftHeight = $this->left ? $this->left->height : 0;
$rightHeight = $this->right ? $this->right->height : 0;
$this->height = max($leftHeight, $rightHeight) + 1;
}

public function balanceFactor(): int
{
$leftHeight = $this->left ? $this->left->height : 0;
$rightHeight = $this->right ? $this->right->height : 0;
return $leftHeight - $rightHeight;
}
}
Loading