Skip to content

Commit a7dc0d3

Browse files
committed
add LevelOrder
1 parent 1702792 commit a7dc0d3

File tree

6 files changed

+187
-35
lines changed

6 files changed

+187
-35
lines changed

src/Algorithm/Traversal/InOrder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* MIT License
55
*
6-
* Copyright (c) 2018 Dogan Ucar
6+
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2020 Dogan Ucar, <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
namespace doganoo\PHPAlgorithms\Algorithm\Traversal;
28+
29+
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractTraverse;
30+
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryNode;
31+
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
32+
33+
/**
34+
* Class LevelOrder
35+
* @package doganoo\PHPAlgorithms\Algorithm\Traversal
36+
* @author Dogan Ucar <[email protected]>
37+
*/
38+
class LevelOrder extends AbstractTraverse {
39+
40+
/** @var BinarySearchTree $binarySearchTree */
41+
private $binarySearchTree;
42+
/** @var array $levels */
43+
private $levels;
44+
45+
/**
46+
* LevelOrder constructor.
47+
* @param BinarySearchTree $binarySearchTree
48+
*/
49+
public function __construct(BinarySearchTree $binarySearchTree) {
50+
$this->binarySearchTree = $binarySearchTree;
51+
$this->levels = [];
52+
53+
$this->setCallable(
54+
function (array $values) {
55+
var_dump(json_encode($values));
56+
});
57+
}
58+
59+
/**
60+
*Traverses the Binary Search Tree in Level Order
61+
*/
62+
public function traverse(): void {
63+
$level = 0;
64+
$result = [];
65+
$this->helper(
66+
$this->binarySearchTree->getRoot()
67+
, $result
68+
, $level
69+
);
70+
71+
72+
/**
73+
* @var int $level
74+
* @var IBinaryNode[] $values
75+
*/
76+
foreach ($result as $level => $values) {
77+
$this->visit($values);
78+
}
79+
80+
}
81+
82+
/**
83+
* Helper function to traverse the BST
84+
*
85+
* @param IBinaryNode|null $node
86+
* @param array $result
87+
* @param int $level
88+
*/
89+
private function helper(?IBinaryNode $node, array &$result, int $level): void {
90+
if (null === $node) return;
91+
92+
$levelArray = $result[$level] ?? [];
93+
$levelArray[] = $node;
94+
$result[$level] = $levelArray;
95+
96+
if (null !== $node->getLeft()) {
97+
$this->helper($node->getLeft(), $result, $level + 1);
98+
}
99+
100+
if (null !== $node->getRight()) {
101+
$this->helper($node->getRight(), $result, $level + 1);
102+
}
103+
}
104+
105+
}

