mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-07-09 19:16:19 +02:00
code styling and refactoring for algorithms: Bellman-Ford, Dijkstras, ReverseLinkedList, CompareBinaryTree and InvertBinaryTree
This commit is contained in:
committed by
Brandon Johnson
parent
7edf1247c3
commit
d588cd1455
43
DataStructures/InvertBinaryTree/BinaryTree.php
Normal file
43
DataStructures/InvertBinaryTree/BinaryTree.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
24
DataStructures/InvertBinaryTree/InvertBinaryTree.php
Normal file
24
DataStructures/InvertBinaryTree/InvertBinaryTree.php
Normal file
@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user