code styling and refactoring for algorithms: Bellman-Ford, Dijkstras, ReverseLinkedList, CompareBinaryTree and InvertBinaryTree

This commit is contained in:
Michał Żarnecki
2024-12-17 06:54:18 +01:00
committed by Brandon Johnson
parent 7edf1247c3
commit d588cd1455
15 changed files with 54 additions and 45 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace DataStructures\CompareBinaryTree;
/**
* Recurrent comparison of binary trees based on comparison of left and right branches
* (https://en.wikipedia.org/wiki/Binary_tree).
*
* @author Michał Żarnecki https://github.com/rzarno
*/
class CompareBinaryTree
{
/**
* compare two binary trees
* @param BinaryTreeNode|null $a
* @param BinaryTreeNode|null $b
* @return bool
*/
public function areTreesEqual(?BinaryTreeNode $a, ?BinaryTreeNode $b): bool
{
if (! $a && $b || $a && ! $b) {
return false;
}
if (! $a && ! $b) {
return true;
}
if ($a->value !== $b->value) {
return false;
}
return $this->areTreesEqual($a->left, $b->left)
&& $this->areTreesEqual($a->right, $b->right);
}
}