Files
TheAlgorithms-PHP/DataStructures/AVLTree/TreeTraversal.php
Ramy eba642dbbe Implemented Segment Tree Data Structure (#166)
* 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

* 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()

---------

Co-authored-by: Brandon Johnson <bbj1979@gmail.com>
Co-authored-by: Ramy-Badr-Ahmed <Ramy-Badr-Ahmed@users.noreply.github.com>
2024-09-30 23:28:03 -06:00

89 lines
2.5 KiB
PHP

<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #163
* https://github.com/TheAlgorithms/PHP/pull/163
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures\AVLTree;
abstract class TreeTraversal
{
/**
* Perform an in-order traversal of the subtree.
* Recursively traverses the subtree rooted at the given node.
*/
public static function inOrder(?AVLTreeNode $node): array
{
$result = [];
if ($node !== null) {
$result = array_merge($result, self::inOrder($node->left));
$result[] = [$node->key => $node->value];
$result = array_merge($result, self::inOrder($node->right));
}
return $result;
}
/**
* Perform a pre-order traversal of the subtree.
* Recursively traverses the subtree rooted at the given node.
*/
public static function preOrder(?AVLTreeNode $node): array
{
$result = [];
if ($node !== null) {
$result[] = [$node->key => $node->value];
$result = array_merge($result, self::preOrder($node->left));
$result = array_merge($result, self::preOrder($node->right));
}
return $result;
}
/**
* Perform a post-order traversal of the subtree.
* Recursively traverses the subtree rooted at the given node.
*/
public static function postOrder(?AVLTreeNode $node): array
{
$result = [];
if ($node !== null) {
$result = array_merge($result, self::postOrder($node->left));
$result = array_merge($result, self::postOrder($node->right));
$result[] = [$node->key => $node->value];
}
return $result;
}
/**
* Perform a breadth-first traversal of the AVL Tree.
*/
public static function breadthFirst(?AVLTreeNode $root): array
{
$result = [];
if ($root === null) {
return $result;
}
$queue = [];
$queue[] = $root;
while (!empty($queue)) {
$currentNode = array_shift($queue);
$result[] = [$currentNode->key => $currentNode->value];
if ($currentNode->left !== null) {
$queue[] = $currentNode->left;
}
if ($currentNode->right !== null) {
$queue[] = $currentNode->right;
}
}
return $result;
}
}