From 1d37e00b3a9afe578ef4441e0b49ac375dc62a03 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 30 Jun 2024 12:37:48 +0100 Subject: [PATCH] posts: trie: add removal --- content/posts/2024-06-30-trie/index.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/content/posts/2024-06-30-trie/index.md b/content/posts/2024-06-30-trie/index.md index b8e4679..2a0d77e 100644 --- a/content/posts/2024-06-30-trie/index.md +++ b/content/posts/2024-06-30-trie/index.md @@ -97,3 +97,20 @@ def insert(self, key: str, value: T) -> bool: # Otherwise, recurse on the child corresponding to the first letter return self._children[key[0]].insert(key[1:], value) ``` + +### Removal + +Removal should also look familiar. + +```python +def remove(self, key: str) -> bool: + # Have we matched the full key? + if not key: + was_mapped = self._value is None + # Remove the value + self._value = None + # Return whether it was mapped + return was_mapped + # Otherwise, recurse on the child corresponding to the first letter + return self._children[key[0]].remove(key[1:]) +```