Skip to content

Commit 7edf124

Browse files
Michał Żarneckidarwinz
authored andcommitted
implementation of algorithms: 1. compare binary trees, 2. inverse binary tree, 3. reverse linked list
1 parent d2fb460 commit 7edf124

File tree

11 files changed

+255
-46
lines changed

11 files changed

+255
-46
lines changed

DIRECTORY.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@
2828
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
2929
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
3030
* CompareBinaryTree
31-
* [CompareBinaryTree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php)
32-
* [Node](./DataStructures/CompareBinaryTree/Node.php)
31+
* [CompareBinaryTree](./DataStructures/CompareBinaryTree.php)
32+
* [BinaryTreeNode](./DataStructures/BinaryTreeNode.php)
3333
* Disjointsets
3434
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
3535
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
3636
* [Doublylinkedlist](./DataStructures/DoublyLinkedList.php)
37+
* InvertBinaryTree
38+
* [InvertBinaryTree](./DataStructures/InvertBinaryTree.php)
39+
* [BinaryTree](./DataStructures/BinaryTree.php)
3740
* [Node](./DataStructures/Node.php)
3841
* [Queue](./DataStructures/Queue.php)
42+
* ReverseLinkedList
43+
* [ReverseLinkedList.php](DataStructures/ReverseLinkedList.php)
44+
* [LinkedListItem.php](DataStructures/LinkedListItem.php)
3945
* Segmenttree
4046
* [Segmenttree](./DataStructures/SegmentTree/SegmentTree.php)
4147
* [Segmenttreenode](./DataStructures/SegmentTree/SegmentTreeNode.php)

DataStructures/BinaryTree.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class BinaryTree {
4+
private ?BinaryTree $left = null;
5+
private ?BinaryTree $right = null;
6+
private $value;
7+
8+
public function setLeft(?BinaryTree $left)
9+
{
10+
$this->left = $left;
11+
return $this;
12+
}
13+
14+
public function getLeft(): ?BinaryTree
15+
{
16+
return $this->left;
17+
}
18+
19+
public function setRight(?BinaryTree $right)
20+
{
21+
$this->right = $right;
22+
return $this;
23+
}
24+
25+
public function getRight(): ?BinaryTree
26+
{
27+
return $this->right;
28+
}
29+
30+
public function setValue($value)
31+
{
32+
$this->value = $value;
33+
return $this;
34+
}
35+
36+
public function getValue()
37+
{
38+
return $this->value;
39+
}
40+
}

DataStructures/BinaryTreeNode.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace DataStructures;
3+
4+
class BinaryTreeNode
5+
{
6+
public function __construct($value, ?BinaryTreeNode $left = null, BinaryTreeNode $right = null)
7+
{
8+
$this->value = $value;
9+
$this->left = $left;
10+
$this->right = $right;
11+
}
12+
13+
public $value;
14+
public ?BinaryTreeNode $left;
15+
public ?BinaryTreeNode $right;
16+
}

DataStructures/CompareBinaryTree/CompareBinaryTree.php renamed to DataStructures/CompareBinaryTree.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
namespace DataStructures\CompareBinaryTree;
2+
3+
namespace DataStructures;
34

