You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Trie/ReadMe.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -123,7 +123,78 @@ And the corresponding swift code:
123
123
124
124
```
125
125
126
+
###Removal
127
+
Removing keys from the trie is a little more tricky, as there a few more cases that we have to take into account the fact that keys may exist that are actually sub-strings of other valid keys. That being said, it isn't as simple a process to just delete the nodes for a specific key, as we could be deleting references/nodes necessary for already exisitng keys!
126
128
129
+
The algorithm would be as follows:
130
+
131
+
```
132
+
133
+
let word be the key to remove
134
+
let node be the root of the trie
135
+
136
+
find(word)
137
+
if word was not found
138
+
return false
139
+
else
140
+
141
+
for each character in word
142
+
node = child node with value character
143
+
144
+
if node has more than just 1 child node
145
+
Mark node as an invalid key, since removing it would remove nodes still in use
146
+
else
147
+
while node has no valid children and node is not the root node
0 commit comments