Added Extra Tests for Splay Tree Operations (#171)

* Added Disjoint Sets Data structure

* Moved DisjointSetTest.php to  tests/DataStructures

* Update DataStructures/DisjointSets/DisjointSet.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update DataStructures/DisjointSets/DisjointSetNode.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update DataStructures/DisjointSets/DisjointSetNode.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/DataStructures/DisjointSetTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/DataStructures/DisjointSetTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Update tests/DataStructures/DisjointSetTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Considered PHPCS remarks. Unit Testing is now working.

* Remove data type mixed. Considered annotations for php7.4.

* Remove data type mixed. Considered annotations for php7.4.

* updating DIRECTORY.md

* Implemented Trie DataStructure

* Added Trie to DIRECTORY.md

* updating DIRECTORY.md

* Implemented AVLTree DataStructure

* updating DIRECTORY.md

* Implemented AVLTree DataStructure

* Implemented SegmentTreeNode.php

* Implementing SegmentTree

* Implementing SegmentTree with updateTree

* Implementing SegmentTree with rangeUpdateTree

* Implementing SegmentTree with query and queryTree

* Added serializing and deserializing of the SegmentTree

* Adding unit tests SegmentTree implementation

* Added unit tests for SegmentTree updates and range updates

* considering PHPCS for Added unit tests for SegmentTree updates and range updates

* Added unit tests for SegmentTree serialization/deserialization and array updates reflections

* Added unit tests for SegmentTree  Edge Cases

* Added unit tests for SegmentTree Exceptions (OutOfBoundsException, InvalidArgumentException)

* Added SegmentTree to DIRECTORY.md

* Implemented Segment Tree Data Structure

* updating DIRECTORY.md

* Added some comments to my files in: #160, #162, #163, #166. Implemented Segment Tree Data Structure.

* Added some comments to my files in: #160, #162, #163, #166. Implemented Segment Tree Data Structure.

* Added comments time complexity for query(), update() and buildTree()

* Implemented Splay Tree Data Structure

* Update tests/DataStructures/SplayTreeTest.php

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>

* Implemented Splay Tree Data Structure. Added counter test for deletion.

* Implemented Splay Tree Data Structure. Added counter test for multiple deletions.

* Implemented Splay Tree Data Structure. Added counter test for multiple deletions.

* Implemented Splay Tree Data Structure. Added abstract setRoot() declaration to the SplayTreeRotations.php

* Implemented Splay Tree Data Structure. Fix for array_rand for non-array result. Rewriting.

* Implemented Splay Tree Data Structure. Fix for multiple deletion test.

* Implemented Splay Tree Data Structure. Added test for large splay tree operations.

---------

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>
Co-authored-by: Ramy-Badr-Ahmed <Ramy-Badr-Ahmed@users.noreply.github.com>
This commit is contained in:
Ramy
2024-10-15 22:55:42 +02:00
committed by GitHub
parent 33604749b6
commit cb2bbf948c
3 changed files with 68 additions and 6 deletions

View File

@ -44,7 +44,7 @@ class SplayTree extends SplayTreeRotations
* Set the root node of the Splay Tree. * Set the root node of the Splay Tree.
* @param SplayTreeNode $node * @param SplayTreeNode $node
*/ */
public function setRoot(SplayTreeNode $node): void protected function setRoot(SplayTreeNode $node): void
{ {
$this->root = $node; $this->root = $node;
} }

View File

@ -13,6 +13,7 @@ namespace DataStructures\SplayTree;
abstract class SplayTreeRotations abstract class SplayTreeRotations
{ {
abstract protected function splay(?SplayTreeNode $node, int $key): ?SplayTreeNode; abstract protected function splay(?SplayTreeNode $node, int $key): ?SplayTreeNode;
abstract protected function setRoot(SplayTreeNode $node): void;
/** /**
* Zig rotation (single right rotation). * Zig rotation (single right rotation).

View File

@ -1,8 +1,9 @@
<?php <?php
/* /*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #168 * Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
* https://github.com/TheAlgorithms/PHP/pull/168 * 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. * Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you! * Thank you!
@ -319,7 +320,7 @@ class SplayTreeTest extends TestCase
$this->assertNull( $this->assertNull(
$node->parent, $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."
); );
} }
@ -390,7 +391,7 @@ class SplayTreeTest extends TestCase
); );
$this->assertNull( $this->assertNull(
$node->parent, $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."
); );
} }
@ -401,10 +402,18 @@ class SplayTreeTest extends TestCase
{ {
$this->populateTree(); $this->populateTree();
$nodesNumber = $this->tree->size();
$node = $this->tree->delete(22); $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->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( $this->assertEquals(
20, 20,
$node->key, $node->key,
@ -441,6 +450,36 @@ class SplayTreeTest extends TestCase
); );
} }
/**
* 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. * Ensures that attempting to delete a non-existing key throws an exception and keeps the tree intact.
*/ */
@ -478,6 +517,28 @@ class SplayTreeTest extends TestCase
$this->assertNull($rootNode4, "Deleting a key in an empty tree should return null."); $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 ------------- // ------------- Test 6 Rotation types of the Splay Tree -------------