code styling and refactoring for algorithms: Bellman-Ford, Dijkstras, ReverseLinkedList, CompareBinaryTree and InvertBinaryTree

This commit is contained in:
Michał Żarnecki
2024-12-17 06:54:18 +01:00
committed by Brandon Johnson
parent 7edf1247c3
commit d588cd1455
15 changed files with 54 additions and 45 deletions

View 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;
}
}

View 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());
}
}