Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
recuurent algorithm for comparing binary trees
  • Loading branch information
Michał Żarnecki committed Dec 16, 2024
commit 304161b330b0645b7132d22f83a4bf5dd424c3e7
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions DataStructures/CompareBinaryTree/CompareBinaryTree.php
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions DataStructures/CompareBinaryTree/Node.php
Original file line number Diff line number Diff line change
@@ -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 changes: 91 additions & 0 deletions tests/DataStructures/CompareBinaryTreeTest.php
Original file line number Diff line number Diff line change
@@ -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));
}
}