-
Notifications
You must be signed in to change notification settings - Fork 1
/
BINARY_TREE.py
79 lines (78 loc) · 2.29 KB
/
BINARY_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class TREE:
def __init__(self,data):
self.left=None
self.right=None
self.data=data
def insertion_binary_search_tree(root,data):
nn=TREE(data)
if root is None:
return TREE(data)
else:
if root.data==data:
return root
elif root.data>data:
root.left=insertion_binary_search_tree(root.left,data)
else:
root.right=insertion_binary_search_tree(root.right,data)
return root
def inorder(root):
if root:
inorder(root.left)
print(root.data)
inorder(root.right)
def preorder(root):
if root:
print(root.data)
preorder(root.left)
preorder(root.right)
def postorder(root):
if root:
postorder(root.left)
postorder(root.right)
print(root.data)
def search(root,element):
if root.data==element:
return True
elif root.data>element:
return search(root.left,element)
return search(root.right,element)
def find_max(root):
temp=root
while temp.right:
temp=temp.right
return temp
def deletion_binary_search_tree(root,data):
if root is None:
return "TREE IS EMPTY"
if root.data<data:
root.right=deletion_binary_search_tree(root.right,data)
elif root.data>data:
root.left=deletion_binary_search_tree(root.left,data)
else:
if root.left is None:
temp=root.right
root=None
return temp
if root.right is None :
temp=root.left
root=None
return temp
temp=find_max(root.left)
root.data=temp.data
root.left=deletion_binary_search_tree(root.left,temp)
return root
root=TREE(10)
root=insertion_binary_search_tree(root,1)
root=insertion_binary_search_tree(root,2)
root=insertion_binary_search_tree(root,3)
root=insertion_binary_search_tree(root,40)
root=insertion_binary_search_tree(root,50)
root=insertion_binary_search_tree(root,60)
root=deletion_binary_search_tree(root,60)
print(f"inorder : : ")
inorder(root)
# print(f"preorder: : ")
# preorder(root)
# print(f"postorder: : ")
# postorder(root)
# print ("YES ELEMENT FOUND IN THIS TREE") if search(root,1) else print("ELEMENT NOT FOUND IN THIS TREE")