forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
367 lines (318 loc) · 9.67 KB
/
tree.py
File metadata and controls
367 lines (318 loc) · 9.67 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Author: OMKAR PATHAK
Created On: 9th August 2017
Node class to create a node
for binary tree
"""
import inspect
class Node(object):
"""
Node class for creating a node for tree.
Each node has its data and a pointer
that points to next node in the Linked List
"""
def __init__(self, data=None):
self.left = None
self.right = None
self.data = data
def set_left(self, node):
"""
for setting the left child of the node
"""
self.left = node
def set_right(self, node):
"""
for setting the right child of the node
"""
self.right = node
def get_left(self):
"""
for getting the left child of the node
"""
return self.left
def get_right(self):
"""
for getting the right child of the node
"""
return self.right
def set_data(self, data):
"""
for setting the data of the node
"""
self.data = data
def get_data(self):
"""
for getting the data of the node
"""
return self.data
@staticmethod
def get_code():
"""
returns the code of the current class
"""
return inspect.getsource(Node)
class BinaryTree(object):
"""
BinaryTree class to create a binary tree
"""
def __init__(self):
self._in_order = []
self._pre_order = []
self._post_order = []
def inorder(self, root):
"""
in this we traverse first to the leftmost node,
then print its data and then traverse for rightmost node
:param root: Node object
"""
if root:
# traverse to leftmost child
self.inorder(root.get_left())
# get the data of current node
self._in_order.append(root.get_data())
# traverse to rightmost child
self.inorder(root.get_right())
return self._in_order
def preorder(self, root):
"""
in this we first print the root node and then
traverse towards leftmost node and then to the rightmost node
:param root: Node object
"""
if root:
# get the data of current node
self._pre_order.append(root.get_data())
# traverse to leftmost child
self.preorder(root.get_left())
# traverse to rightmost child
self.preorder(root.get_right())
return self._pre_order
def postorder(self, root):
"""
in this we first traverse to the leftmost node and then
to the rightmost node and then print the data
:param root: Node object
"""
if root:
# traverse to leftmost child
self.postorder(root.get_left())
# traverse to rightmost child
self.postorder(root.get_right())
# traverse to rightmost child
self._post_order.append(root.get_data())
return self._post_order
@staticmethod
def get_code():
"""
returns the code of the current class
"""
return inspect.getsource(BinaryTree)
class BSTNode(object):
"""
class for creating a node for binary Search tree
"""
def __init__(self, data):
self.data = data
self.left_child = None
self.right_child = None
self._in_order = []
self._pre_order = []
self._post_order = []
def set_left(self, node):
"""
for setting the left child of the node
"""
self.left_child = node
def set_right(self, node):
"""
for setting the right child of the node
"""
self.right_child = node
def get_left(self):
"""
returns the left child of the current node
"""
return self.left_child
def get_right(self):
"""
returns the right child of the current node
"""
return self.right_child
def set_data(self, data):
"""
for setting the data of a node
"""
self.data = data
def get_data(self):
"""
returns the data of the current node
"""
return self.data
def insert(self, data):
"""
For inserting the data in the Tree
"""
if self.data == data:
# As BST cannot contain duplicate data
return False
elif data < self.data:
# Data less than the root data is placed to the left of the root
if self.left_child:
return self.left_child.insert(data)
else:
self.left_child = BSTNode(data)
return True
else:
# Data greater than the root data is placed to the right of the root
if self.right_child:
return self.right_child.insert(data)
else:
self.right_child = BSTNode(data)
return True
@staticmethod
def min_val_bst_node(bst_node):
"""
for returning the node with the lowest value
"""
current = bst_node
# loop down to find the leftmost leaf
while current.left_child is not None:
current = current.left_child
return current
def delete(self, data):
"""
For deleting the bst_node
"""
if self is None:
return None
# if current bst_node's data is less than that of root bst_node,
# then only search in left subtree else right subtree
if data < self.data:
self.left_child = self.left_child.delete(data)
elif data > self.data:
self.right_child = self.right_child.delete(data)
else:
# deleting bst_node with one child
if self.left_child is None:
temp = self.right_child
return temp
elif self.right_child is None:
temp = self.left_child
return temp
# deleting bst_node with two children
# first get the inorder successor
temp = self.min_val_bst_node(self.right_child)
self.data = temp.data
self.right_child = self.right_child.delete(temp.data)
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.left_child:
return self.left_child.find(data)
else:
return False
else:
if self.right_child:
return self.right_child.find(data)
else:
return False
def inorder(self, root):
"""
in this we first traverse to the leftmost node and
then print the data and then move to the rightmost child
:param root: Node object
"""
if root:
# traverse to leftmost child
self.inorder(root.get_left())
# get the data of current node
self._in_order.append(root.get_data())
# traverse to rightmost child
self.inorder(root.get_right())
return self._in_order
def preorder(self, root):
"""
in this we first print the root node and then
traverse towards leftmost node and then to the rightmost node
:param root: Node object
"""
if root:
# get the data of current node
self._pre_order.append(root.get_data())
# traverse to leftmost child
self.preorder(root.get_left())
# traverse to rightmost child
self.preorder(root.get_right())
return self._pre_order
def postorder(self, root):
"""
in this we first traverse to the leftmost node
and then to the rightmost node and then print the data
:param root: Node object
"""
if root:
# traverse to leftmost child
self.postorder(root.get_left())
# traverse to rightmost child
self.postorder(root.get_right())
# get the data of current node
self._post_order.append(root.get_data())
return self._post_order
@staticmethod
def get_code():
"""
returns the code of current class
"""
return inspect.getsource(BSTNode)
class BinarySearchTree(object):
def __init__(self):
self.root = None
def insert(self, data):
"""
inserts a node in the tree
"""
if self.root:
return self.root.insert(data)
else:
self.root = BSTNode(data)
return True
def delete(self, data):
"""
deletes the node with the specified data from the tree
"""
if self.root is not None:
return self.root.delete(data)
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
def preorder(self):
"""
finding the preorder of the tree
"""
if self.root is not None:
return self.root.preorder(self.root)
def inorder(self):
"""
finding the inorder of the tree
"""
if self.root is not None:
return self.root.inorder(self.root)
def postorder(self):
"""
finding the postorder of the tree
"""
if self.root is not None:
return self.root.postorder(self.root)
@staticmethod
def get_code():
"""
returns the code of the current class
"""
return inspect.getsource(BinarySearchTree)