-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBSTShell.py
394 lines (394 loc) · 13.3 KB
/
BSTShell.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> bst = BSTNode(15)
>>> bst.value
15
>>> b = BST()
>>> b.insert(None, bst)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
b.insert(None, bst)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 19, in insert
return parent.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.insert(-1, bst)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
b.insert(-1, bst)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 15, in insert
elif parent and node.value<=parent.value:
AttributeError: 'int' object has no attribute 'value'
>>> b.insert(BSTNode(-1), bst)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
b.insert(BSTNode(-1), bst)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 18, in insert
parent.right = self.insert(parent.right, node)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 18, in insert
parent.right = self.insert(parent.right, node)
AttributeError: 'NoneType' object has no attribute 'right'
>>> b.root
<__main__.BSTNode object at 0x05C78ED0>
>>> b.root.value
15
>>> b.insert(b.root, BSTNode(10))
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
b.insert(b.root, BSTNode(10))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 16, in insert
parent.left = self.insert(parent.left, node)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 18, in insert
parent.right = self.insert(parent.right, node)
AttributeError: 'NoneType' object has no attribute 'right'
>>> b.root.value
15
>>> b.root.left.value
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
b.root.left.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.left
>>> print(b.root.left)
None
>>> b.parent
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
b.parent
AttributeError: 'BST' object has no attribute 'parent'
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> bst = BSTNode(15)
>>> b = BST()
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> bst = BSTNode(15)
>>> b = BST()
>>> b.insert(None, bst)
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
b.insert(None, bst)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 21, in insert
return parent.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root
<__main__.BSTNode object at 0x05CEE530>
>>> b.root.value
15
>>> b.insert(b.root, BSTNode(10))
15
>>> b.insert(b.root, BSTNode(20))
15
>>> b.insert(b.root, BSTNode(8))
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
b.insert(b.root, BSTNode(8))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 18, in insert
parent.left = self.insert(parent.left, node)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 17, in insert
elif parent and node.value<=parent.value:
AttributeError: 'int' object has no attribute 'value'
>>> bst = BSTNode(8)
>>> b.insert(b.root, BSTNode(8))
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
b.insert(b.root, BSTNode(8))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 18, in insert
parent.left = self.insert(parent.left, node)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 17, in insert
elif parent and node.value<=parent.value:
AttributeError: 'int' object has no attribute 'value'
>>> from BSTImplementation import BST
>>> parent
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
parent
NameError: name 'parent' is not defined
>>> insert()
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
insert()
NameError: name 'insert' is not defined
>>> BST.parent
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
BST.parent
AttributeError: type object 'BST' has no attribute 'parent'
>>> b.root
<__main__.BSTNode object at 0x05CEE530>
>>> b.root.value
15
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> bst = BSTNode(8)
>>> b = BST()
>>> bst = BSTNode(15)
>>> b.insert(None, bst)
'Root value:15'
>>> b.insert(b.root, BSTNode(10))
Left Node Value:10, Parent Value:15
parent:10
15
>>> b.insert(b.root, BSTNode(20))
Right Node Value:20, Parent Value:15
parent:20
15
>>> b.insert(b.root, BSTNode(8))
Left Node Value:8, Parent Value:15
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
b.insert(b.root, BSTNode(8))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 21, in insert
parent.left = self.insert(parent.left, node)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 19, in insert
elif parent and node.value<=parent.value:
AttributeError: 'int' object has no attribute 'value'
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> bst = BSTNode(15)
>>> b.insert(None, bst)
'Root value:15'
>>> b.insert(b.root, BSTNode(10))
Left Node Value:10, Parent Value:15
parent:10
<__main__.BSTNode object at 0x05ABF670>
>>> b.insert(b.root, BSTNode(8))
Left Node Value:8, Parent Value:15
Left Node Value:8, Parent Value:10
parent:8
<__main__.BSTNode object at 0x05ABF670>
>>> b.root.value
15
>>> b.root.left.value
10
>>> b.root.right.value
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
b.root.right.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.right
>>> b.insert(b.root, BSTNode(20))
Right Node Value:20, Parent Value:15
parent:20
<__main__.BSTNode object at 0x05ABF670>
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> b.insert(None, bst=BSTNode(8))
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
b.insert(None, bst=BSTNode(8))
TypeError: insert() got an unexpected keyword argument 'bst'
>>> b.insert(None, BSTNode(8))
'Root value:8'
>>> b.insert(b.root, BSTNode(15))
Right Node Value:15, Parent Value:8
Traceback (most recent call last):
File "<pyshell#52>", line 1, in <module>
b.insert(b.root, BSTNode(15))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 21, in insert
parent.right = self.insert(parent.right, node)
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 16, in insert
elif node.value<=parent.value:
AttributeError: 'NoneType' object has no attribute 'value'
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> b.insert(None, BSTNode(15))
'Root value:15'
>>> b.insert(b.root, BSTNode(15))
Left Node Value:15, Parent Value:15
parent:15
<__main__.BSTNode object at 0x05D1F650>
>>> b.insert(b.root, BSTNode(20))
Right Node Value:20, Parent Value:15
parent:20
<__main__.BSTNode object at 0x05D1F650>
>>> b.insert(b.root, BSTNode(8))
Left Node Value:8, Parent Value:15
Left Node Value:8, Parent Value:15
parent:8
<__main__.BSTNode object at 0x05D1F650>
>>> b.insert(b.root, BSTNode(17))
Right Node Value:17, Parent Value:15
Left Node Value:17, Parent Value:20
parent:17
<__main__.BSTNode object at 0x05D1F650>
>>> b.insert(b.root, BSTNode(25))
Right Node Value:25, Parent Value:15
Right Node Value:25, Parent Value:20
parent:25
<__main__.BSTNode object at 0x05D1F650>
>>> b.root.value
15
>>> b.right.value
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
b.right.value
AttributeError: 'BST' object has no attribute 'right'
>>> b.root.right.value
20
>>> b.root.left.value
15
>>> b.root.right.right.value
25
>>> b.root.right.right.value
25
>>> b.root.right.right.right.value
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
b.root.right.right.right.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.right.left.value
17
>>> b.root.right.left.left.value
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
b.root.right.left.left.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.left.left.value
8
>>> b.root.left.left.left.value
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
b.root.left.left.left.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.left.right.value
Traceback (most recent call last):
File "<pyshell#71>", line 1, in <module>
b.root.left.right.value
AttributeError: 'NoneType' object has no attribute 'value'
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST(BSTNode(15))
>>> b.insert(b.root, BSTNode(10))
Left Node Value:10, Parent Value:15
parent:10
<__main__.BSTNode object at 0x05829E90>
>>> b.insert(b.root, BSTNode(20))
Right Node Value:20, Parent Value:15
parent:20
<__main__.BSTNode object at 0x05829E90>
>>> b.insert(b.root, BSTNode(8))
Left Node Value:8, Parent Value:15
Left Node Value:8, Parent Value:10
parent:8
<__main__.BSTNode object at 0x05829E90>
>>> b.insert(b.root, BSTNode(12))
Left Node Value:12, Parent Value:15
Right Node Value:12, Parent Value:10
parent:12
<__main__.BSTNode object at 0x05829E90>
>>> b.insert(b.root, BSTNode(17))
Right Node Value:17, Parent Value:15
Left Node Value:17, Parent Value:20
parent:17
<__main__.BSTNode object at 0x05829E90>
>>> b.insert(b.root, BSTNode(25))
Right Node Value:25, Parent Value:15
Right Node Value:25, Parent Value:20
parent:25
<__main__.BSTNode object at 0x05829E90>
>>> b.min_node()
(<__main__.BSTNode object at 0x05D5F450>, 8)
>>> b.insert(b.root, BSTNode(1))
Left Node Value:1, Parent Value:15
Left Node Value:1, Parent Value:10
Left Node Value:1, Parent Value:8
parent:1
<__main__.BSTNode object at 0x05829E90>
>>> b.min_node()
(<__main__.BSTNode object at 0x05D5FB10>, 1)
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> b.insert_iter(BSTNode(16))
'Root value:16'
>>> b.insert_iter(BSTNode(10))
>>> b.root.value
16
>>> b.root.right.value
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
b.root.right.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.left
>>> b.root.left.value
Traceback (most recent call last):
File "<pyshell#89>", line 1, in <module>
b.root.left.value
AttributeError: 'NoneType' object has no attribute 'value'
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> b.insert_iter(BSTNode(16))
'Root value:16'
>>> b.insert_iter(BSTNode(10))
>>> b.root.value
16
>>> b.root.left.value
Traceback (most recent call last):
File "<pyshell#94>", line 1, in <module>
b.root.left.value
AttributeError: 'NoneType' object has no attribute 'value'
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> b.insert_iter(BSTNode(16))
'Root value:16'
>>> b.insert_iter(BSTNode(10))
Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
b.insert_iter(BSTNode(10))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 66, in insert_iter
print("Current:{}, current.right:{}".format(current.value, current.left.value))
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.insert_iter(BSTNode(20))
Right, 10
Traceback (most recent call last):
File "<pyshell#98>", line 1, in <module>
b.insert_iter(BSTNode(20))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 66, in insert_iter
print("Current:{}, current.right:{}".format(current.value, current.left.value))
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.value
16
>>> b.root.left.value
Traceback (most recent call last):
File "<pyshell#100>", line 1, in <module>
b.root.left.value
AttributeError: 'NoneType' object has no attribute 'value'
>>> b.root.right.value
10
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> b.insert_iter(BSTNode(16))
'Root value:16'
>>> b.insert_iter(BSTNode(10))
Traceback (most recent call last):
File "<pyshell#104>", line 1, in <module>
b.insert_iter(BSTNode(10))
File "D:/SatyamSinha/DS Programs/BSTImplementation.py", line 57, in insert_iter
print("Left, {}".format(current.value))
AttributeError: 'NoneType' object has no attribute 'value'
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>>
========== RESTART: D:/SatyamSinha/DS Programs/BSTImplementation.py ==========
>>> b = BST()
>>> b.insert_iter(BSTNode(16))
'Root value:16'
>>> b.insert_iter(BSTNode(10))
Current:16, Node:10
Previous Left 10
>>> b.insert_iter(BSTNode(20))
Current:16, Node:20
Previous Right 20
>>> b.max_node()
(<__main__.BSTNode object at 0x05899EB0>, 20)
>>> b.min_node()
(<__main__.BSTNode object at 0x05BDF470>, 10)
>>>