45
/**
56
* Recurrent comparison of binary trees based on comparison of left and right branches
@@ -11,11 +12,11 @@ class CompareBinaryTree
1112
{
1213
/**
1314
* compare two binary trees
14-
* @param Node|null $a
15-
* @param Node|null $b
15+
* @param BinaryTreeNode|null $a
16+
* @param BinaryTreeNode|null $b
1617
* @return bool
1718
*/
18-
public function areTreesEqual(?Node $a, ?Node $b): bool
19+
public function areTreesEqual(?BinaryTreeNode $a, ?BinaryTreeNode $b): bool
1920
{
2021
if (! $a && $b || $a && ! $b) {
2122
return false;

DataStructures/CompareBinaryTree/Node.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

DataStructures/InvertBinaryTree.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace DataStructures;
4+
5+
use BinaryTree;
6+
7+
/**
8+
* Recurrent algorithm to invert binary tree (mirror)
9+
* (https://medium.com/@kvrware/inverting-binary-tree-b0ff3a5cb0df).
10+
*
11+
* @author Michał Żarnecki https://github.com/rzarno
12+
*/
13+
class InvertBinaryTree
14+
{
15+
public function invert(?BinaryTree $b): void
16+
{
17+
if (! $b) {
18+
return;
19+
}
20+
$tmp = $b->getLeft();
21+
$b->setLeft($b->getRight());
22+
$b->setRight($tmp);
23+
$this->invert($b->getLeft());
24+
$this->invert($b->getRight());
25+
}
26+
}

DataStructures/LinkedListItem.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class LinkedListItem {
4+
private ?LinkedListItem $next = null;
5+
private ?LinkedListItem $prev = null;
6+
private $value;
7+
8+
public function setNext(?LinkedListItem $next)
9+
{
10+
$this->next = $next;
11+
return $this;
12+
}
13+
14+
public function getNext(): ?LinkedListItem
15+
{
16+
return $this->next;
17+
}
18+
19+
public function setPrev(?LinkedListItem $prev)
20+
{
21+
$this->prev = $prev;
22+
return $this;
23+
}
24+
25+
public function getPrev(): ?LinkedListItem
26+
{
27+
return $this->prev;
28+
}
29+
30+
public function setValue($value)
31+
{
32+
$this->value = $value;
33+
return $this;
34+
}
35+
36+
public function getValue()
37+
{
38+
return $this->value;
39+
}
40+
}

DataStructures/ReverseLinkedList.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* Reverse linked list
5+
* (https://en.wikipedia.org/wiki/Linked_list).
6+
*
7+
* @author Michał Żarnecki https://github.com/rzarno
8+
*/
9+
class ReverseLinkedList
10+
{
11+
public function reverse(LinkedListItem $item): LinkedListItem
12+
{
13+
$next = $item->getNext();
14+
$item->setNext(null);
15+
while (true) {
16+
$item->setPrev($next);
17+
if (! $next) {
18+
return $item;
19+
}
20+
$nextNext = $next->getNext();
21+
$next->setNext($item);
22+
$item = $next;
23+
$next = $nextNext;
24+
}
25+
}
26+
}

tests/DataStructures/CompareBinaryTreeTest.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,33 @@
33
namespace DataStructures;
44

55
require_once __DIR__ . '/../../vendor/autoload.php';
6-
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/Node.php';
7-
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree/CompareBinaryTree.php';
6+
require_once __DIR__ . '/../../DataStructures/BinaryTreeNode.php';
7+
require_once __DIR__ . '/../../DataStructures/CompareBinaryTree.php';
88

9-
use DataStructures\CompareBinaryTree\CompareBinaryTree;
10-
use DataStructures\CompareBinaryTree\Node;
119
use PHPUnit\Framework\TestCase;
1210

1311
class CompareBinaryTreeTest extends TestCase
1412
{
1513
public function testBinaryTreesAreEqualWhenAreEqualInReality()
1614
{
17-
$tree1 = new Node(
15+
$tree1 = new BinaryTreeNode(
1816
'A',
19-
new Node(
17+
new BinaryTreeNode(
2018
'B',
21-
new Node(
19+
new BinaryTreeNode(
2220
'D'
2321
),
24-
new Node(
22+
new BinaryTreeNode(
2523
'E',
2624
null,
27-
new Node(
25+
new BinaryTreeNode(
2826
'F'
2927
)
3028
)
3129
),
32-
new Node(
30+
new BinaryTreeNode(
3331
'C',
34-
new Node('G')
32+
new BinaryTreeNode('G')
3533
)
3634
);
3735

@@ -44,43 +42,43 @@ public function testBinaryTreesAreEqualWhenAreEqualInReality()
4442
public function testBinaryTreesAreNotEqualWhenAreNotEqualInReality()
4543
{
4644

47-
$tree1 = new Node(
45+
$tree1 = new BinaryTreeNode(
4846
'A',
49-
new Node(
47+
new BinaryTreeNode(
5048
'B',
51-
new Node(
49+
new BinaryTreeNode(
5250
'F'
5351
),
54-
new Node(
52+
new BinaryTreeNode(
5553
'E',
5654
null,
57-
new Node(
55+
new BinaryTreeNode(
5856
'D'
5957
)
6058
)
6159
),
62-
new Node(
60+
new BinaryTreeNode(
6361
'C',
64-
new Node('G')
62+
new BinaryTreeNode('G')
6563
)
6664
);
6765

68-
$tree2 = new Node(
66+
$tree2 = new BinaryTreeNode(
6967
'A',
70-
new Node(
68+
new BinaryTreeNode(
7169
'B',
72-
new Node(
70+
new BinaryTreeNode(
7371
'F'
7472
),
75-
new Node(
73+
new BinaryTreeNode(
7674
'E',
7775
null,
78-
new Node(
76+
new BinaryTreeNode(
7977
'D'
8078
)
8179
)
8280
),
83-
new Node(
81+
new BinaryTreeNode(
8482
'C'
8583
)
8684
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace DataStructures;
4+
5+
require_once __DIR__ . '/../../vendor/autoload.php';
6+
require_once __DIR__ . '/../../DataStructures/BinaryTree.php';
7+
require_once __DIR__ . '/../../DataStructures/InvertBinaryTree.php';
8+
9+
use BinaryTree;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class InvertBinaryTreeTest extends TestCase
13+
{
14+
public function testInvertBinaryTree()
15+
{
16+
$b = (new BinaryTree())->setValue(1);
17+
$bl = (new BinaryTree())->setValue(3);
18+
$b->setLeft($bl);
19+
$br = (new BinaryTree())->setValue(2);
20+
$b->setRight($br);
21+
$br->setLeft((new BinaryTree())->setValue(4));
22+
$br->setRight((new BinaryTree())->setValue(5));
23+
24+
$expected = (new BinaryTree())->setValue(1);
25+
$expectedBr = (new BinaryTree())->setValue(3);
26+
$expected->setRight($expectedBr);
27+
$expectedBl = (new BinaryTree())->setValue(2);
28+
$expected->setLeft($expectedBl);
29+
$expectedBl->setRight((new BinaryTree())->setValue(4));
30+
$expectedBl->setLeft((new BinaryTree())->setValue(5));
31+
32+
(new InvertBinaryTree())->invert($b);
33+
34+
$this->assertEquals($expected, $b);
35+
}
36+
}

0 commit comments

Comments
 (0)