Skip to content

Commit 96a5d66

Browse files
committed
several methods added, phpunit extended
1 parent 3096ed3 commit 96a5d66

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

src/Algorithm/Search/LinkedListSearch.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class LinkedListSearch {
4040
*
4141
* @param AbstractLinkedList $linkedList
4242
* @return bool
43+
* @deprecated this method belongs to doganoo\PHPAlgorithms\Common\Abstracts\AbstractLinkedList and is moved there. Please use this method instead.
4344
*/
4445
public function hasLoop(AbstractLinkedList $linkedList): bool {
4546
$tortoise = $linkedList->getHead();
@@ -60,7 +61,7 @@ public function hasLoop(AbstractLinkedList $linkedList): bool {
6061

6162
/**
6263
* @param AbstractLinkedList $linkedList
63-
* @param int $k
64+
* @param int $k
6465
* @return \doganoo\PHPAlgorithms\Datastructure\Lists\Node|null
6566
*/
6667
public function findKthElementFromEnd(AbstractLinkedList $linkedList, int $k): ?Node {

src/Common/Abstracts/AbstractLinkedList.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace doganoo\PHPAlgorithms\Common\Abstracts;
2727

2828
use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
29+
use doganoo\PHPAlgorithms\Common\Interfaces\IUnaryNode;
2930
use doganoo\PHPAlgorithms\Common\Util\Comparator;
3031
use doganoo\PHPAlgorithms\Datastructure\Lists\Node;
3132

@@ -492,6 +493,50 @@ public function compareTo($object): int {
492493
return -1;
493494
}
494495

496+
/**
497+
* also known as the Runner Technique
498+
*
499+
* @return bool
500+
*/
501+
public function hasLoop(): bool {
502+
$tortoise = $this->getHead();
503+
$hare = $this->getHead();
504+
505+
while ($tortoise !== null && $hare->getNext() !== null) {
506+
$hare = $hare->getNext()->getNext();
507+
508+
if (Comparator::equals($tortoise->getValue(), $hare->getValue())) {
509+
return true;
510+
}
511+
512+
$tortoise = $tortoise->getNext();
513+
}
514+
return false;
515+
}
516+
517+
/**
518+
* returns the middle node of the linked list
519+
*
520+
* @return IUnaryNode|null
521+
*/
522+
public function getMiddleNode(): ?IUnaryNode {
523+
$head = $this->getHead();
524+
525+
if (null === $head) return null;
526+
527+
$p = $head;
528+
$q = $head;
529+
530+
while (null !== $p &&
531+
null !== $q && //actually not really necessary since $p and $q point to the same object
532+
null !== $q->getNext()
533+
) {
534+
$p = $p->getNext();
535+
$q = $q->getNext()->getNext();
536+
}
537+
return $p;
538+
}
539+
495540
/**
496541
* Specify data which should be serialized to JSON
497542
*
@@ -506,6 +551,7 @@ public function jsonSerialize() {
506551
];
507552
}
508553

554+
//TODO implement
509555
//protected function removeDuplicates() {
510556
// $node = $this->head;
511557
// $previous = $this->head;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,13 @@ public function jsonSerialize() {
191191
, "type" => "MAX_HEAP",
192192
];
193193
}
194+
195+
/**
196+
* returns the heap as an array
197+
*
198+
* @return array|null
199+
*/
200+
public function getHeap(): ?array {
201+
return $this->heap;
202+
}
194203
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,13 @@ public function jsonSerialize() {
191191
, "type" => "MIN_HEAP",
192192
];
193193
}
194+
195+
/**
196+
* returns the heap as an array
197+
*
198+
* @return array|null
199+
*/
200+
public function getHeap(): ?array {
201+
return $this->heap;
202+
}
194203
}

tests/Lists/LinkedLists/DoublyLinkedListTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,25 @@ public function testRemoveAndReplace() {
214214
$node = $list->getNodeByKey(3);
215215
$this->assertTrue($replaced && $node instanceof Node);
216216
}
217+
218+
/**
219+
* tests whether the singly linked list has a loop or not
220+
*/
221+
public function testHasLoop() {
222+
$list = LinkedListUtil::getDoublyLinkedList();
223+
$this->assertTrue(false === $list->hasLoop());
224+
$list = LinkedListUtil::getDoublyLinkedListWithLoop();
225+
$this->assertTrue(true === $list->hasLoop());
226+
}
227+
228+
public function testMiddlePart() {
229+
$ll = LinkedListUtil::getCustomDoublyLinkedList(5);
230+
/** @var Node $node */
231+
$node = $ll->getMiddleNode();
232+
233+
print_r($node->getKey());
234+
ob_flush();
235+
$this->assertTrue(3 === $node->getKey());
236+
$this->assertTrue(md5(3) === $node->getValue());
237+
}
217238
}

tests/Util/LinkedListUtil.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public static function getDoublyLinkedList(): DoublyLinkedList {
5151
return $list;
5252
}
5353

54+
/**
55+
* returns a doubly linked list with $n elements
56+
*
57+
* @param int $n the number of elements
58+
* @return DoublyLinkedList
59+
*/
60+
public static function getCustomDoublyLinkedList(int $n): DoublyLinkedList {
61+
$list = new DoublyLinkedList();
62+
for ($i = 0; $i < $n; $i++) {
63+
$list->add($i + 1, md5($i + 1));
64+
}
65+
return $list;
66+
}
67+
5468
/**
5569
* returns a singly linked list containing three elements
5670
*
@@ -64,6 +78,28 @@ public static function getSinglyLinkedList(): SinglyLinkedList {
6478
return $list;
6579
}
6680

81+
/**
82+
* returns a singly linked list containing three elements with a loop
83+
*
84+
* @return DoublyLinkedList
85+
*/
86+
public static function getDoublyLinkedListWithLoop(): DoublyLinkedList {
87+
$list = new DoublyLinkedList();
88+
$one = LinkedListUtil::getNode(1, "one");
89+
$two = LinkedListUtil::getNode(1, 2);
90+
$three = LinkedListUtil::getNode(1, new stdClass());
91+
92+
$one->setNext($two);
93+
$one->setPrevious(null);
94+
$two->setNext($three);
95+
$two->setPrevious($one);
96+
$three->setNext($one);
97+
$three->setPrevious($two);
98+
99+
$list->setHead($one);
100+
return $list;
101+
}
102+
67103
/**
68104
* creates a node instance with the given parameters
69105
*

0 commit comments

Comments
 (0)