Skip to content

Commit 329409c

Browse files
PudottaPomminButt4cak3
authored andcommitted
PHP unify code style for bubble, bogo and tree traversal (#515)
1 parent 03912e7 commit 329409c

File tree

6 files changed

+141
-107
lines changed

6 files changed

+141
-107
lines changed

contents/bogo_sort/bogo_sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ In code, it looks something like this:
4040
{% sample lang="swift" %}
4141
[import:13-19, lang:"swift"](code/swift/bogosort.swift)
4242
{% sample lang="php" %}
43-
[import:11-16, lang:"php"](code/php/bogo_sort.php)
43+
[import:15-22, lang:"php"](code/php/bogo_sort.php)
4444
{% sample lang="nim" %}
4545
[import:16-18, lang:"nim"](code/nim/bogo_sort.nim)
4646
{% sample lang="ruby" %}
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
<?php
2+
declare(strict_types=1);
23

34
function is_sorted(array $array): bool
45
{
5-
for ($i = 0, $count = count($array); $i < $count - 1; $i++)
6-
if ($array[$i] < $array[$i + 1]) return false;
6+
for ($i = 0, $count = count($array); $i < $count - 1; $i++) {
7+
if ($array[$i] > $array[$i + 1]) {
8+
return false;
9+
}
10+
}
711

8-
return true;
12+
return true;
913
}
1014

1115
function bogo_sort(array $array): array
1216
{
13-
if (!is_sorted($array)) shuffle($array);
17+
while (!is_sorted($array)) {
18+
shuffle($array);
19+
}
1420

15-
return $array;
21+
return $array;
1622
}
1723

1824

1925
$unsorted = [10, 7, 3, 1, 4, 8, 5, 6, 9, 2];
2026
$bogo_sorted = bogo_sort($unsorted);
2127

22-
echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL);
23-
echo sprintf('Sorted: %s%s', implode(',', $bogo_sorted), PHP_EOL);
28+
printf('Unsorted: %s', implode(',', $unsorted));
29+
echo PHP_EOL;
30+
printf('Sorted: %s', implode(',', $bogo_sorted));
31+
echo PHP_EOL;

contents/bubble_sort/bubble_sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
4747
{% sample lang="crystal" %}
4848
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
4949
{% sample lang="php" %}
50-
[import:3-15, lang:"php"](code/php/bubble_sort.php)
50+
[import:4-17, lang:"php"](code/php/bubble_sort.php)
5151
{% sample lang="lisp" %}
5252
[import:3-28, lang:"lisp"](code/clisp/bubble_sort.lisp)
5353
{% sample lang="nim" %}
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<?php
2+
declare(strict_types=1);
23

34
function bubble_sort(array $arr): array
45
{
5-
for ($i = 0, $length = count($arr); $i < $length; $i++) {
6-
for ($j = 1; $j < $length; $j++) {
7-
if ($arr[$j - 1] > $arr[$j]) {
8-
$tmp = $arr[$j - 1];
9-
$arr[$j - 1] = $arr[$j];
10-
$arr[$j] = $tmp;
11-
}
6+
for ($i = 0, $length = count($arr); $i < $length; $i++) {
7+
for ($j = 1; $j < $length; $j++) {
8+
if ($arr[$j - 1] > $arr[$j]) {
9+
$tmp = $arr[$j - 1];
10+
$arr[$j - 1] = $arr[$j];
11+
$arr[$j] = $tmp;
12+
}
13+
}
1214
}
13-
}
14-
return $arr;
15+
16+
return $arr;
1517
}
1618

1719
$unsorted = [1, 2, 6, 47, 4, 9, 3, 7, 8, 23, 15];
1820
$bubble_sorted = bubble_sort($unsorted);
1921

20-
echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL);
21-
echo sprintf('Sorted: %s%s', implode(',', $bubble_sorted), PHP_EOL);
22+
printf('Unsorted: %s', implode(',', $unsorted));
23+
echo PHP_EOL;
24+
printf('Sorted: %s', implode(',', $bubble_sorted));
25+
echo PHP_EOL;
Lines changed: 103 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,143 @@
11
<?php
2+
declare(strict_types=1);
23

34
class Tree implements JsonSerializable
45
{
5-
private $id;
6-
private $children = [];
6+
private $id;
7+
private $children = [];
78

8-
public function __construct(int $id, array $children = [])
9-
{
10-
$this->id = $id;
11-
$this->children = $children;
12-
}
9+
public function __construct(int $id, array $children = [])
10+
{
11+
$this->id = $id;
12+
$this->children = $children;
13+
}
1314

14-
public function getId(): int { return $this->id; }
15+
public function getId(): int
16+
{
17+
return $this->id;
18+
}
1519

16-
public function getChildren(): array { return $this->children; }
20+
public function getChildren(): array
21+
{
22+
return $this->children;
23+
}
1724

18-
public function addChild(Tree $child): void { $this->children[] = $child; }
25+
public function addChild(Tree $child): void
26+
{
27+
$this->children[] = $child;
28+
}
1929

20-
public function jsonSerialize(): array
21-
{
22-
return [
23-
'id' => $this->id,
24-
'children' => $this->children
25-
];
26-
}
30+
public function jsonSerialize(): array
31+
{
32+
return [
33+
'id' => $this->id,
34+
'children' => $this->children,
35+
];
36+
}
2737
}
2838

