forked from iharsh234/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinarySearchTree.py
106 lines (83 loc) · 2.32 KB
/
BinarySearchTree.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
class node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.head = None
def Insert(self, value):
temp = node(value)
if(self.head == None):
self.head = temp
else:
current = self.head
while current != None:
if value < current.data:
if current.left != None:
current = current.left
else:
current.left = temp
return
else:
if current.right != None:
current = current.right
else:
current.right = temp
return
def Search(self, value):
current = self.head
while current != None:
if value < current.data:
current = current.left
elif value > current.data:
current = current.right
else:
print("Element " + str(value) + " Found")
return
print("Element " + str(value) + " not Found")
def Min_Value(self, head):
while head.left != None:
head = head.left
return head.data
def Delete_Key(self, head, value):
if head == None:
return head
if value < head.data:
head.left = self.Delete_Key(head.left, value)
elif value > head.data:
head.right = self.Delete_Key(head.right, value)
else:
if head.right == None:
return head.left
elif head.left == None:
return head.right
head.data = self.Min_Value(head.right)
head.right = self.Delete_Key(head.right, head.data)
return head
def Delete(self, value):
self.head = self.Delete_Key(self.head, value)
BST = BinarySearchTree()
BST.Insert(5)
BST.Insert(7)
BST.Insert(9)
BST.Insert(8)
BST.Insert(6)
BST.Insert(4)
BST.Search(9)
BST.Search(2)
BST.Delete(7)
BST.Delete(5)
BST.Delete(4)
BST.Search(9)
BST.Search(2)
BST.Search(5)
BST.Search(6)
''' Output
Element 9 Found
Element 2 not Found
Element 9 Found
Element 2 not Found
Element 5 not Found
Element 6 Found
'''