Skip to content

Commit

Permalink
several methods added, phpunit extended
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Nov 28, 2018
1 parent 3096ed3 commit 96a5d66
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Algorithm/Search/LinkedListSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class LinkedListSearch {
*
* @param AbstractLinkedList $linkedList
* @return bool
* @deprecated this method belongs to doganoo\PHPAlgorithms\Common\Abstracts\AbstractLinkedList and is moved there. Please use this method instead.
*/
public function hasLoop(AbstractLinkedList $linkedList): bool {
$tortoise = $linkedList->getHead();
Expand All @@ -60,7 +61,7 @@ public function hasLoop(AbstractLinkedList $linkedList): bool {

/**
* @param AbstractLinkedList $linkedList
* @param int $k
* @param int $k
* @return \doganoo\PHPAlgorithms\Datastructure\Lists\Node|null
*/
public function findKthElementFromEnd(AbstractLinkedList $linkedList, int $k): ?Node {
Expand Down
46 changes: 46 additions & 0 deletions src/Common/Abstracts/AbstractLinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace doganoo\PHPAlgorithms\Common\Abstracts;

use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
use doganoo\PHPAlgorithms\Common\Interfaces\IUnaryNode;
use doganoo\PHPAlgorithms\Common\Util\Comparator;
use doganoo\PHPAlgorithms\Datastructure\Lists\Node;

Expand Down Expand Up @@ -492,6 +493,50 @@ public function compareTo($object): int {
return -1;
}

/**
* also known as the Runner Technique
*
* @return bool
*/
public function hasLoop(): bool {
$tortoise = $this->getHead();
$hare = $this->getHead();

while ($tortoise !== null && $hare->getNext() !== null) {
$hare = $hare->getNext()->getNext();

if (Comparator::equals($tortoise->getValue(), $hare->getValue())) {
return true;
}

$tortoise = $tortoise->getNext();
}
return false;
}

/**
* returns the middle node of the linked list
*
* @return IUnaryNode|null
*/
public function getMiddleNode(): ?IUnaryNode {
$head = $this->getHead();

if (null === $head) return null;

$p = $head;
$q = $head;

while (null !== $p &&
null !== $q && //actually not really necessary since $p and $q point to the same object
null !== $q->getNext()
) {
$p = $p->getNext();
$q = $q->getNext()->getNext();
}
return $p;
}

/**
* Specify data which should be serialized to JSON
*
Expand All @@ -506,6 +551,7 @@ public function jsonSerialize() {
];
}

//TODO implement
//protected function removeDuplicates() {
// $node = $this->head;
// $previous = $this->head;
Expand Down
9 changes: 9 additions & 0 deletions src/Datastructure/Graph/Tree/Heap/MaxHeap.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,13 @@ public function jsonSerialize() {
, "type" => "MAX_HEAP",
];
}

/**
* returns the heap as an array
*
* @return array|null
*/
public function getHeap(): ?array {
return $this->heap;
}
}
9 changes: 9 additions & 0 deletions src/Datastructure/Graph/Tree/Heap/MinHeap.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,13 @@ public function jsonSerialize() {
, "type" => "MIN_HEAP",
];
}

/**
* returns the heap as an array
*
* @return array|null
*/
public function getHeap(): ?array {
return $this->heap;
}
}
21 changes: 21 additions & 0 deletions tests/Lists/LinkedLists/DoublyLinkedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,25 @@ public function testRemoveAndReplace() {
$node = $list->getNodeByKey(3);
$this->assertTrue($replaced && $node instanceof Node);
}

/**
* tests whether the singly linked list has a loop or not
*/
public function testHasLoop() {
$list = LinkedListUtil::getDoublyLinkedList();
$this->assertTrue(false === $list->hasLoop());
$list = LinkedListUtil::getDoublyLinkedListWithLoop();
$this->assertTrue(true === $list->hasLoop());
}

public function testMiddlePart() {
$ll = LinkedListUtil::getCustomDoublyLinkedList(5);
/** @var Node $node */
$node = $ll->getMiddleNode();

print_r($node->getKey());
ob_flush();
$this->assertTrue(3 === $node->getKey());
$this->assertTrue(md5(3) === $node->getValue());
}
}
36 changes: 36 additions & 0 deletions tests/Util/LinkedListUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public static function getDoublyLinkedList(): DoublyLinkedList {
return $list;
}

/**
* returns a doubly linked list with $n elements
*
* @param int $n the number of elements
* @return DoublyLinkedList
*/
public static function getCustomDoublyLinkedList(int $n): DoublyLinkedList {
$list = new DoublyLinkedList();
for ($i = 0; $i < $n; $i++) {
$list->add($i + 1, md5($i + 1));
}
return $list;
}

/**
* returns a singly linked list containing three elements
*
Expand All @@ -64,6 +78,28 @@ public static function getSinglyLinkedList(): SinglyLinkedList {
return $list;
}

/**
* returns a singly linked list containing three elements with a loop
*
* @return DoublyLinkedList
*/
public static function getDoublyLinkedListWithLoop(): DoublyLinkedList {
$list = new DoublyLinkedList();
$one = LinkedListUtil::getNode(1, "one");
$two = LinkedListUtil::getNode(1, 2);
$three = LinkedListUtil::getNode(1, new stdClass());

$one->setNext($two);
$one->setPrevious(null);
$two->setNext($three);
$two->setPrevious($one);
$three->setNext($one);
$three->setPrevious($two);

$list->setHead($one);
return $list;
}

/**
* creates a node instance with the given parameters
*
Expand Down

0 comments on commit 96a5d66

Please sign in to comment.