def find_val_or_next_smaller_iter(bst, x, d=None):
    """Get the greatest value <= x in a binary search tree.
    Returns None if no such value can be found.
    """
    while True:
        if bst is None:
            return d
        elif bst.val == x:
            return x
        elif bst.val > x:
            (bst, x, d) = (bst.left, x, d)
        else:
            (bst, x, d) = (bst.right, x, bst.val)