recuurent algorithm for comparing binary trees

This commit is contained in:
Michał Żarnecki 2024-12-16 19:39:15 +01:00 committed by Brandon Johnson
parent b1290daa54
commit d2fb4603e8
4 changed files with 145 additions and 0 deletions

View File

@ -27,6 +27,9 @@
* [Bstnode](./DataStructures/BinarySearchTree/BSTNode.php)
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
* CompareBinaryTree
* [CompareBinaryTree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php)
* [Node](./DataStructures/CompareBinaryTree/Node.php)
* Disjointsets
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
@ -50,6 +53,7 @@
* [Bellmanford](./Graphs/BellmanFord.php)
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
* [Dijkstra's](./Graphs/Dijkstras.php)
## Maths
* [Absolutemax](./Maths/AbsoluteMax.php)

View File

@ -0,0 +1,34 @@
<?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 Node|null $a
* @param Node|null $b
* @return bool
*/
public function areTreesEqual(?Node $a, ?Node $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);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace DataStructures\CompareBinaryTree;
class Node
{
public function __construct($value, ?Node $left = null, Node $right = null)
{
$this->value = $value;
$this->left = $left;
$this->right = $right;
}
public $value;
public ?Node $left;
public ?Node $right;
}

View File

@ -0,0 +1,91 @@
<?php
namespace DataStructures;
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/Node.php';
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/CompareBinaryTree.php';
use DataStructures\CompareBinaryTree\CompareBinaryTree;
use DataStructures\CompareBinaryTree\Node;
use PHPUnit\Framework\TestCase;
class CompareBinaryTreeTest extends TestCase
{
public function testBinaryTreesAreEqualWhenAreEqualInReality()
{
$tree1 = new Node(
'A',
new Node(
'B',
new Node(
'D'
),
new Node(
'E',
null,
new Node(
'F'
)
)
),
new Node(
'C',
new Node('G')
)
);
$tree2 = clone $tree1;
$sut = new CompareBinaryTree();
$this->assertTrue($sut->areTreesEqual($tree1, $tree2));
}
public function testBinaryTreesAreNotEqualWhenAreNotEqualInReality()
{
$tree1 = new Node(
'A',
new Node(
'B',
new Node(
'F'
),
new Node(
'E',
null,
new Node(
'D'
)
)
),
new Node(
'C',
new Node('G')
)
);
$tree2 = new Node(
'A',
new Node(
'B',
new Node(
'F'
),
new Node(
'E',
null,
new Node(
'D'
)
)
),
new Node(
'C'
)
);
$sut = new CompareBinaryTree();
$this->assertFalse($sut->areTreesEqual($tree1, $tree2));
}
}