-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursive_bs.py
28 lines (23 loc) · 1009 Bytes
/
recursive_bs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
""" In this programme we are going to implement binary search tree using recursion method"""
def recursive_bs(array,target,low,high):
while(low <= high):
# the // will leave remainder as zero if you don't use it then you may run into error
mid = (low+high )//2
if (target == array[mid]):
return array[mid]
elif (target < array[mid]):
return recursive_bs(array,target,low,mid-1)
elif (target > array[mid]):
return recursive_bs(array,target,mid+1, high)
print('Element not found in the array ')
return False
#arr = [12,14,17,18,19,22,25,28,30]
#print(recursive_bs(arr,1,0,len(arr)))
# another way to take the input from the user
num = int(input("Enter the number of elements in your list:\n"))
array = []
for i in range(num):
array.append(int(input('Enter the element into your array:\n')))
array.sort()
target =int(input("Enter the element you are looking for:\n"))
print(recursive_bs(array,target,0,len(array)))