-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathBST.py
161 lines (146 loc) · 5.01 KB
/
BST.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self, value):
node = Node(value)
temp = self.root
if temp is None:
self.root = node
else:
while True:
if temp.value == value :
print("Redundant values are not allowed!")
break
if temp.value > value:
if temp.left is None:
temp.left = node
break
temp = temp.left
else:
if temp.right is None:
temp.right = node
break
temp = temp.right
def deletion(self, value):
temp = self.root
if temp is None:
print("Tree is Empty!")
else:
while temp.value != value:
temp3 = temp
if temp.value > value:
if temp.left is None:
print("Value not found!")
break
temp = temp.left
else:
if temp.value < value:
if temp.right is None:
print("Value not found!")
break
temp = temp.right
else:
while True:
if temp.left:
temp2 = temp.left
while True:
if temp2.right:
temp3 = temp2
temp2 = temp2.right
else:
temp.value = temp2.value
temp = temp2
break
elif temp.right:
temp2 = temp.right
while True:
if temp2.left:
temp3 = temp2
temp2 = temp2.left
else:
temp.value = temp2.value
temp = temp2
break
else:
if temp3.left :
if temp3.left.value == temp.value :
temp3.left = None
temp3.right = None
else :
temp3.right = None
break
def inorder(self):
root = self.root
print("Inorder :", end="")
def inorderp(root):
if root:
inorderp(root.left)
print(root.value, end=" ")
inorderp(root.right)
inorderp(root)
print()
def postorder(self):
root = self.root
print("Postorder :", end=" ")
def postorderp(root):
if root:
postorderp(root.left)
postorderp(root.right)
print(root.value, end=" ")
postorderp(root)
print()
def preorder(self):
root = self.root
print("Preorder :", end="")
def preorderp(root):
if root:
print(root.value, end=" ")
preorderp(root.left)
preorderp(root.right)
preorderp(root)
print()
def search(self, value):
temp = self.root
flag = 0
if temp is None:
print("Tree is Empty!")
else:
while temp is not None:
if temp.value == value:
flag = 1
break
elif temp.value < value:
temp = temp.right
else:
temp = temp.left
if flag == 1:
print("Value found in the tree")
return True
else:
print("Value not found in the tree")
return False
tree = BinarySearchTree()
print("Enter the operations you want to perform :\n1 ,for insertion\n2,for deletion\n3,for printing tree in postorder\n4, in inorder\n5,in preorder \n 6 to exit \n ")
while True :
input1 = int(input("Enter the number: "))
if input1 == 1 :
tree.insert(int(input("Enter the number you want to insert : ")))
print()
elif input1 == 2 :
tree.deletion(int(input("Enter the you wanna delete :")))
print()
elif input1 == 3:
tree.postorder()
elif input1 == 4:
tree.inorder()
elif input1 == 5:
tree.preorder()
elif input1 == 6 :
break
else :
print("Invalid Key !")