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
implementation of algorithms: 1. compare binary trees, 2. inverse bin…
…ary tree, 3. reverse linked list
  • Loading branch information
Michał Żarnecki committed Dec 16, 2024
commit dc89c3da1888ff3f989398f7d111b6a6d8cf942c
10 changes: 8 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
* CompareBinaryTree
* [CompareBinaryTree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php)
* [Node](./DataStructures/CompareBinaryTree/Node.php)
* [CompareBinaryTree](./DataStructures/CompareBinaryTree.php)
* [BinaryTreeNode](./DataStructures/BinaryTreeNode.php)
* Disjointsets
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
* [Doublylinkedlist](./DataStructures/DoublyLinkedList.php)
* InvertBinaryTree
* [InvertBinaryTree](./DataStructures/InvertBinaryTree.php)
* [BinaryTree](./DataStructures/BinaryTree.php)
* [Node](./DataStructures/Node.php)
* [Queue](./DataStructures/Queue.php)
* ReverseLinkedList
* [ReverseLinkedList.php](DataStructures/ReverseLinkedList.php)
* [LinkedListItem.php](DataStructures/LinkedListItem.php)
* Segmenttree
* [Segmenttree](./DataStructures/SegmentTree/SegmentTree.php)
* [Segmenttreenode](./DataStructures/SegmentTree/SegmentTreeNode.php)
Expand Down
40 changes: 40 additions & 0 deletions DataStructures/BinaryTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

class BinaryTree {
private ?BinaryTree $left = null;
private ?BinaryTree $right = null;
private $value;

public function setLeft(?BinaryTree $left)
{
$this->left = $left;
return $this;
}

public function getLeft(): ?BinaryTree
{
return $this->left;
}

public function setRight(?BinaryTree $right)
{
$this->right = $right;
return $this;
}

public function getRight(): ?BinaryTree
{
return $this->right;
}

public function setValue($value)
{
$this->value = $value;
return $this;
}

public function getValue()
{
return $this->value;
}
}
16 changes: 16 additions & 0 deletions DataStructures/BinaryTreeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace DataStructures;

