mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
Update bst_search.py
This commit is contained in:
parent
419eeee0dc
commit
fb8081ae42
1 changed files with 20 additions and 3 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue