Skip to content

Commit 3096ed3

Browse files
committed
smaller fixes and improvements
1 parent 0980ac5 commit 3096ed3

File tree

9 files changed

+77
-19
lines changed

9 files changed

+77
-19
lines changed

src/Datastructure/Graph/Graph/Node.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace doganoo\PHPAlgorithms\Datastructure\Graph\Graph;
2727

2828
use doganoo\PHPAlgorithms\Common\Interfaces\INode;
29+
use doganoo\PHPAlgorithms\Common\Util\Comparator;
2930
use doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists\ArrayList;
3031

3132
/**
@@ -85,7 +86,7 @@ public function hasAdjacent(Node $node) {
8586
* @var Node $value
8687
*/
8788
foreach ($this->adjacent as $key => $value) {
88-
if ($value->getValue() == $node->getValue()) {
89+
if (Comparator::equals($value->getValue(), $node->getValue())) {
8990
return true;
9091
}
9192
}

src/Datastructure/Graph/Tree/Heap/MaxHeap.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace doganoo\PHPAlgorithms\Datastructure\Graph\Tree\Heap;
2727

28+
use doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException;
2829
use doganoo\PHPAlgorithms\Common\Interfaces\IHeap;
2930
use doganoo\PHPAlgorithms\Common\Util\Comparator;
3031

@@ -65,6 +66,7 @@ public function clear(): bool {
6566

6667
/**
6768
* @param int $element
69+
* @throws IndexOutOfBoundsException
6870
*/
6971
public function insert(int $element): void {
7072
$length = $this->length();
@@ -75,8 +77,15 @@ public function insert(int $element): void {
7577
$current = $this->heap[$currentPosition];
7678
$parent = $this->heap[$parentPosition];
7779

80+
//this could be implemented in recursive way as well!
7881
while (Comparator::greaterThan($current, $parent)) {
7982
$this->swap($currentPosition, $parentPosition);
83+
//since we have swapped the positions, we need
84+
//to redefine our current position and parent position
85+
//and 'bubble up':
86+
//the current position is the parent position and the
87+
//parent position is the parent position of the current position
88+
//(i know, it is confusing :-))
8089
$currentPosition = $this->getParentPosition($currentPosition);
8190
$parentPosition = $this->getParentPosition($currentPosition);
8291
$current = $this->heap[$currentPosition];
@@ -120,9 +129,11 @@ public function length(): int {
120129
*
121130
* @param int $pos
122131
* @return int
132+
* @throws IndexOutOfBoundsException
123133
*/
124134
public function getParentPosition(int $pos): int {
125-
return $pos === 0 ? 0 : $pos / 2;
135+
if ($pos < 0) throw new IndexOutOfBoundsException("$pos < 0");
136+
return $pos === 0 ? 0 : \intval($pos / 2);
126137
}
127138

128139
/**

src/Datastructure/Graph/Tree/Heap/MinHeap.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace doganoo\PHPAlgorithms\Datastructure\Graph\Tree\Heap;
2727

28+
use doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException;
2829
use doganoo\PHPAlgorithms\Common\Interfaces\IHeap;
2930
use doganoo\PHPAlgorithms\Common\Util\Comparator;
3031

@@ -65,6 +66,7 @@ public function clear(): bool {
6566

6667
/**
6768
* @param int $element
69+
* @throws IndexOutOfBoundsException
6870
*/
6971
public function insert(int $element): void {
7072
$length = $this->length();
@@ -75,8 +77,15 @@ public function insert(int $element): void {
7577
$current = $this->heap[$currentPosition];
7678
$parent = $this->heap[$parentPosition];
7779

80+
//this could be implemented in recursive way as well!
7881
while (Comparator::lessThan($current, $parent)) {
7982
$this->swap($currentPosition, $parentPosition);
83+
//since we have swapped the positions, we need
84+
//to redefine our current position and parent position
85+
//and 'bubble up':
86+
//the current position is the parent position and the
87+
//parent position is the parent position of the current position
88+
//(i know, it is confusing :-))
8089
$currentPosition = $this->getParentPosition($currentPosition);
8190
$parentPosition = $this->getParentPosition($currentPosition);
8291
$current = $this->heap[$currentPosition];
@@ -120,9 +129,11 @@ public function length(): int {
120129
*
121130
* @param int $pos
122131
* @return int
132+
* @throws IndexOutOfBoundsException
123133
*/
124134
public function getParentPosition(int $pos): int {
125-
return $pos === 0 ? 0 : $pos / 2;
135+
if ($pos < 0) throw new IndexOutOfBoundsException("$pos < 0");
136+
return $pos === 0 ? 0 : intval($pos / 2);
126137
}
127138

128139
/**

src/Datastructure/Graph/Tree/Trie/Node.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,45 @@ public function __construct() {
4444
$this->children = [];
4545
}
4646

47+
/**
48+
* @param int $position
49+
* @return bool
50+
*/
51+
public function hasChild(int $position): bool {
52+
return null !== $this->getChildNode($position);
53+
}
54+
55+
/**
56+
* @param int $position
57+
* @return Node|null
58+
*/
4759
public function getChildNode(int $position): ?Node {
4860
if (isset($this->children[$position])) {
4961
return $this->children[$position];
5062
}
5163
return null;
5264
}
5365

66+
/**
67+
* @param int $position
68+
*/
5469
public function createChildNode(int $position) {
5570
$node = new Node();
5671
$node->setValue($position);
5772
$this->children[$position] = $node;
5873
}
5974

75+
/**
76+
* creates an node that indicates the end of the word
77+
*/
6078
public function createEndOfWordNode() {
6179
$this->children[] = new EndOfWordNode();
6280
}
6381

82+
/**
83+
* indicates whether it is the end of the node
84+
* @return bool
85+
*/
6486
public function isEndOfNode() {
6587
return $this->children[0] instanceof EndOfWordNode;
6688
}

src/Datastructure/Graph/Tree/Trie/Trie.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,44 @@ public function __construct() {
5555
public function insert(string $string) {
5656
$node = $this->root;
5757
for ($i = 0; $i < \strlen($string); $i++) {
58-
$position = \ord($string[$i]) - \ord("a");
59-
if (null === $node->getChildNode($position)) {
60-
$node->createChildNode($position);
58+
$charIndex = $this->getCharIndex($string[$i]);
59+
if (!$node->hasChild($charIndex)) {
60+
$node->createChildNode($charIndex);
6161
}
62-
$node = $node->getChildNode($position);
62+
$node = $node->getChildNode($charIndex);
6363
}
6464
$node->createEndOfWordNode();
6565
}
6666

67+
/**
68+
* returns the ASCII value of $char - ASCII value of 'a'
69+
*
70+
* @param string $char
71+
* @return int
72+
*/
73+
private function getCharIndex(string $char): int {
74+
if (\strlen($char) > 1) $char = \substr($char, 0, 1);
75+
return \ord($char) - \ord('a');
76+
}
77+
6778
/**
6879
* searches for an string in the trie. $isPrefix indicates whether
6980
* the method should search for the entire word.
7081
*
7182
* @param string $key
72-
* @param bool $isPrefix
83+
* @param bool $isPrefix
7384
* @return bool
7485
*/
7586
public function search(string $key, bool $isPrefix = false): bool {
7687
$length = \strlen($key);
7788
$node = $this->root;
7889

7990
for ($i = 0; $i < $length; $i++) {
80-
$index = \ord($key[$i]) - \ord('a');
81-
if (null === $node->getChildNode($index)) {
91+
$charIndex = $this->getCharIndex($key[$i]);
92+
if (!$node->hasChild($charIndex)) {
8293
return false;
8394
}
84-
$node = $node->getChildNode($index);
95+
$node = $node->getChildNode($charIndex);
8596
}
8697
if ($isPrefix) {
8798
return null !== $node;

src/Datastructure/Lists/ArrayLists/StringBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private function arrayListToStringBuilder(ArrayList $arrayList, bool $reverse =
162162
}
163163
}
164164
while (!$stack->isEmpty()) {
165-
$stringBuilder->append($stack->peek());
165+
$stringBuilder->append($stack->pop());
166166
}
167167
} else {
168168
$queue = new Queue();

src/Datastructure/Maps/HashMap.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* TODO implement entrySet
4141
* TODO implement values
4242
* TODO implement Java-like generics for key and value
43+
* TODO replace LinkedList with BinarySearchTree
4344
* TODO (optional) implement universal hashing
4445
*
4546
* @package doganoo\PHPAlgorithms\Maps
@@ -221,7 +222,7 @@ public function getNodeByKey($key): ?Node {
221222
public function size(): int {
222223
$size = 0;
223224
/**
224-
* @var string $hash
225+
* @var string $hash
225226
* @var AbstractLinkedList $list
226227
*/
227228
foreach ($this->bucket as $hash => $list) {
@@ -239,7 +240,7 @@ public function size(): int {
239240
public function containsValue($value): bool {
240241

241242
/**
242-
* @var string $arrayIndex
243+
* @var string $arrayIndex
243244
* @var SinglyLinkedList $list
244245
*/
245246
foreach ($this->bucket as $arrayIndex => $list) {
@@ -267,7 +268,7 @@ public function containsValue($value): bool {
267268
*/
268269
public function containsKey($key): bool {
269270
/**
270-
* @var string $arrayIndex
271+
* @var string $arrayIndex
271272
* @var SinglyLinkedList $list
272273
*/
273274
foreach ($this->bucket as $arrayIndex => $list) {
@@ -300,7 +301,7 @@ public function containsKey($key): bool {
300301
*/
301302
public function getNodeByValue($value): ?Node {
302303
/**
303-
* @var string $arrayIndex
304+
* @var string $arrayIndex
304305
* @var SinglyLinkedList $list
305306
*/
306307
foreach ($this->bucket as $arrayIndex => $list) {

src/Datastructure/Stackqueue/Queue.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function rear() {
111111
* @return bool
112112
*/
113113
public function isEmpty(): bool {
114-
return $this->size() === 0;
114+
return $this->tail === $this->head;
115115
}
116116

117117
/**
@@ -120,7 +120,8 @@ public function isEmpty(): bool {
120120
* @return int
121121
*/
122122
public function size(): int {
123-
return $this->queueSize();
123+
if ($this->tail > $this->head) return 0;
124+
return $this->head - $this->tail;
124125
}
125126

126127
/**

src/Datastructure/Stackqueue/Stack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function sort(): void {
9292
* @return bool
9393
*/
9494
public function isEmpty(): bool {
95-
return $this->stackSize() === 0;
95+
return $this->size() === 0;
9696
}
9797

9898
/**

0 commit comments

Comments
 (0)