Two properties make up a recursive behavior:
Base case
(terminal case)
Recursive step
base case
# Person's ancestor Base case: person's parent Recursive: person's parent's ancestors # Fib Sequence Base case: fib(0) = 0 Recursive: fib(1) = 1 Fib(n) = Fib(n-1) + Fib(n-2)
Because recursion relies on a native call stack, it makes unwinding easy.
Stack size is rarely an issue when you are dealing with logarithmic time complexities like binary search.
For example, one million records would be approximately take 20 recursive iteration depth. Billion records will approximately take 30 recursive iteration depth.
# n = 1,000,000 (million) log2(1,000,000) ~= 20 # n = 1,000,000,000 (billion) log2(1,00,000,000) ~= 30
In Python, a default stack size limit is 1000
.
# getting recursion limit import sys print(sys.getrecursionlimit()) # changing the recursion limit sys.setrecursionlimit(1500)