Skip to content

Commit d2fb460

Browse files
Michał Żarneckidarwinz
authored andcommitted
recuurent algorithm for comparing binary trees
1 parent b1290da commit d2fb460

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
* [Bstnode](./DataStructures/BinarySearchTree/BSTNode.php)
2828
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
2929
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
30+
* CompareBinaryTree
31+
* [CompareBinaryTree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php)
32+
* [Node](./DataStructures/CompareBinaryTree/Node.php)
3033
* Disjointsets
3134
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
3235
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
@@ -50,6 +53,7 @@
5053
* [Bellmanford](./Graphs/BellmanFord.php)
5154
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
5255
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
56+
* [Dijkstra's](./Graphs/Dijkstras.php)
5357

5458
## Maths
5559
* [Absolutemax](./Maths/AbsoluteMax.php)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace DataStructures\CompareBinaryTree;
3+
4+
/**
5+
* Recurrent comparison of binary trees based on comparison of left and right branches
6+
* (https://en.wikipedia.org/wiki/Binary_tree).
7+
*
8+
* @author Michał Żarnecki https://github.com/rzarno
9+
*/
10+
class CompareBinaryTree
11+
{
12+
/**
13+
* compare two binary trees
14+
* @param Node|null $a
15+
* @param Node|null $b
16+
* @return bool
17+
*/
18+
public function areTreesEqual(?Node $a, ?Node $b): bool
19+
{
20+
if (! $a && $b || $a && ! $b) {
21+
return false;
22+
}
23+
24+
if (! $a && ! $b) {
25+
return true;
26+
}
27+
28+
if ($a->value !== $b->value) {
29+
return false;
30+
}
31+
return $this->areTreesEqual($a->left, $b->left)
32+
&& $this->areTreesEqual($a->right, $b->right);
33+
}
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace DataStructures\CompareBinaryTree;
3+
4+
class Node
5+
{
6+
public function __construct($value, ?Node $left = null, Node $right = null)
7+
{
8+
$this->value = $value;
9+
$this->left = $left;
10+
$this->right = $right;
11+
}
12+
13+
public $value;
14+
public ?Node $left;
15+
public ?Node $right;
16+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace DataStructures;
4+
5+
require_once __DIR__ . '/../../vendor/autoload.php';
6+
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/Node.php';
7+
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/CompareBinaryTree.php';
8+
9+
use DataStructures\CompareBinaryTree\CompareBinaryTree;
10+
use DataStructures\CompareBinaryTree\Node;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class CompareBinaryTreeTest extends TestCase
14+
{
15+
public function testBinaryTreesAreEqualWhenAreEqualInReality()
16+
{
17+
$tree1 = new Node(
18+
'A',
19+
new Node(
20+
'B',
21+
new Node(
22+
'D'
23+
),
24+
new Node(
25+
'E',
26+
null,
27+
new Node(
28+
'F'
29+
)
30+
)
31+
),
32+
new Node(
33+
'C',
34+
new Node('G')
35+
)
36+
);
37+
38+
$tree2 = clone $tree1;
39+
40+
$sut = new CompareBinaryTree();
41+
$this->assertTrue($sut->areTreesEqual($tree1, $tree2));
42+
}
43+
44+
public function testBinaryTreesAreNotEqualWhenAreNotEqualInReality()
45+
{
46+
47+
$tree1 = new Node(
48+
'A',
49+
new Node(
50+
'B',
51+
new Node(
52+
'F'
53+
),
54+
new Node(
55+
'E',
56+
null,
57+
new Node(
58+
'D'
59+
)
60+
)
61+
),
62+
new Node(
63+
'C',
64+
new Node('G')
65+
)
66+
);
67+
68+
$tree2 = new Node(
69+
'A',
70+
new Node(
71+
'B',
72+
new Node(
73+
'F'
74+
),
75+
new Node(
76+
'E',
77+
null,
78+
new Node(
79+
'D'
80+
)
81+
)
82+
),
83+
new Node(
84+
'C'
85+
)
86+
);
87+
88+
$sut = new CompareBinaryTree();
89+
$this->assertFalse($sut->areTreesEqual($tree1, $tree2));
90+
}
91+
}

0 commit comments

Comments
 (0)