class BinaryTreeNode
{
public function __construct($value, ?BinaryTreeNode $left = null, BinaryTreeNode $right = null)
{
$this->value = $value;
$this->left = $left;
$this->right = $right;
}

public $value;
public ?BinaryTreeNode $left;
public ?BinaryTreeNode $right;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace DataStructures\CompareBinaryTree;

namespace DataStructures;

/**
* Recurrent comparison of binary trees based on comparison of left and right branches
Expand All @@ -11,11 +12,11 @@ class CompareBinaryTree
{
/**
* compare two binary trees
* @param Node|null $a
* @param Node|null $b
* @param BinaryTreeNode|null $a
* @param BinaryTreeNode|null $b
* @return bool
*/
public function areTreesEqual(?Node $a, ?Node $b): bool
public function areTreesEqual(?BinaryTreeNode $a, ?BinaryTreeNode $b): bool
{
if (! $a && $b || $a && ! $b) {
return false;
Expand Down
16 changes: 0 additions & 16 deletions DataStructures/CompareBinaryTree/Node.php

This file was deleted.

26 changes: 26 additions & 0 deletions DataStructures/InvertBinaryTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DataStructures;

use BinaryTree;

/**
* Recurrent algorithm to invert binary tree (mirror)
* (https://medium.com/@kvrware/inverting-binary-tree-b0ff3a5cb0df).
*
* @author Michał Żarnecki https://github.com/rzarno
*/
class InvertBinaryTree
{
public function invert(?BinaryTree $b): void
{
if (! $b) {
return;
}
$tmp = $b->getLeft();
$b->setLeft($b->getRight());
$b->setRight($tmp);
$this->invert($b->getLeft());
$this->invert($b->getRight());
}
}
40 changes: 40 additions & 0 deletions DataStructures/LinkedListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

class LinkedListItem {
private ?LinkedListItem $next = null;
private ?LinkedListItem $prev = null;
private $value;

public function setNext(?LinkedListItem $next)
{
$this->next = $next;
return $this;
}

public function getNext(): ?LinkedListItem
{
return $this->next;
}

public function setPrev(?LinkedListItem $prev)
{
$this->prev = $prev;
return $this;
}

public function getPrev(): ?LinkedListItem
{
return $this->prev;
}

public function setValue($value)
{
$this->value = $value;
return $this;
}

public function getValue()
{
return $this->value;
}
}
26 changes: 26 additions & 0 deletions DataStructures/ReverseLinkedList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Reverse linked list
* (https://en.wikipedia.org/wiki/Linked_list).
*
* @author Michał Żarnecki https://github.com/rzarno
*/
class ReverseLinkedList
{
public function reverse(LinkedListItem $item): LinkedListItem
{
$next = $item->getNext();
$item->setNext(null);
while (true) {
$item->setPrev($next);
if (! $next) {
return $item;
}
$nextNext = $next->getNext();
$next->setNext($item);
$item = $next;
$next = $nextNext;
}
}
}
46 changes: 22 additions & 24 deletions tests/DataStructures/CompareBinaryTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,33 @@
namespace DataStructures;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/Node.php';
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/CompareBinaryTree.php';
require_once __DIR__ . '/../../DataStructures/BinaryTreeNode.php';
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree.php';

use DataStructures\CompareBinaryTree\CompareBinaryTree;
use DataStructures\CompareBinaryTree\Node;
use PHPUnit\Framework\TestCase;

class CompareBinaryTreeTest extends TestCase
{
public function testBinaryTreesAreEqualWhenAreEqualInReality()
{
$tree1 = new Node(
$tree1 = new BinaryTreeNode(
'A',
new Node(
new BinaryTreeNode(
'B',
new Node(
new BinaryTreeNode(
'D'
),
new Node(
new BinaryTreeNode(
'E',
null,
new Node(
new BinaryTreeNode(
'F'
)
)
),
new Node(
new BinaryTreeNode(
'C',
new Node('G')
new BinaryTreeNode('G')
)
);

Expand All @@ -44,43 +42,43 @@ public function testBinaryTreesAreEqualWhenAreEqualInReality()
public function testBinaryTreesAreNotEqualWhenAreNotEqualInReality()
{

$tree1 = new Node(
$tree1 = new BinaryTreeNode(
'A',
new Node(
new BinaryTreeNode(
'B',
new Node(
new BinaryTreeNode(
'F'
),
new Node(
new BinaryTreeNode(
'E',
null,
new Node(
new BinaryTreeNode(
'D'
)
)
),
new Node(
new BinaryTreeNode(
'C',
new Node('G')
new BinaryTreeNode('G')
)
);

$tree2 = new Node(
$tree2 = new BinaryTreeNode(
'A',
new Node(
new BinaryTreeNode(
'B',
new Node(
new BinaryTreeNode(
'F'
),
new Node(
new BinaryTreeNode(
'E',
null,
new Node(
new BinaryTreeNode(
'D'
)
)
),
new Node(
new BinaryTreeNode(
'C'
)
);
Expand Down
36 changes: 36 additions & 0 deletions tests/DataStructures/InvertBinaryTreeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace DataStructures;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../DataStructures/BinaryTree.php';
require_once __DIR__ . '/../../DataStructures/InvertBinaryTree.php';

use BinaryTree;
use PHPUnit\Framework\TestCase;

class InvertBinaryTreeTest extends TestCase
{
public function testInvertBinaryTree()
{
$b = (new BinaryTree())->setValue(1);
$bl = (new BinaryTree())->setValue(3);
$b->setLeft($bl);
$br = (new BinaryTree())->setValue(2);
$b->setRight($br);
$br->setLeft((new BinaryTree())->setValue(4));
$br->setRight((new BinaryTree())->setValue(5));

$expected = (new BinaryTree())->setValue(1);
$expectedBr = (new BinaryTree())->setValue(3);
$expected->setRight($expectedBr);
$expectedBl = (new BinaryTree())->setValue(2);
$expected->setLeft($expectedBl);
$expectedBl->setRight((new BinaryTree())->setValue(4));
$expectedBl->setLeft((new BinaryTree())->setValue(5));

(new InvertBinaryTree())->invert($b);

$this->assertEquals($expected, $b);
}
}
36 changes: 36 additions & 0 deletions tests/DataStructures/ReverseLinkedListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace DataStructures;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../DataStructures/LinkedListItem.php';
require_once __DIR__ . '/../../DataStructures/ReverseLinkedList.php';

use LinkedListItem;
use ReverseLinkedList;
use PHPUnit\Framework\TestCase;

class ReverseLinkedListTest extends TestCase
{
public function testReverseLinkedList()
{
$list = [1,2,3,4,5];

$firstItem = (new LinkedListItem())->setValue(0);

$prevItem = $firstItem;

foreach ($list as $value) {
$item = new LinkedListItem();
$item->setValue($value);
$item->setPrev($prevItem);
$prevItem->setNext($item);
$prevItem = $item;
}

$newFirstItem = (new ReverseLinkedList())->reverse($firstItem);
do {
$this->assertEquals($newFirstItem->getValue(), array_pop($list));
} while ($newFirstItem = $newFirstItem->getNext());
}
}