From fb8081ae427f8d98ee9eea866b683f03fcfd6450 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Thu, 3 Aug 2023 13:31:57 -0700 Subject: [PATCH] Update bst_search.py --- trees/bst_search.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/trees/bst_search.py b/trees/bst_search.py index 05e03c6..2e000b9 100644 --- a/trees/bst_search.py +++ b/trees/bst_search.py @@ -2,14 +2,31 @@ # -*- coding: utf-8 -*- # author: bt3gl -def search_bst(root, val): +def search_bst_recursive(root, val): if root is None or root.val == val: return root if val > root.val: - return search_bst(root.right, val) + return search_bst_recursive(root.right, val) else: - return search_bst(root.left, val) + return search_bst_recursive(root.left, val) + + +def search_bst_iterative(root, val): + + node = root + while node: + + if node.val == val: + return node + + if node.val < val: + node = node.right + + else: + node = node.left + + return False