-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathBinary_Search_Tree.py
62 lines (53 loc) · 1.62 KB
/
Binary_Search_Tree.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def create_node(value):
return {'value': value, 'left': None, 'right': None}
def insert(root, value):
if root is None:
return create_node(value)
if value < root['value']:
root['left'] = insert(root['left'], value)
else:
root['right'] = insert(root['right'], value)
return root
def search(root, value):
if root is None:
return "Not Found"
if root['value'] == value:
return "Found"
if value < root['value']:
return search(root['left'], value)
else:
return search(root['right'], value)
def find_min(root):
while root['left'] is not None:
root = root['left']
return root
def delete(root, value):
if root is None:
return root
if value < root['value']:
root['left'] = delete(root['left'], value)
elif value > root['value']:
root['right'] = delete(root['right'], value)
else:
if root['left'] is None:
return root['right']
elif root['right'] is None:
return root['left']
min_node = find_min(root['right'])
root['value'] = min_node['value']
root['right'] = delete(root['right'], min_node['value'])
return root
def in_order(root, result=[]):
if root is not None:
in_order(root['left'], result)
result.append(root['value'])
in_order(root['right'], result)
return result
root = None
root = insert(root, 20)
root = insert(root, 10)
root = insert(root, 30)
root = insert(root, 25)
print(search(root, 25))
root = delete(root, 10)
print(in_order(root))