2939
class TreeTraversal
3040
{
31-
public static function dfs_recursive(Tree $tree): void
32-
{
33-
if ($tree->getId()) echo $tree->getId() . PHP_EOL;
34-
foreach ($tree->getChildren() as $child) static::dfs_recursive($child);
35-
}
36-
37-
public static function dfs_recursive_postorder(Tree $tree): void
38-
{
39-
foreach ($tree->getChildren() as $child) static::dfs_recursive_postorder($child);
40-
echo $tree->getId() . PHP_EOL;
41-
}
42-
43-
public static function dfs_recursive_inorder_binary(Tree $tree): void
44-
{
45-
switch (count($tree->getChildren())) {
46-
case 2:
47-
static::dfs_recursive_inorder_binary($tree->getChildren()[0]);
48-
echo $tree->getId() . PHP_EOL;
49-
static::dfs_recursive_inorder_binary($tree->getChildren()[1]);
50-
break;
51-
case 1:
52-
static::dfs_recursive_inorder_binary($tree->getChildren()[0]);
53-
echo $tree->getId() . PHP_EOL;
54-
break;
55-
case 0:
56-
echo $tree->getId() . PHP_EOL;
57-
break;
58-
default:
59-
throw new InvalidArgumentException('Not a binary tree!');
60-
break;
41+
public static function DFSRecursive(Tree $tree): void
42+
{
43+
if ($tree->getId()) {
44+
echo $tree->getId() . PHP_EOL;
45+
}
46+
foreach ($tree->getChildren() as $child) {
47+
static::DFSRecursive($child);
48+
}
6149
}
62-
}
6350

64-
public static function dfs_stack(Tree $tree): void
65-
{
66-
$stack = [$tree];
67-
$temp = null;
51+
public static function DFSRecursivePostorder(Tree $tree): void
52+
{
53+
foreach ($tree->getChildren() as $child) {
54+
static::DFSRecursivePostorder($child);
55+
}
56+
echo $tree->getId() . PHP_EOL;
57+
}
6858

69-
while (null !== ($temp = array_pop($stack))) {
70-
echo $temp->getId() . PHP_EOL;
71-
foreach ($temp->getChildren() as $child) $stack[] = $child;
59+
public static function DFSRecursiveInorderBinary(Tree $tree): void
60+
{
61+
switch (count($tree->getChildren())) {
62+
case 2:
63+
static::DFSRecursiveInorderBinary($tree->getChildren()[0]);
64+
echo $tree->getId() . PHP_EOL;
65+
static::DFSRecursiveInorderBinary($tree->getChildren()[1]);
66+
break;
67+
case 1:
68+
static::DFSRecursiveInorderBinary($tree->getChildren()[0]);
69+
echo $tree->getId() . PHP_EOL;
70+
break;
71+
case 0:
72+
echo $tree->getId() . PHP_EOL;
73+
break;
74+
default:
75+
throw new InvalidArgumentException('Not a binary tree!');
76+
break;
77+
}
7278
}
73-
}
7479

75-
public static function dfs_queue(Tree $tree): void
76-
{
77-
$stack = [$tree];
78-
$temp = null;
80+
public static function DFSStack(Tree $tree): void
81+
{
82+
$stack = [$tree];
83+
$temp = null;
84+
85+
while (null !== ($temp = array_pop($stack))) {
86+
echo $temp->getId() . PHP_EOL;
87+
foreach ($temp->getChildren() as $child) {
88+
$stack[] = $child;
89+
}
90+
}
91+
}
7992

80-
while (null !== ($temp = array_shift($stack))) {
81-
echo $temp->getId() . PHP_EOL;
82-
foreach ($temp->getChildren() as $child) $stack[] = $child;
93+
public static function DFSQueue(Tree $tree): void
94+
{
95+
$stack = [$tree];
96+
$temp = null;
97+
98+
while (null !== ($temp = array_shift($stack))) {
99+
echo $temp->getId() . PHP_EOL;
100+
foreach ($temp->getChildren() as $child) {
101+
$stack[] = $child;
102+
}
103+
}
83104
}
84-
}
85105
}
86106

87-
function generate_tree(int $num_of_rows, int $num_of_children, int $id = -1): Tree
107+
function generate_tree(int $numOfRows, int $numOfChildren, int $id = -1): Tree
88108
{
89-
if ($id === -1) $id = 1;
90-
$node = new Tree($id);
109+
if ($id === -1) {
110+
$id = 1;
111+
}
112+
$node = new Tree($id);
91113

92-
if ($num_of_rows > 1)
93-
for ($i = 0; $i < $num_of_children; $i++) {
94-
$child = generate_tree($num_of_rows - 1, $num_of_children, $id * 10 + $i + 1);
95-
$node->addChild($child);
114+
if ($numOfRows > 1) {
115+
for ($i = 0; $i < $numOfChildren; $i++) {
116+
$child = generate_tree($numOfRows - 1, $numOfChildren, $id * 10 + $i + 1);
117+
$node->addChild($child);
118+
}
96119
}
97120

98-
return $node;
121+
return $node;
99122
}
100123

101124
$node = generate_tree(3, 3);
102-
echo sprintf('TreeTraversal in JSON format: %s%s%s', PHP_EOL, json_encode($node), PHP_EOL);
103125

104126
echo 'DFS Recursive:' . PHP_EOL;
105-
TreeTraversal::dfs_recursive($node);
127+
TreeTraversal::DFSRecursive($node);
106128

107129
echo 'DFS Recursive Postorder:' . PHP_EOL;
108-
TreeTraversal::dfs_recursive_postorder($node);
130+
TreeTraversal::DFSRecursivePostorder($node);
109131

110132
echo 'DFS Stack:' . PHP_EOL;
111-
TreeTraversal::dfs_stack($node);
133+
TreeTraversal::DFSStack($node);
112134

113135
echo 'DFS Queue:' . PHP_EOL;
114-
TreeTraversal::dfs_queue($node);
136+
TreeTraversal::DFSQueue($node);
115137

116-
// If you want try binary order non-binary tree
117-
// Comment generation of new tree bellow
118-
// If you do that, exception will be thrown
138+
// If you want to try to run binary order on a non-binary tree,
139+
// comment out the generation of the new tree below.
140+
// If you do that, an exception will be thrown
119141
$node = generate_tree(3, 2);
120142
echo 'DFS Recursive Inorder Binary:' . PHP_EOL;
121-
TreeTraversal::dfs_recursive_inorder_binary($node);
143+
TreeTraversal::DFSRecursiveInorderBinary($node);

contents/tree_traversal/tree_traversal.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ As a note, a `node` struct is not necessary in javascript, so this is an example
2929
{% sample lang="swift"%}
3030
[import:1-9, lang:"swift"](code/swift/tree.swift)
3131
{% sample lang="php"%}
32-
[import:3-27, lang:"php"](code/php/tree_traversal.php)
32+
[import:4-37, lang:"php"](code/php/tree_traversal.php)
3333
{% sample lang="crystal" %}
3434
[import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr)
3535
{% sample lang="go" %}
@@ -67,7 +67,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
6767
{% sample lang="swift"%}
6868
[import:24-30, lang:"swift"](code/swift/tree.swift)
6969
{% sample lang="php"%}
70-
[import:31-35, lang:"php"](code/php/tree_traversal.php)
70+
[import:41-49, lang:"php"](code/php/tree_traversal.php)
7171
{% sample lang="crystal" %}
7272
[import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr)
7373
{% sample lang="go" %}
@@ -113,7 +113,7 @@ Now, in this case the first element searched through is still the root of the tr
113113
{% sample lang="swift"%}
114114
[import:32-38, lang:"swift"](code/swift/tree.swift)
115115
{% sample lang="php"%}
116-
[import:37-41, lang:"php"](code/php/tree_traversal.php)
116+
[import:51-57, lang:"php"](code/php/tree_traversal.php)
117117
{% sample lang="crystal" %}
118118
[import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr)
119119
{% sample lang="go" %}
@@ -154,7 +154,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
154154
{% sample lang="swift"%}
155155
[import:40-53, lang:"swift"](code/swift/tree.swift)
156156
{% sample lang="php"%}
157-
[import:43-62, lang:"php"](code/php/tree_traversal.php)
157+
[import:59-78, lang:"php"](code/php/tree_traversal.php)
158158
{% sample lang="crystal" %}
159159
[import:17-31, lang:"crystal"](code/crystal/tree-traversal.cr)
160160
{% sample lang="go" %}
@@ -205,7 +205,7 @@ In code, it looks like this:
205205
{% sample lang="swift"%}
206206
[import:55-67, lang:"swift"](code/swift/tree.swift)
207207
{% sample lang="php"%}
208-
[import:64-73, lang:"php"](code/php/tree_traversal.php)
208+
[import:80-91, lang:"php"](code/php/tree_traversal.php)
209209
{% sample lang="crystal" %}
210210
[import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr)
211211
{% sample lang="go" %}
@@ -248,7 +248,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
248248
{% sample lang="swift"%}
249249
[import:69-81, lang:"swift"](code/swift/tree.swift)
250250
{% sample lang="php"%}
251-
[import:65-74, lang:"php"](code/php/tree_traversal.php)
251+
[import:93-104, lang:"php"](code/php/tree_traversal.php)
252252
{% sample lang="crystal" %}
253253
[import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr)
254254
{% sample lang="go" %}

0 commit comments

Comments
 (0)