forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'TheAlgorithms:master' into master
- Loading branch information
Showing
21 changed files
with
816 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace DataStructures\CompareBinaryTree; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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 BinaryTreeNode|null $a | ||
* @param BinaryTreeNode|null $b | ||
* @return bool | ||
*/ | ||
public function areTreesEqual(?BinaryTreeNode $a, ?BinaryTreeNode $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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace DataStructures\InvertBinaryTree; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace DataStructures\InvertBinaryTree; | ||
|
||
/** | ||
* 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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace DataStructures\ReverseLinkedList; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace DataStructures\ReverseLinkedList; | ||
|
||
/** | ||
* 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/** | ||
* The Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph. | ||
* (https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm). | ||
* | ||
* @author Michał Żarnecki https://github.com/rzarno | ||
* @param array $verticesNames An array of vertices names | ||
* @param GraphEdge[] $edges An array of edges | ||
* @param string $start The starting vertex | ||
* @return array An array of shortest paths from $start to all other vertices | ||
*/ | ||
function dijkstras(array $verticesNames, array $edges, string $start): array | ||
{ | ||
$vertices = array_combine($verticesNames, array_fill(0, count($verticesNames), PHP_INT_MAX)); | ||
$visitedNodes = []; | ||
|
||
$nextVertex = $start; | ||
$vertices[$start] = 0; | ||
while (count($visitedNodes) < count($verticesNames)) { //continue until all nodes are visited | ||
foreach ($edges as $edge) { | ||
if ($edge->start == $nextVertex) { //consider only nodes connected to current one | ||
$vertices[$edge->end] = min($vertices[$edge->end], $vertices[$nextVertex] + $edge->weight); | ||
} | ||
} | ||
|
||
// find vertex with current lowest value to be starting point in next iteration | ||
$minVertexName = null; | ||
$minVertexWeight = PHP_INT_MAX; | ||
foreach ($vertices as $name => $weight) { | ||
if (in_array($name, $visitedNodes) || $name == $nextVertex) { | ||
continue; | ||
} | ||
if ($weight <= $minVertexWeight) { | ||
$minVertexName = $name; | ||
$minVertexWeight = $weight; | ||
} | ||
} | ||
$visitedNodes[] = $nextVertex; | ||
$nextVertex = $minVertexName; | ||
} | ||
return $vertices; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
class GraphEdge | ||
{ | ||
public string $start; | ||
public string $end; | ||
public int $weight; | ||
} |
Oops, something went wrong.