Compare commits

..

5 commits

Author SHA1 Message Date
Bruno BELANYI 42910bca86 Add Treap Revisited post
Some checks failed
ci/woodpecker/push/deploy/2 Pipeline failed
2024-07-27 18:34:04 +01:00
Bruno BELANYI 36cb7e080f posts: treap: add removal 2024-07-27 18:33:05 +01:00
Bruno BELANYI 6d292a3978 posts: treap-revisited: add insertion 2024-07-27 18:32:47 +01:00
Bruno BELANYI 7cbbdcad06 posts: treap: add merge 2024-07-27 18:32:28 +01:00
Bruno BELANYI d89ea56edd posts: treap-revisited: add split 2024-07-27 18:32:09 +01:00

View file

@ -48,29 +48,27 @@ Splitting a tree means taking a key, and getting the following output:
```python
type OptionalNode[K, V] = Node[K, V] | None
class SplitResult(NamedTuple):
left: OptionalNode
node: OptionalNode
right: OptionalNode
def split(root: OptionalNode[K, V], key: K) -> SplitResult:
def split(
root: OptionalNode[K, V],
key: K,
) -> tuple[OptionalNode[K, V], OptionalNode[K, V], OptionalNode[K, V]]:
# Base case, empty tree
if root is None:
return SplitResult(None, None, None)
return None, None, None
# If we found the key, simply extract left and right
if root.key == key:
left, right = root.left, root.right
root.left, root.right = None, None
return SplitResult(left, root, right)
return left, root, right
# Otherwise, recurse on the corresponding side of the tree
if root.key < key:
left, node, right = split(root.right, key)
root.right = left
return SplitResult(root, node, right)
return root, node, right
if key < root.key:
left, node, right = split(root.left, key)
root.left = right
return SplitResult(left, node, root)
return left, node, root
raise RuntimeError("Unreachable")
```