Skip to content

Added Extra Tests for Splay Tree Operations #171

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 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
e22d974
Implemented Splay Tree Data Structure. Added counter test for deletion.
Ramy-Badr-Ahmed Oct 10, 2024
d28e8bd
Implemented Splay Tree Data Structure. Added counter test for multipl…
Ramy-Badr-Ahmed Oct 10, 2024
eed2b3c
Implemented Splay Tree Data Structure. Added counter test for multipl…
Ramy-Badr-Ahmed Oct 11, 2024
e2d9d80
Implemented Splay Tree Data Structure. Added abstract setRoot() decla…
Ramy-Badr-Ahmed Oct 11, 2024
0a046c2
Implemented Splay Tree Data Structure. Fix for array_rand for non-arr…
Ramy-Badr-Ahmed Oct 11, 2024
06aea18
Implemented Splay Tree Data Structure. Fix for multiple deletion test.
Ramy-Badr-Ahmed Oct 11, 2024
f6b93b0
Implemented Splay Tree Data Structure. Added test for large splay tre…
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
2 changes: 1 addition & 1 deletion DataStructures/SplayTree/SplayTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getRoot(): ?SplayTreeNode
* Set the root node of the Splay Tree.
* @param SplayTreeNode $node
*/
public function setRoot(SplayTreeNode $node): void
protected function setRoot(SplayTreeNode $node): void
{
$this->root = $node;
}
Expand Down
1 change: 1 addition & 0 deletions DataStructures/SplayTree/SplayTreeRotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
abstract class SplayTreeRotations
{
abstract protected function splay(?SplayTreeNode $node, int $key): ?SplayTreeNode;
abstract protected function setRoot(SplayTreeNode $node): void;

/**
* Zig rotation (single right rotation).
Expand Down
71 changes: 66 additions & 5 deletions tests/DataStructures/SplayTreeTest.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: #168
* https://github.com/TheAlgorithms/PHP/pull/168
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
* in Pull Request #168: https://github.com/TheAlgorithms/PHP/pull/168
* and #171: https://github.com/TheAlgorithms/PHP/pull/171
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
Expand Down Expand Up @@ -319,7 +320,7 @@ public function testSearchNonExistingKey()

$this->assertNull(
$node->parent,
"The last visited node must have become the new root with has no parent. Failed to splay correctly."
"The last visited node must have become the new root which has no parent. Failed to splay correctly."
);
}

Expand Down Expand Up @@ -390,7 +391,7 @@ public function testUpdateNonExistingKey()
);
$this->assertNull(
$node->parent,
"The last visited node must have become the new root with has no parent. Failed to splay correctly."
"The last visited node must have become the new root which has no parent. Failed to splay correctly."
);
}

Expand All @@ -401,10 +402,18 @@ public function testDeleteExistingKey()
{
$this->populateTree();

$nodesNumber = $this->tree->size();
$node = $this->tree->delete(22);
$isFound = $this->tree->isFound(22);

$isFound = $this->tree->isFound(22);
$this->assertFalse($isFound, "Node with key 22 was not deleted.");

$this->assertEquals(
$nodesNumber - 1,
$this->tree->size(),
"After deletion, total nodes count was not updated correctly."
);

$this->assertEquals(
20,
$node->key,
Expand Down Expand Up @@ -441,6 +450,36 @@ public function testMergeAfterDeleteExistingKey()
);
}

/**
* Tests deletion of multiple nodes and checks if the tree size is updated.
*/
public function testDeleteMultipleKeys()
{
$arrayData = [200 => "Value 200", 150 => "Value 150", 170 => "Value 170",
250 => "Value 250", 300 => "Value 300", 360 => "Value 360", 230 => "Value 230",
240 => "Value 240", 220 => "Value 220", 50 => "Value 50", 28 => "Value 28",
164 => "Value 164", 321 => "Value 321", 40 => "Value 40"
];

$splayTree = new SplayTree($arrayData);
$treeSize = $splayTree->size();

$nodesToDelete = [150, 300, 50, 240, 170];
$expectedSize = $treeSize - count($nodesToDelete);

foreach ($nodesToDelete as $key) {
$splayTree->delete($key);
$isFound = $this->tree->isFound($key);
$this->assertFalse($isFound, "Node with key $key was not deleted.");
}

$this->assertEquals(
$expectedSize,
$splayTree->size(),
"After deletion, total nodes count was not updated correctly."
);
}

/**
* Ensures that attempting to delete a non-existing key throws an exception and keeps the tree intact.
*/
Expand Down Expand Up @@ -478,6 +517,28 @@ public function testOperationsOnEmptyTree()
$this->assertNull($rootNode4, "Deleting a key in an empty tree should return null.");
}

/**
* Test insert, search, delete on large trees
*/
public function testLargeTree(): void
{
// Inserting a large number of nodes
for ($i = 1; $i <= 1000; $i++) {
$this->tree->insert($i, "Value $i");
}

// Verify that all inserted nodes can be searched
for ($i = 1; $i <= 1000; $i++) {
$this->assertEquals("Value $i", $this->tree->search($i)->value, "Value for key $i should be 'Value $i'");
}

// Verify that all inserted nodes can be deleted
for ($i = 1; $i <= 5; $i++) {
$this->tree->delete($i);
$this->assertFalse($this->tree->isFound($i), "Node was not deleted correctly");
}
}


// ------------- Test 6 Rotation types of the Splay Tree -------------

Expand Down
Loading