-
Notifications
You must be signed in to change notification settings - Fork 436
/
BinarySearchTree.py
214 lines (181 loc) · 6.15 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Author: OMKAR PATHAK
# This program illustrates an example of Binary Search Tree using Python
# Binary Search Tree, is a node-based binary tree data structure which has the following properties:
#
# The left subtree of a node contains only nodes with keys less than the node’s key.
# The right subtree of a node contains only nodes with keys greater than the node’s key.
# The left and right subtree each must also be a binary search tree.
# There must be no duplicate nodes.
class Node(object):
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
def insert(self, data):
''' For inserting the data in the Tree '''
if self.data == data:
return False # As BST cannot contain duplicate data
elif data < self.data:
''' Data less than the root data is placed to the left of the root '''
if self.leftChild:
return self.leftChild.insert(data)
else:
self.leftChild = Node(data)
return True
else:
''' Data greater than the root data is placed to the right of the root '''
if self.rightChild:
return self.rightChild.insert(data)
else:
self.rightChild = Node(data)
return True
def minValueNode(self, node):
current = node
# loop down to find the leftmost leaf
while(current.leftChild is not None):
current = current.leftChild
return current
def maxValueNode(self, node):
current = node
# loop down to find the leftmost leaf
while(current.rightChild is not None):
current = current.rightChild
return current
def delete(self, data,root):
''' For deleting the node '''
if self is None:
return None
# if current node's data is less than that of root node, then only search in left subtree else right subtree
if data < self.data:
self.leftChild = self.leftChild.delete(data,root)
elif data > self.data:
self.rightChild = self.rightChild.delete(data,root)
else:
# deleting node with one child
if self.leftChild is None:
if self == root:
temp = self.minValueNode(self.rightChild)
self.data = temp.data
self.rightChild = self.rightChild.delete(temp.data,root)
temp = self.rightChild
self = None
return temp
elif self.rightChild is None:
if self == root:
temp = self.maxValueNode(self.leftChild)
self.data = temp.data
self.leftChild = self.leftChild.delete(temp.data,root)
temp = self.leftChild
self = None
return temp
# deleting node with two children
# first get the inorder successor
temp = self.minValueNode(self.rightChild)
self.data = temp.data
self.rightChild = self.rightChild.delete(temp.data,root)
return self
def find(self, data):
''' This function checks whether the specified data is in tree or not '''
if(data == self.data):
return True
elif(data < self.data):
if self.leftChild:
return self.leftChild.find(data)
else:
return False
else:
if self.rightChild:
return self.rightChild.find(data)
else:
return False
def preorder(self):
'''For preorder traversal of the BST '''
if self:
print(str(self.data), end = ' ')
if self.leftChild:
self.leftChild.preorder()
if self.rightChild:
self.rightChild.preorder()
def inorder(self):
''' For Inorder traversal of the BST '''
if self:
if self.leftChild:
self.leftChild.inorder()
print(str(self.data), end = ' ')
if self.rightChild:
self.rightChild.inorder()
def postorder(self):
''' For postorder traversal of the BST '''
if self:
if self.leftChild:
self.leftChild.postorder()
if self.rightChild:
self.rightChild.postorder()
print(str(self.data), end = ' ')
class Tree(object):
def __init__(self):
self.root = None
def insert(self, data):
if self.root:
return self.root.insert(data)
else:
self.root = Node(data)
return True
def delete(self, data):
if self.root is not None:
return self.root.delete(data,self.root)
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
def preorder(self):
if self.root is not None:
print()
print('Preorder: ')
self.root.preorder()
def inorder(self):
print()
if self.root is not None:
print('Inorder: ')
self.root.inorder()
def postorder(self):
print()
if self.root is not None:
print('Postorder: ')
self.root.postorder()
if __name__ == '__main__':
tree = Tree()
tree.insert(10)
tree.insert(12)
tree.insert(5)
tree.insert(4)
tree.insert(20)
tree.insert(8)
tree.insert(7)
tree.insert(15)
tree.insert(13)
print(tree.find(1))
print(tree.find(12))
''' Following tree is getting created:
10
/ \
5 12
/ \ \
4 8 20
/ /
7 15
/
13
'''
tree.preorder()
tree.inorder()
tree.postorder()
print('\n\nAfter deleting 20')
tree.delete(20)
tree.inorder()
tree.preorder()
print('\n\nAfter deleting 10')
tree.delete(10)
tree.inorder()
tree.preorder()