@@ -152,7 +152,8 @@ def inorder(self, arr: list, node: Node):
152
152
def find_kth_smallest (self , k : int , node : Node ) -> int :
153
153
"""Return the kth smallest element in a binary search tree"""
154
154
arr : list = []
155
- self .inorder (arr , node ) # append all values to list using inorder traversal
155
+ # append all values to list using inorder traversal
156
+ self .inorder (arr , node )
156
157
return arr [k - 1 ]
157
158
158
159
@@ -162,7 +163,8 @@ def postorder(curr_node):
162
163
"""
163
164
node_list = list ()
164
165
if curr_node is not None :
165
- node_list = postorder (curr_node .left ) + postorder (curr_node .right ) + [curr_node ]
166
+ node_list = postorder (curr_node .left ) + \
167
+ postorder (curr_node .right ) + [curr_node ]
166
168
return node_list
167
169
168
170
@@ -186,36 +188,36 @@ def binary_search_tree():
186
188
Traceback (most recent call last):
187
189
...
188
190
IndexError: Warning: Tree is empty! please use another.
189
- """
190
- testlist = (8 , 3 , 6 , 1 , 10 , 14 , 13 , 4 , 7 )
191
- t = BinarySearchTree ()
192
- for i in testlist :
193
- t .insert (i )
194
-
195
- # Prints all the elements of the list in order traversal
196
- print (t )
197
-
198
- if t .search (6 ) is not None :
199
- print ("The value 6 exists" )
200
- else :
201
- print ("The value 6 doesn't exist" )
202
-
203
- if t .search (- 1 ) is not None :
204
- print ("The value -1 exists" )
205
- else :
206
- print ("The value -1 doesn't exist" )
207
-
208
- if not t .empty ():
209
- print ("Max Value: " , t .get_max ().value )
210
- print ("Min Value: " , t .get_min ().value )
211
191
212
- for i in testlist :
213
- t .remove (i )
214
- print (t )
192
+ >>> t = BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
193
+ >>> t.search(6)
194
+ {'6': (4, 7)}
195
+ >>> t.search(-1)
196
+ >>> print(t.root.value)
197
+ 8
198
+ >>> print(t)
199
+ {'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, None)})})}
200
+ >>> t.get_max().value
201
+ 14
202
+ >>> t.get_min().value
203
+ 1
204
+ >>> t.find_kth_smallest(k=2,node=t.root)
205
+ 3
206
+ >>> t.find_kth_smallest(k=5,node=t.root)
207
+ 7
208
+ >>> t.remove(7)
209
+ >>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
210
+ 8 3 1 6 4 10 14 13
211
+ >>> t.remove(10)
212
+ >>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
213
+ 8 3 1 6 4 14 13
214
+ >>> t.remove(14)
215
+ >>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
216
+ 8 3 1 6 4 13
217
+ """
215
218
216
219
217
220
if __name__ == "__main__" :
218
221
import doctest
219
222
220
223
doctest .testmod ()
221
- # binary_search_tree()
0 commit comments