Skip to content

Commit

Permalink
smaller fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Nov 26, 2018
1 parent 0980ac5 commit 3096ed3
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/Datastructure/Graph/Graph/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace doganoo\PHPAlgorithms\Datastructure\Graph\Graph;

use doganoo\PHPAlgorithms\Common\Interfaces\INode;
use doganoo\PHPAlgorithms\Common\Util\Comparator;
use doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists\ArrayList;

/**
Expand Down Expand Up @@ -85,7 +86,7 @@ public function hasAdjacent(Node $node) {
* @var Node $value
*/
foreach ($this->adjacent as $key => $value) {
if ($value->getValue() == $node->getValue()) {
if (Comparator::equals($value->getValue(), $node->getValue())) {
return true;
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/Datastructure/Graph/Tree/Heap/MaxHeap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

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

use doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException;
use doganoo\PHPAlgorithms\Common\Interfaces\IHeap;
use doganoo\PHPAlgorithms\Common\Util\Comparator;

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

/**
* @param int $element
* @throws IndexOutOfBoundsException
*/
public function insert(int $element): void {
$length = $this->length();
Expand All @@ -75,8 +77,15 @@ public function insert(int $element): void {
$current = $this->heap[$currentPosition];
$parent = $this->heap[$parentPosition];

//this could be implemented in recursive way as well!
while (Comparator::greaterThan($current, $parent)) {
$this->swap($currentPosition, $parentPosition);
//since we have swapped the positions, we need
//to redefine our current position and parent position
//and 'bubble up':
//the current position is the parent position and the
//parent position is the parent position of the current position
//(i know, it is confusing :-))
$currentPosition = $this->getParentPosition($currentPosition);
$parentPosition = $this->getParentPosition($currentPosition);
$current = $this->heap[$currentPosition];
Expand Down Expand Up @@ -120,9 +129,11 @@ public function length(): int {
*
* @param int $pos
* @return int
* @throws IndexOutOfBoundsException
*/
public function getParentPosition(int $pos): int {
return $pos === 0 ? 0 : $pos / 2;
if ($pos < 0) throw new IndexOutOfBoundsException("$pos < 0");
return $pos === 0 ? 0 : \intval($pos / 2);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Datastructure/Graph/Tree/Heap/MinHeap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

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

use doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException;
use doganoo\PHPAlgorithms\Common\Interfaces\IHeap;
use doganoo\PHPAlgorithms\Common\Util\Comparator;

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

/**
* @param int $element
* @throws IndexOutOfBoundsException
*/
public function insert(int $element): void {
$length = $this->length();
Expand All @@ -75,8 +77,15 @@ public function insert(int $element): void {
$current = $this->heap[$currentPosition];
$parent = $this->heap[$parentPosition];

//this could be implemented in recursive way as well!
while (Comparator::lessThan($current, $parent)) {
$this->swap($currentPosition, $parentPosition);
//since we have swapped the positions, we need
//to redefine our current position and parent position
//and 'bubble up':
//the current position is the parent position and the
//parent position is the parent position of the current position
//(i know, it is confusing :-))
$currentPosition = $this->getParentPosition($currentPosition);
$parentPosition = $this->getParentPosition($currentPosition);
$current = $this->heap[$currentPosition];
Expand Down Expand Up @@ -120,9 +129,11 @@ public function length(): int {
*
* @param int $pos
* @return int
* @throws IndexOutOfBoundsException
*/
public function getParentPosition(int $pos): int {
return $pos === 0 ? 0 : $pos / 2;
if ($pos < 0) throw new IndexOutOfBoundsException("$pos < 0");
return $pos === 0 ? 0 : intval($pos / 2);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Datastructure/Graph/Tree/Trie/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,45 @@ public function __construct() {
$this->children = [];
}

/**
* @param int $position
* @return bool
*/
public function hasChild(int $position): bool {
return null !== $this->getChildNode($position);
}

/**
* @param int $position
* @return Node|null
*/
public function getChildNode(int $position): ?Node {
if (isset($this->children[$position])) {
return $this->children[$position];
}
return null;
}

/**
* @param int $position
*/
public function createChildNode(int $position) {
$node = new Node();
$node->setValue($position);
$this->children[$position] = $node;
}

/**
* creates an node that indicates the end of the word
*/
public function createEndOfWordNode() {
$this->children[] = new EndOfWordNode();
}

/**
* indicates whether it is the end of the node
* @return bool
*/
public function isEndOfNode() {
return $this->children[0] instanceof EndOfWordNode;
}
Expand Down
27 changes: 19 additions & 8 deletions src/Datastructure/Graph/Tree/Trie/Trie.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,44 @@ public function __construct() {
public function insert(string $string) {
$node = $this->root;
for ($i = 0; $i < \strlen($string); $i++) {
$position = \ord($string[$i]) - \ord("a");
if (null === $node->getChildNode($position)) {
$node->createChildNode($position);
$charIndex = $this->getCharIndex($string[$i]);
if (!$node->hasChild($charIndex)) {
$node->createChildNode($charIndex);
}
$node = $node->getChildNode($position);
$node = $node->getChildNode($charIndex);
}
$node->createEndOfWordNode();
}

/**
* returns the ASCII value of $char - ASCII value of 'a'
*
* @param string $char
* @return int
*/
private function getCharIndex(string $char): int {
if (\strlen($char) > 1) $char = \substr($char, 0, 1);
return \ord($char) - \ord('a');
}

/**
* searches for an string in the trie. $isPrefix indicates whether
* the method should search for the entire word.
*
* @param string $key
* @param bool $isPrefix
* @param bool $isPrefix
* @return bool
*/
public function search(string $key, bool $isPrefix = false): bool {
$length = \strlen($key);
$node = $this->root;

for ($i = 0; $i < $length; $i++) {
$index = \ord($key[$i]) - \ord('a');
if (null === $node->getChildNode($index)) {
$charIndex = $this->getCharIndex($key[$i]);
if (!$node->hasChild($charIndex)) {
return false;
}
$node = $node->getChildNode($index);
$node = $node->getChildNode($charIndex);
}
if ($isPrefix) {
return null !== $node;
Expand Down
2 changes: 1 addition & 1 deletion src/Datastructure/Lists/ArrayLists/StringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function arrayListToStringBuilder(ArrayList $arrayList, bool $reverse =
}
}
while (!$stack->isEmpty()) {
$stringBuilder->append($stack->peek());
$stringBuilder->append($stack->pop());
}
} else {
$queue = new Queue();
Expand Down
9 changes: 5 additions & 4 deletions src/Datastructure/Maps/HashMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* TODO implement entrySet
* TODO implement values
* TODO implement Java-like generics for key and value
* TODO replace LinkedList with BinarySearchTree
* TODO (optional) implement universal hashing
*
* @package doganoo\PHPAlgorithms\Maps
Expand Down Expand Up @@ -221,7 +222,7 @@ public function getNodeByKey($key): ?Node {
public function size(): int {
$size = 0;
/**
* @var string $hash
* @var string $hash
* @var AbstractLinkedList $list
*/
foreach ($this->bucket as $hash => $list) {
Expand All @@ -239,7 +240,7 @@ public function size(): int {
public function containsValue($value): bool {

/**
* @var string $arrayIndex
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
Expand Down Expand Up @@ -267,7 +268,7 @@ public function containsValue($value): bool {
*/
public function containsKey($key): bool {
/**
* @var string $arrayIndex
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
Expand Down Expand Up @@ -300,7 +301,7 @@ public function containsKey($key): bool {
*/
public function getNodeByValue($value): ?Node {
/**
* @var string $arrayIndex
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
Expand Down
5 changes: 3 additions & 2 deletions src/Datastructure/Stackqueue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function rear() {
* @return bool
*/
public function isEmpty(): bool {
return $this->size() === 0;
return $this->tail === $this->head;
}

/**
Expand All @@ -120,7 +120,8 @@ public function isEmpty(): bool {
* @return int
*/
public function size(): int {
return $this->queueSize();
if ($this->tail > $this->head) return 0;
return $this->head - $this->tail;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Datastructure/Stackqueue/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function sort(): void {
* @return bool
*/
public function isEmpty(): bool {
return $this->stackSize() === 0;
return $this->size() === 0;
}

/**
Expand Down

0 comments on commit 3096ed3

Please sign in to comment.