Memory efficient trie with incremental garbage collection.
Initialize a triehugger and add some words to it:
triehugger = Triehugger()
triehugger.add("beets")
triehugger.add("beetle")
triehugger.add("beetlejuice")
Your triehugger looks like:
-
- b
- be
- bee
- beet
- beets
- beetl
- beetle
- beetlej
- beetleju
- beetlejui
- beetlejuic
- beetlejuice
Now let's delete beetlejuice
:
triehugger.delete("beetlejuice")
It now looks like:
-
- b
- be
- bee
- beet
- beets
- beetl
- beetle
Note that when you deleted the word beetlejuice
, the nodes beetlej
, beetleju
, beetlejui
, beetlejuic
were also removed, since they only existed to direct you to beetlejuice
.
Once beetlejuice
is gone, they have no reason to remain!
However, beetle
remains because it was a word we explicitly added and one we want to store.