I am posting this to replace my comments from yesterday and show the code.
, . . , . .
. .
. docstring elif
return
if
( ).
def find_val_or_next_smallest1(bst, x):
if bst is None:
return None
if bst.val == x:
return x
if bst.val > x:
return find_val_or_next_smallest1(bst.left, x)
else:
right_best = find_val_or_next_smallest1(bst.right, x)
if right_best is None:
return bst.val
return right_best
. . , :
right_best = find_val_or_next_smallest1(bst.right, x)
if right_best is None:
return bst.val
return right_best
bst.val
, , , . , bst.val
. " , ". "return None, ". None. found
, , .
def find_val_or_next_smallest2(bst, x, found=None):
if bst is None:
return found
if bst.val == x:
return x
if bst.val > x:
return find_val_or_next_smallest2(bst.left, x, found)
else:
return find_val_or_next_smallest2(bst.right, x, found=bst.val)
, :
def find_val_or_next_smallest3(bst, x, found=None):
while True:
if bst is None:
return found
if bst.val == x:
return x
if bst.val > x:
bst, x, found = bst.left, x, found
else:
bst, x, found = bst.right, x, bst.val
:
def find_val_or_next_smallest4(bst, x):
found=None
while True:
if bst is None:
return found
if bst.val == x:
return x
if bst.val > x:
bst = bst.left
else:
bst, found = bst.right, bst.val