From 84e504cb41cf7647860b54f0a39e0ace7ec13d49 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 20 Jul 2024 19:23:00 +0100 Subject: [PATCH] posts: treap: add search --- content/posts/2024-07-20-treap/index.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/content/posts/2024-07-20-treap/index.md b/content/posts/2024-07-20-treap/index.md index 34952d4..b3de450 100644 --- a/content/posts/2024-07-20-treap/index.md +++ b/content/posts/2024-07-20-treap/index.md @@ -79,3 +79,21 @@ class Treap[K, V]: # The tree starts out empty self._root = None ``` + +### Search + +Searching the tree is the same as in any other _Binary Search Tree_. + +```python +def get(self, key: K) -> T | None: + node = self._root + # The usual BST traversal + while node is not None: + if node.key == key: + return node.value + elif node.key < key: + node = node.right + else: + node = node.left + return None +```