forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.php
173 lines (151 loc) · 4.71 KB
/
Trie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request #162 and #172
* https://github.com/TheAlgorithms/PHP/pull/162
* https://github.com/TheAlgorithms/PHP/pull/172
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures\Trie;
class Trie
{
private TrieNode $root;
public function __construct()
{
$this->root = new TrieNode();
}
/**
* Get the root node of the Trie.
*/
public function getRoot(): TrieNode
{
return $this->root;
}
/**
* Insert a word into the Trie.
*/
public function insert(string $word): void
{
$node = $this->root;
for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
$node = $node->addChild($char);
}
$node->isEndOfWord = true;
}
/**
* Search for a word in the Trie.
*/
public function search(string $word): bool
{
$node = $this->root;
for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
if (!$node->hasChild($char)) {
return false;
}
$node = $node->getChild($char);
}
return $node->isEndOfWord;
}
/**
* Find all words that start with a given prefix.
*/
public function startsWith(string $prefix): array
{
$prefix = strtolower($prefix); // Normalize the prefix to lowercase
$node = $this->root;
for ($i = 0; $i < strlen($prefix); $i++) {
$char = $prefix[$i];
if (!$node->hasChild($char)) {
return [];
}
$node = $node->getChild($char);
}
return $this->findWordsFromNode($node, $prefix);
}
/**
* Helper function to find all words from a given node.
*/
private function findWordsFromNode(TrieNode $node, string $prefix): array
{
$words = [];
if ($node->isEndOfWord) {
$words[] = $prefix;
}
foreach ($node->children as $char => $childNode) {
$words = array_merge($words, $this->findWordsFromNode($childNode, $prefix . $char));
}
return $words;
}
/**
* Delete a word from the Trie.
* Recursively traverses the Trie and removes nodes
*
* @param string $word The word to delete.
* @return bool Returns true if the word was successfully deleted, otherwise false.
*/
public function delete(string $word): bool
{
return $this->deleteHelper($this->root, $word, 0);
}
/**
* Helper function for deleting a word.
* Recursively traverse the Trie and removes nodes.
*
* @param TrieNode $node The current node in the Trie.
* @param string $word The word being deleted.
* @param int $index The current index in the word.
* @return bool Returns true if the current node should be deleted, otherwise false.
*/
private function deleteHelper(TrieNode $node, string &$word, int $index): bool
{
if ($index === strlen($word)) {
if (!$node->isEndOfWord) {
return false;
}
$node->isEndOfWord = false;
return empty($node->children);
}
$char = $word[$index];
$childNode = $node->getChild($char);
if ($childNode === null) {
return false;
}
// Recursively delete the child node
$shouldDeleteCurrentNode = $this->deleteHelper($childNode, $word, $index + 1);
if ($shouldDeleteCurrentNode) {
unset($node->children[$char]);
return !$node->isEndOfWord; // true if current node is not the end of another word
}
return false;
}
/**
* Recursively traverses the Trie starting from the given node and collects all words.
*
* @param TrieNode $node The starting node for traversal.
* @param string $prefix The prefix of the current path in the Trie.
* @return array An array of words found in the Trie starting from the given node.
*/
public function traverseTrieNode(TrieNode $node, string $prefix = ''): array
{
$words = [];
if ($node->isEndOfWord) {
$words[] = $prefix;
}
foreach ($node->children as $char => $childNode) {
$words = array_merge($words, $this->traverseTrieNode($childNode, $prefix . $char));
}
return $words;
}
/**
* Gets all words stored in the Trie.
*
* @return array An array of all words in the Trie.
*/
public function getWords(): array
{
return $this->traverseTrieNode($this->root);
}
}