Skip to content

Commit 999379d

Browse files
committed
Trie wordCount, LinkedList intersectionNode
1 parent 25f4e1f commit 999379d

File tree

4 files changed

+121
-24
lines changed

4 files changed

+121
-24
lines changed

src/Common/Abstracts/AbstractLinkedList.php

+71-21
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\INode;
2930
use doganoo\PHPAlgorithms\Common\Interfaces\IUnaryNode;
3031
use doganoo\PHPAlgorithms\Common\Util\Comparator;
3132
use doganoo\PHPAlgorithms\Datastructure\Lists\Node;
@@ -285,27 +286,6 @@ public abstract function prepend(?Node $node): bool;
285286
// return $list;
286287
//}
287288

288-
/**
289-
* returns the number of elements in a list
290-
*
291-
* @return int
292-
*/
293-
public function size() {
294-
if ($this->isEmpty()) {
295-
return 0;
296-
}
297-
return $this->head->size();
298-
}
299-
300-
/**
301-
* if the list is empty or not
302-
*
303-
* @return bool
304-
*/
305-
public function isEmpty() {
306-
return $this->head == null;
307-
}
308-
309289
/**
310290
* adds a Node instance to the list
311291
*
@@ -537,6 +517,76 @@ public function getMiddleNode(): ?IUnaryNode {
537517
return $p;
538518
}
539519

520+
public function getIntersectionNode(AbstractLinkedList $list): ?INode {
521+
if (0 === $this->size()) return null;
522+
if (0 === $list->size()) return null;
523+
524+
$l1 = $this->getHead();
525+
$l2 = $list->getHead();
526+
527+
$c1 = 0;
528+
$c2 = 0;
529+
530+
while (null !== $l1) {
531+
$c1++;
532+
$l1 = $l1->getNext();
533+
}
534+
535+
while (null !== $l2) {
536+
$c2++;
537+
$l2 = $l2->getNext();
538+
}
539+
540+
$l1 = $this->getHead();
541+
$l2 = $list->getHead();
542+
543+
if ($c1 > $c2) {
544+
$len = $c1 - $c2;
545+
while ($len > 0) {
546+
$l1 = $l1->getNext();
547+
$len--;
548+
}
549+
} else {
550+
$len = $c2 - $c1;
551+
while ($len > 0) {
552+
$l2 = $l2->getNext();
553+
$len--;
554+
}
555+
}
556+
557+
558+
while (null !== $l1 && null !== $l2) {
559+
if ($l1 == $l2) {
560+
return $l1;
561+
}
562+
$l1 = $l1->getNext();
563+
$l2 = $l2->getNext();
564+
}
565+
566+
return null;
567+
}
568+
569+
/**
570+
* returns the number of elements in a list
571+
*
572+
* @return int
573+
*/
574+
public function size() {
575+
if ($this->isEmpty()) {
576+
return 0;
577+
}
578+
return $this->head->size();
579+
}
580+
581+
/**
582+
* if the list is empty or not
583+
*
584+
* @return bool
585+
*/
586+
public function isEmpty() {
587+
return $this->head == null;
588+
}
589+
540590
/**
541591
* Specify data which should be serialized to JSON
542592
*

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function createEndOfWordNode() {
8383
* indicates whether it is the end of the node
8484
* @return bool
8585
*/
86-
public function isEndOfNode() {
86+
public function isEndOfWordNode() {
8787
return $this->children[0] instanceof EndOfWordNode;
8888
}
8989

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
* @package doganoo\PHPAlgorithms\Datastructure\Trie
3636
*/
3737
class Trie implements IComparable, \JsonSerializable {
38+
/**
39+
* number of characters in english alphabet
40+
*/
41+
public const ALPHABET_SIZE = 26;
3842
/**
3943
* @var RootNode
4044
*/
@@ -97,7 +101,7 @@ public function search(string $key, bool $isPrefix = false): bool {
97101
if ($isPrefix) {
98102
return null !== $node;
99103
} else {
100-
return (null !== $node && $node->isEndOfNode());
104+
return (null !== $node && $node->isEndOfWordNode());
101105
}
102106
}
103107

@@ -114,6 +118,34 @@ public function compareTo($object): int {
114118
return -1;
115119
}
116120

121+
/**
122+
* returns the number of words in the trie
123+
*
124+
* @return int
125+
*/
126+
public function countWords(): int {
127+
return $this->_countWords($this->root);
128+
}
129+
130+
/**
131+
* helper method for counting number of words in the trie
132+
*
133+
* @param Node $node
134+
* @return int
135+
*/
136+
private function _countWords(?Node $node): int {
137+
$result = 0;
138+
if (null === $node) return $result;
139+
if ($node->isEndOfWordNode()) $result++;
140+
for ($i = 0; $i < self::ALPHABET_SIZE; $i++) {
141+
if ($node->hasChild($i)) {
142+
$child = $node->getChildNode($i);
143+
$result += $this->_countWords($child);
144+
}
145+
}
146+
return $result;
147+
}
148+
117149
/**
118150
* Specify data which should be serialized to JSON
119151
*

tests/Graph/trees/trie/TrieTest.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* SOFTWARE.
2424
*/
2525

26+
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\Trie\Trie;
27+
2628
/**
2729
* Class TrieTest
2830
*/
@@ -31,12 +33,25 @@ class TrieTest extends \PHPUnit\Framework\TestCase {
3133
* tests inserting and searching
3234
*/
3335
public function testAdd() {
34-
$trie = new \doganoo\PHPAlgorithms\Datastructure\Graph\Tree\Trie\Trie();
36+
$trie = new Trie();
3537
$trie->insert("Test");
3638
$found = $trie->search("Test");
3739
$this->assertTrue($found === true);
3840
$found = $trie->search("Te", true);
3941
$this->assertTrue($found === true);
4042
}
4143

44+
public function testWordCount() {
45+
$trie = new Trie();
46+
$trie->insert("this");
47+
$trie->insert("is");
48+
$trie->insert("a");
49+
$trie->insert("very");
50+
$trie->insert("long");
51+
$trie->insert("word");
52+
53+
$this->assertTrue(6 === $trie->countWords());
54+
$this->assertTrue(6 === $trie->getAllWords()->size());
55+
}
56+
4257
}

0 commit comments

Comments
 (0)