posts: treap: add removal

This commit is contained in:
Bruno BELANYI 2024-07-27 18:33:05 +01:00
parent 6d292a3978
commit 36cb7e080f

View file

@ -126,3 +126,19 @@ def insert(self, key: K, value: V) -> bool:
# Signal whether the key was already in the key
return was_updated
```
### Removal
Removing a key from the tree is similar to inserting a new key, and forgetting
to insert it back: simply `split` the tree and `merge` it back without the
extracted middle node.
```python
def remove(self, key: K) -> bool:
# `node` contains the key, or `None` if the key wasn't in the tree
left, node, right = split(self._root, key)
# Put the tree back together, without the extract node
self._root = merge(left, right)
# Signal whether `key` was mapped in the tree
return node is not None
```