src/Common/Abstracts/AbstractTraverse.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ abstract class AbstractTraverse {
4444
*/
4545
public abstract function traverse();
4646

47-
/**
48-
* @param IBinaryNode|null $node
49-
* @return mixed
50-
*/
51-
public abstract function _traverse(?IBinaryNode $node);
52-
5347
/**
5448
* @param $value
5549
*/

tests/Graph/trees/BinarySearchTreeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
namespace doganoo\PHPAlgorithmsTest\Graph\trees;
2828

2929
use doganoo\PHPAlgorithms\Algorithm\Traversal\InOrder;
30+
use doganoo\PHPAlgorithms\Algorithm\Traversal\LevelOrder;
3031
use doganoo\PHPAlgorithms\Algorithm\Traversal\PostOrder;
3132
use doganoo\PHPAlgorithms\Algorithm\Traversal\PreOrder;
33+
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryNode;
3234
use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
3335
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
3436
use doganoo\PHPAlgorithmsTest\Util\TreeUtil;
@@ -104,6 +106,27 @@ public function testPostOrder() {
104106
$this->assertTrue($array === [1, 2, 6, 5]);
105107
}
106108

109+
/**
110+
* tests post order Traversal
111+
*/
112+
public function testLevelOrder(): void {
113+
$bst = TreeUtil::getBinarySearchTree();
114+
$array = [];
115+
$traversal = new LevelOrder($bst);
116+
$traversal->setCallable(
117+
/** @var IBinaryNode[] $values */
118+
function (array $values) use (&$array) {
119+
$array[] = array_map(
120+
function (IBinaryNode $node) {
121+
return $node->getValue();
122+
}
123+
, $values
124+
);
125+
});
126+
$traversal->traverse();
127+
$this->assertTrue($array === [[5], [2, 6], [1]]);
128+
}
129+
107130
public function testWithObjects() {
108131
$tree = new BinarySearchTree();
109132
$upper = 10;

tests/Table/HashTableTest.php

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HashTableTest extends TestCase {
3636
/**
3737
* tests adding new elements to the map
3838
*/
39-
public function testAddition() {
39+
public function testAddition(): void {
4040
$class = stdClass::class;
4141
$hashMap = new HashTable();
4242
$boolean = $hashMap->add(1, $class);
@@ -45,7 +45,7 @@ public function testAddition() {
4545
$this->assertTrue(null !== $has);
4646
}
4747

48-
public function testSize() {
48+
public function testSize(): void {
4949
$class = stdClass::class;
5050
$hashMap = new HashTable();
5151
$hashMap->add(1, $class);
@@ -60,7 +60,7 @@ public function testSize() {
6060
/**
6161
* tests querying the map for a value
6262
*/
63-
public function testContains() {
63+
public function testContains(): void {
6464
$class = stdClass::class;
6565
$hashMap = new HashTable();
6666
$hashMap->add(1, $class);
@@ -71,7 +71,7 @@ public function testContains() {
7171
/**
7272
* tests retrieving a node from the map
7373
*/
74-
public function testGetNodeByValue() {
74+
public function testGetNodeByValue(): void {
7575
$class = stdClass::class;
7676
$hashMap = new HashTable();
7777
$hashMap->add(1, $class);
@@ -82,23 +82,53 @@ public function testGetNodeByValue() {
8282
/**
8383
* tests removing a value from the map
8484
*/
85-
public function testRemove() {
85+
public function testRemove(): void {
8686
$hashTable = new HashTable();
87-
$hashTable->put("about", new class{});
88-
$hashTable->put("account", new class{});
89-
$hashTable->put("apps", new class{});
90-
$hashTable->put("calorie_tracker", new class{});
91-
$hashTable->put("tnc", new class{});
92-
$hashTable->put("forgot_password", new class{});
93-
$hashTable->put("general_api", new class{});
94-
$hashTable->put("install", new class{});
95-
$hashTable->put("login", new class{});
96-
$hashTable->put("logout", new class{});
97-
$hashTable->put("maintenance", new class{});
98-
$hashTable->put("password_manager", new class{});
99-
$hashTable->put("promotion", new class{});
100-
$hashTable->put("register", new class{});
101-
$hashTable->put("users", new class{});
87+
$hashTable->put("about", new class {
88+
89+
});
90+
$hashTable->put("account", new class {
91+
92+
});
93+
$hashTable->put("apps", new class {
94+
95+
});
96+
$hashTable->put("calorie_tracker", new class {
97+
98+
});
99+
$hashTable->put("tnc", new class {
100+
101+
});
102+
$hashTable->put("forgot_password", new class {
103+
104+
});
105+
$hashTable->put("general_api", new class {
106+
107+
});
108+
$hashTable->put("install", new class {
109+
110+
});
111+
$hashTable->put("login", new class {
112+
113+
});
114+
$hashTable->put("logout", new class {
115+
116+
});
117+
$hashTable->put("maintenance", new class {
118+
119+
});
120+
$hashTable->put("password_manager", new class {
121+
122+
});
123+
$hashTable->put("promotion", new class {
124+
125+
});
126+
$hashTable->put("register", new class {
127+
128+
});
129+
$hashTable->put("users", new class {
130+
131+
});
102132
$this->assertTrue(null !== $hashTable->get("calorie_tracker"));
103133
$hashTable->remove("calorie_tracker");
104134
$this->assertTrue(null === $hashTable->get("calorie_tracker"));
@@ -107,7 +137,7 @@ public function testRemove() {
107137
/**
108138
* tests adding different key types to the map
109139
*/
110-
public function testKeyTypes() {
140+
public function testKeyTypes(): void {
111141
$hashMap = new HashTable();
112142
$added = $hashMap->add(new stdClass(), "stdClass");
113143
$this->assertTrue($added);
@@ -116,21 +146,21 @@ public function testKeyTypes() {
116146
/**
117147
* tests retrieving all keys from the map
118148
*/
119-
public function testKeySet() {
149+
public function testKeySet(): void {
120150
$hashMap = HashTableUtil::getHashTable(10);
121151
$keySet = $hashMap->keySet();
122152
$this->assertTrue(count($keySet) == 10);
123153
}
124154

125-
public function testClosure() {
155+
public function testClosure(): void {
126156
$hashMap = new HashTable();
127157
$added = $hashMap->add("test", function () {
128158
return new stdClass();
129159
});
130160
$this->assertTrue($added);
131161
$added = $hashMap->add("test2", new class {
132162

133-
public function x() {
163+
public function x():void {
134164
}
135165

136166
});

tests/Util/LinkedListUtil.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ public static function getDoublyLinkedListWithLoop(): DoublyLinkedList {
110110
/**
111111
* creates a node instance with the given parameters
112112
*
113-
* @param $key
114-
* @param $value
115-
* @param null $next
116-
* @param null $prev
113+
* @param mixed $key
114+
* @param mixed $value
115+
* @param null $next
116+
* @param null $prev
117117
* @return Node
118118
*/
119119
public static function getNode($key, $value, $next = null, $prev = null): Node {

0 commit comments

Comments
 (0)