Skip to content
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

Enhanced AVL Tree Data Structure with (de-)serialization Feature #173

Merged
merged 50 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
82 changes: 80 additions & 2 deletions DataStructures/AVLTree/AVLTree.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #163
* https://github.com/TheAlgorithms/PHP/pull/163
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
* in Pull Request #163: https://github.com/TheAlgorithms/PHP/pull/163
* and #173: https://github.com/TheAlgorithms/PHP/pull/173
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
Expand Down Expand Up @@ -311,4 +312,81 @@ private function getMinNode(AVLTreeNode $node): AVLTreeNode
}
return $node;
}

/**
* Serializes the segment tree into a JSON string.
*
* @return string The serialized AVL Tree as a JSON string.
*/
public function serialize(): string
{
return json_encode($this->serializeTree($this->root));
}

/**
* Recursively serializes the AVL Tree.
*
* @param AVLTreeNode|null $node
* @return array
*/
private function serializeTree(?AVLTreeNode $node): array
{
if ($node === null) {
return [];
}
return [
'key' => $node->key,
'value' => $node->value,
'left' => $this->serializeTree($node->left),
'right' => $this->serializeTree($node->right),
'height' => $node->height,
];
}

/**
* Deserializes a JSON string into an AVL Tree object
*
* @param string $data The JSON representation of an AVL Tree to deserialize.
*/
public function deserialize(string $data): void
{
$this->root = $this->deserializeTree(json_decode($data, true));
$this->counter = 0;
$this->updateNodeCount($this->root);
}

/**
* Recursively deserializes an AVL Tree from an array representation.
*
* @param array $data The serialized data for the node.
* @return AVLTreeNode|null The root node of the deserialized tree.
*/
private function deserializeTree(array $data): ?AVLTreeNode
{
if (empty($data)) {
return null;
}

$node = new AVLTreeNode($data['key'], $data['value']);
$node->height = $data['height'];

$node->left = $this->deserializeTree($data['left']);
$node->right = $this->deserializeTree($data['right']);

return $node;
}

/**
* Updates the deserialized tree size.
*
* @param AVLTreeNode|null $node The root node of the deserialized tree.
*/
private function updateNodeCount(?AVLTreeNode $node): void
{
if ($node !== null) {
$this->counter++;
$this->updateNodeCount($node->left);
$this->updateNodeCount($node->right);
}
}
}
67 changes: 65 additions & 2 deletions tests/DataStructures/AVLTreeTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #163
* https://github.com/TheAlgorithms/PHP/pull/163
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
* in Pull Request #163: https://github.com/TheAlgorithms/PHP/pull/163
* and #173: https://github.com/TheAlgorithms/PHP/pull/173
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
Expand Down Expand Up @@ -37,6 +38,9 @@ private function populateTree(): void
$this->tree->insert(15, 'Value 15');
}

/**
* Tests the insert and search operations in the AVLTree.
*/
public function testInsertAndSearch(): void
{
$this->populateTree();
Expand All @@ -47,6 +51,10 @@ public function testInsertAndSearch(): void
$this->assertNull($this->tree->search(25), 'Value for non-existent key 25 should be null');
}

/**
* Tests the deletion of nodes and ensures the AVLTree maintains
* its integrity after deletions.
*/
public function testDelete(): void
{
$this->populateTree();
Expand Down Expand Up @@ -167,6 +175,9 @@ public function testInsertDuplicateKeys(): void
);
}

/**
* Tests the insertion and deletion of a large number of nodes.
*/
public function testLargeTree(): void
{
// Inserting a large number of nodes
Expand All @@ -186,6 +197,9 @@ public function testLargeTree(): void
}
}

/**
* Tests whether the AVLTree remains balanced after insertions.
*/
public function testBalance(): void
{
$this->populateTree();
Expand Down Expand Up @@ -300,4 +314,53 @@ public function testSizeOnEmptyTree(): void
$this->tree = new AVLTree();
$this->assertEquals(0, $this->tree->size(), 'Size should be 0 for an empty tree');
}

/**
* Test serialization and deserialization
*/
public function testAVLTreeSerialization(): void
{
$avlTree = new AVLTree();

$avlTree->insert(100, 'Value 100');
$avlTree->insert(200, 'Value 200');
$avlTree->insert(50, 'Value 50');
$avlTree->insert(150, 'Value 150');
$avlTree->insert(350, 'Value 350');
$avlTree->insert(40, 'Value 40');
$avlTree->insert(90, 'Value 90');

$avlTreeRoot = $avlTree->getRoot();
$serializedAVLTree = $avlTree->serialize();

$deserializedTree = new AVLTree();
$deserializedTree->deserialize($serializedAVLTree);

$deserializedTreeRoot = $deserializedTree->getRoot();

$this->assertEquals($deserializedTreeRoot->key, $avlTreeRoot->key, 'The two roots key should match');
$this->assertEquals($deserializedTreeRoot->value, $avlTreeRoot->value, 'The two roots value should match');
$this->assertEquals(
$deserializedTreeRoot->left->key,
$avlTreeRoot->left->key,
'Left child of the two roots should match'
);
$this->assertEquals(
$deserializedTreeRoot->right->key,
$avlTreeRoot->right->key,
'Left child of the two roots should match'
);
$this->assertEquals(
$deserializedTreeRoot->height,
$avlTreeRoot->height,
'The two trees should match in height'
);
$this->assertEquals($deserializedTree->size(), $avlTree->size(), 'The two trees should match in size');

$this->assertSame(
$deserializedTree->inOrderTraversal(),
$avlTree->inOrderTraversal(),
'Tree structure was not retained'
);
}
}
Loading