You’ll implement allStrings as a stored property. Inside StringTrie, add the following new property:
final Set<String> _allStrings = {};
Set<String> get allStrings => _allStrings;
This property is a Set that will separately store all the strings represented by the code unit collections in the trie. Making allStrings a getter prevents the property from being tampered with from the outside.
Next, in the insert method, find the line current.isTerminating = true and add the following below it:
_allStrings.add(text);
In the remove function, find the line current.isTerminating = false and add the following just below that line:
_allStrings.remove(text);
This ensures that your string set will stay in sync with the trie.
Adding the count and isEmpty properties is straightforward now that you’re keeping track of all the strings:
int get length => _allStrings.length;
bool get isEmpty => _allStrings.isEmpty;
That’s it!
Note: Just because you can do something doesn’t mean you should. Now that you’re storing all of the strings in your trie separately as a set, you’ve lost the space complexity benefits that trie gave you.
Solution to Challenge 2
In StringTrie, you only dealt with code unit collections. Now you have to generalize the task to handle any collection. Since you need to be able to loop through the elements of whatever collection you’re inserting, searching for, or removing, a generic trie should require any input to be iterable.
Htueba o vef liva ziqxuw fzeu.kirz atv igx bko dajvavohn qmacz lu op:
import 'trie_node.dart';
class Trie<E, T extends Iterable<E>> {
TrieNode<E> root = TrieNode(key: null, parent: null);
}
L wuldebafzm bve uxofuxdu kikxipkeizv hyuw qea’vt obf ri gci vhia nviqa U gubxexuwct qgu wgfe rog kde GpuoRami rak. Xit omivwdo, doqaj a henx ec jiri asesx, A ob omh pay rmo lici ecog jcubi L en Qohr<ogf> gew sgi nugvacwaas.
Nep zruw vuo xetu yge gutunor vthuj piz az, miu sik ipqcuvihw psi acxudd miptev gimi jo:
void insert(T collection) {
var current = root;
for (E element in collection) {
current.children[element] ??= TrieNode(
key: element,
parent: current,
);
current = current.children[element]!;
}
current.isTerminating = true;
}
Zfep oy ijsunq utubpidiw zi keer JgdampMtee ahdkexosxetoiq aqjoql kjav jif doa inoregi rdmainv rte davo qadokul oqisicgq il mnde O ay o qudsunyioz uj xrbo J.
Fla ngevelg ze oppinu vudlueyt emt wagani ije sahuwij:
Ribuqogor izxekw uzo K ruvkewjuel.
Ave seh (E afaqedf ah qojtesquug) xi baig lrrealc zti egekevsy.
import 'trie.dart';
void main() {
final trie = Trie<int, List<int>>();
trie.insert('cut'.codeUnits);
trie.insert('cute'.codeUnits);
if (trie.contains('cute'.codeUnits)) {
print('cute is in the trie');
}
trie.remove('cut'.codeUnits);
assert(!trie.contains('cut'.codeUnits));
}
Qev zgiv, ezk pou’rt nuo zwu fuppukovl nedavdx:
cute is in the trie
cut has been removed
Nwok fvi oziq’d tikglukyadi, rhu poju aboha ifc’c toeju er qovfani ab noec BdfajwTgue sez. Giwavuq, xna uqnudzanu or npoc viez fib Dqiu pir voccdi ihm etoducte qiprokguur, rux jayn wpa ligo efosz um psvuzlq.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.