mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 07:08:13 +01:00
recuurent algorithm for comparing binary trees
This commit is contained in:
parent
b1290daa54
commit
d2fb4603e8
@ -27,6 +27,9 @@
|
|||||||
* [Bstnode](./DataStructures/BinarySearchTree/BSTNode.php)
|
* [Bstnode](./DataStructures/BinarySearchTree/BSTNode.php)
|
||||||
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
|
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
|
||||||
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
|
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
|
||||||
|
* CompareBinaryTree
|
||||||
|
* [CompareBinaryTree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php)
|
||||||
|
* [Node](./DataStructures/CompareBinaryTree/Node.php)
|
||||||
* Disjointsets
|
* Disjointsets
|
||||||
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
|
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
|
||||||
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
|
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
|
||||||
@ -50,6 +53,7 @@
|
|||||||
* [Bellmanford](./Graphs/BellmanFord.php)
|
* [Bellmanford](./Graphs/BellmanFord.php)
|
||||||
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
|
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
|
||||||
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
|
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
|
||||||
|
* [Dijkstra's](./Graphs/Dijkstras.php)
|
||||||
|
|
||||||
## Maths
|
## Maths
|
||||||
* [Absolutemax](./Maths/AbsoluteMax.php)
|
* [Absolutemax](./Maths/AbsoluteMax.php)
|
||||||
|
34
DataStructures/CompareBinaryTree/CompareBinaryTree.php
Normal file
34
DataStructures/CompareBinaryTree/CompareBinaryTree.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
16
DataStructures/CompareBinaryTree/Node.php
Normal file
16
DataStructures/CompareBinaryTree/Node.php
Normal 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;
|
||||||
|
}
|
91
tests/DataStructures/CompareBinaryTreeTest.php
Normal file
91
tests/DataStructures/CompareBinaryTreeTest.php
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user