Skip to content

Commit 08a1f3b

Browse files
author
Michał Żarnecki
committed
code styling and refactoring for algorithms: Bellman-Ford, Dijkstras, ReverseLinkedList, CompareBinaryTree and InvertBinaryTree
1 parent dc89c3d commit 08a1f3b

File tree

15 files changed

+54
-45
lines changed

15 files changed

+54
-45
lines changed

DIRECTORY.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@
2828
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
2929
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
3030
* CompareBinaryTree
31-
* [CompareBinaryTree](./DataStructures/CompareBinaryTree.php)
32-
* [BinaryTreeNode](./DataStructures/BinaryTreeNode.php)
31+
* [CompareBinaryTree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php)
32+
* [BinaryTreeNode](./DataStructures/CompareBinaryTree/BinaryTreeNode.php)
3333
* Disjointsets
3434
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
3535
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
3636
* [Doublylinkedlist](./DataStructures/DoublyLinkedList.php)
3737
* InvertBinaryTree
38-
* [InvertBinaryTree](./DataStructures/InvertBinaryTree.php)
39-
* [BinaryTree](./DataStructures/BinaryTree.php)
38+
* [InvertBinaryTree](./DataStructures/InvertBinaryTree/InvertBinaryTree.php)
39+
* [BinaryTree](./DataStructures/InvertBinaryTree/BinaryTree.php)
4040
* [Node](./DataStructures/Node.php)
4141
* [Queue](./DataStructures/Queue.php)
4242
* ReverseLinkedList
43-
* [ReverseLinkedList.php](DataStructures/ReverseLinkedList.php)
44-
* [LinkedListItem.php](DataStructures/LinkedListItem.php)
43+
* [ReverseLinkedList.php](DataStructures/ReverseLinkedList/ReverseLinkedList.php)
44+
* [LinkedListItem.php](DataStructures/ReverseLinkedList/LinkedListItem.php)
4545
* Segmenttree
4646
* [Segmenttree](./DataStructures/SegmentTree/SegmentTree.php)
4747
* [Segmenttreenode](./DataStructures/SegmentTree/SegmentTreeNode.php)
@@ -60,6 +60,7 @@
6060
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
6161
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
6262
* [Dijkstra's](./Graphs/Dijkstras.php)
63+
* [Edge.php](Graphs/Edge.php)
6364

6465
## Maths
6566
* [Absolutemax](./Maths/AbsoluteMax.php)

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

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

45
class BinaryTreeNode
56
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DataStructures;
3+
namespace DataStructures\CompareBinaryTree;
44

55
/**
66
* Recurrent comparison of binary trees based on comparison of left and right branches

DataStructures/BinaryTree.php renamed to DataStructures/InvertBinaryTree/BinaryTree.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3-
class BinaryTree {
3+
namespace DataStructures\InvertBinaryTree;
4+
5+
class BinaryTree
6+
{
47
private ?BinaryTree $left = null;
58
private ?BinaryTree $right = null;
69
private $value;

DataStructures/InvertBinaryTree.php renamed to DataStructures/InvertBinaryTree/InvertBinaryTree.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

3-
namespace DataStructures;
4-
5-
use BinaryTree;
3+
namespace DataStructures\InvertBinaryTree;
64

75
/**
86
* Recurrent algorithm to invert binary tree (mirror)

DataStructures/LinkedListItem.php renamed to DataStructures/ReverseLinkedList/LinkedListItem.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3-
class LinkedListItem {
3+
namespace DataStructures\ReverseLinkedList;
4+
5+
class LinkedListItem
6+
{
47
private ?LinkedListItem $next = null;
58
private ?LinkedListItem $prev = null;
69
private $value;

DataStructures/ReverseLinkedList.php renamed to DataStructures/ReverseLinkedList/ReverseLinkedList.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace DataStructures\ReverseLinkedList;
4+
35
/**
46
* Reverse linked list
57
* (https://en.wikipedia.org/wiki/Linked_list).

Graphs/BellmanFord.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
<?php
22

3-
class Edge
4-
{
5-
public $start;
6-
public $end;
7-
public int $weight;
8-
}
9-
103
/**
114
* The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the
125
* other vertices in a weighted digraph.
136
* (https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm).
147
*
158
* @author Michał Żarnecki https://github.com/rzarno
16-
* @param array $verticesNames An array of verticies names
17-
* @param Edge[] $edges An array of edges
9+
* @param array $verticesNames An array of vertices names
10+
* @param GraphEdge[] $edges An array of edges
1811
* @param string $start The starting vertex
1912
* @return array An array of shortest paths from $start to all other vertices
2013
*/
21-
function bellmanFord(array $verticesNames, array $edges, string $start, bool $verbose = false)
14+
function bellmanFord(array $verticesNames, array $edges, string $start, bool $verbose = false): array
2215
{
2316
$vertices = array_combine($verticesNames, array_fill(0, count($verticesNames), PHP_INT_MAX));
2417

@@ -31,7 +24,7 @@ function bellmanFord(array $verticesNames, array $edges, string $start, bool $ve
3124
$change = false;
3225
foreach ($vertices as $vertice => $minWeight) {
3326
if ($verbose) {
34-
echo "checking vertice $vertice\n";
27+
echo "checking vertex $vertice\n";
3528
}
3629
if ($start === $vertice) {
3730
$vertices[$vertice] = 0;
@@ -40,7 +33,8 @@ function bellmanFord(array $verticesNames, array $edges, string $start, bool $ve
4033
foreach ($edges[$vertice] as $edge) {
4134
if ($vertices[$edge->end] > $vertices[$vertice] + $edge->weight) {
4235
if ($verbose) {
43-
echo "replace $vertice " . $vertices[$edge->end] . " with " . $vertices[$vertice] + $edge->weight . "\n ";
36+
echo "replace $vertice " . $vertices[$edge->end] . " with "
37+
. ($vertices[$vertice] + $edge->weight) . "\n ";
4438
}
4539
$vertices[$edge->end] = $vertices[$vertice] + $edge->weight;
4640
$change = true;

Graphs/Dijkstras.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
<?php
22

3-
class Edge
4-
{
5-
public $start;
6-
public $end;
7-
public int $weight;
8-
}
9-
103
/**
114
* The Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.
125
* (https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm).
136
*
147
* @author Michał Żarnecki https://github.com/rzarno
158
* @param array $verticesNames An array of vertices names
16-
* @param Edge[] $edges An array of edges
9+
* @param GraphEdge[] $edges An array of edges
1710
* @param string $start The starting vertex
1811
* @return array An array of shortest paths from $start to all other vertices
1912
*/

Graphs/GraphEdge.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
class GraphEdge
4+
{
5+
public string $start;
6+
public string $end;
7+
public int $weight;
8+
}

tests/DataStructures/CompareBinaryTreeTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace DataStructures;
44

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

9+
use DataStructures\CompareBinaryTree\BinaryTreeNode;
10+
use DataStructures\CompareBinaryTree\CompareBinaryTree;
911
use PHPUnit\Framework\TestCase;
1012

1113
class CompareBinaryTreeTest extends TestCase

tests/DataStructures/InvertBinaryTreeTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace DataStructures;
44

55
require_once __DIR__ . '/../../vendor/autoload.php';
6-
require_once __DIR__ . '/../../DataStructures/BinaryTree.php';
7-
require_once __DIR__ . '/../../DataStructures/InvertBinaryTree.php';
6+
require_once __DIR__ . '/../../DataStructures/InvertBinaryTree/BinaryTree.php';
7+
require_once __DIR__ . '/../../DataStructures/InvertBinaryTree/InvertBinaryTree.php';
88

9-
use BinaryTree;
9+
use DataStructures\InvertBinaryTree\BinaryTree;
10+
use DataStructures\InvertBinaryTree\InvertBinaryTree;
1011
use PHPUnit\Framework\TestCase;
1112

1213
class InvertBinaryTreeTest extends TestCase

tests/DataStructures/ReverseLinkedListTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace DataStructures;
44

55
require_once __DIR__ . '/../../vendor/autoload.php';
6-
require_once __DIR__ . '/../../DataStructures/LinkedListItem.php';
7-
require_once __DIR__ . '/../../DataStructures/ReverseLinkedList.php';
6+
require_once __DIR__ . '/../../DataStructures/ReverseLinkedList/LinkedListItem.php';
7+
require_once __DIR__ . '/../../DataStructures/ReverseLinkedList/ReverseLinkedList.php';
88

9-
use LinkedListItem;
10-
use ReverseLinkedList;
9+
10+
use DataStructures\ReverseLinkedList\LinkedListItem;
11+
use DataStructures\ReverseLinkedList\ReverseLinkedList;
1112
use PHPUnit\Framework\TestCase;
1213

1314
class ReverseLinkedListTest extends TestCase

tests/Graphs/BellmanFordTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
require_once __DIR__ . '/../../vendor/autoload.php';
4+
require_once __DIR__ . '/../../Graphs/GraphEdge.php';
45
require_once __DIR__ . '/../../Graphs/BellmanFord.php';
56

67
use PHPUnit\Framework\TestCase;
@@ -24,7 +25,7 @@ public function testBellmanFord()
2425
#prepare array of edges listed by edge start to simplify Bellman-Ford updating weights of other edges
2526
$edges = [];
2627
foreach ($edgesRaw as $edgeRaw) {
27-
$edge = new Edge();
28+
$edge = new GraphEdge();
2829
$edge->start = $edgeRaw[0];
2930
$edge->end = $edgeRaw[2];
3031
$edge->weight = $edgeRaw[1];

tests/Graphs/DijkstrasTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Graphs;
44
require_once __DIR__ . '/../../vendor/autoload.php';
5+
require_once __DIR__ . '/../../Graphs/GraphEdge.php';
56
require_once __DIR__ . '/../../Graphs/Dijkstras.php';
67

7-
use Edge;
8+
use GraphEdge;
89
use PHPUnit\Framework\TestCase;
910

1011
class DijkstrasTest extends TestCase
@@ -26,7 +27,7 @@ public function testDijkstras()
2627
#prepare array of edges listed by edge start to simplify Dijkstra's updating weights of other edges
2728
$edges = [];
2829
foreach ($edgesRaw as $edgeRaw) {
29-
$edge = new Edge();
30+
$edge = new GraphEdge();
3031
$edge->start = $edgeRaw[0];
3132
$edge->end = $edgeRaw[2];
3233
$edge->weight = $edgeRaw[1];

0 commit comments

Comments
 (0)