@@ -85,9 +85,12 @@ def delete(self, label):
85
85
self .__reassignNodes (node , node .getLeft ())
86
86
#Has two children
87
87
else :
88
+ #Gets the max value of the left branch
88
89
tmpNode = self .getMax (node .getLeft ())
90
+ #Deletes the tmpNode
89
91
self .delete (tmpNode .getLabel ())
90
- self .__reassignNodes (node , tmpNode )
92
+ #Assigns the value to the node to delete and keesp tree structure
93
+ node .setLabel (tmpNode .getLabel ())
91
94
92
95
def getNode (self , label ):
93
96
curr_node = None
@@ -135,11 +138,13 @@ def empty(self):
135
138
return True
136
139
return False
137
140
138
- def preShow (self , curr_node ):
141
+ def __InOrderTraversal (self , curr_node ):
142
+ nodeList = []
139
143
if curr_node is not None :
140
- print (curr_node .getLabel ())
141
- self .preShow (curr_node .getLeft ())
142
- self .preShow (curr_node .getRight ())
144
+ nodeList .insert (0 , curr_node )
145
+ nodeList = nodeList + self .__InOrderTraversal (curr_node .getLeft ())
146
+ nodeList = nodeList + self .__InOrderTraversal (curr_node .getRight ())
147
+ return nodeList
143
148
144
149
def getRoot (self ):
145
150
return self .root
@@ -149,19 +154,44 @@ def __isRightChildren(self, node):
149
154
return True
150
155
return False
151
156
152
- def __reassignNodes (self ,node , newChildren ):
157
+ def __reassignNodes (self , node , newChildren ):
153
158
if (newChildren is not None ):
154
159
newChildren .setParent (node .getParent ())
155
160
if (node .getParent () is not None ):
156
161
#If it is the Right Children
157
- if (self .__isRightChildren (node )):a
158
- node .getParent ().setRight (newChildren )
162
+ if (self .__isRightChildren (node )):
163
+ node .getParent ().setRight (newChildren )
159
164
else :
160
165
#Else it is the left children
161
166
node .getParent ().setLeft (newChildren )
167
+
168
+ #This function traversal the tree. By default it returns an
169
+ #In order traversal list. You can pass a function to traversal
170
+ #The tree as needed by client code
171
+ def traversalTree (self , traversalFunction = None , root = None ):
172
+ if (traversalFunction is None ):
173
+ #Returns a list of nodes in preOrder by default
174
+ return self .__InOrderTraversal (self .root )
162
175
else :
163
- #It is the root of the tree
164
- node .setLabel (newChildren .getLabel ())
176
+ #Returns a list of nodes in the order that the users wants to
177
+ return traversalFunction (self .root )
178
+
179
+ #Returns an string of all the nodes labels in the list
180
+ #In Order Traversal
181
+ def __str__ (self ):
182
+ list = self .__InOrderTraversal (self .root )
183
+ str = ""
184
+ for x in list :
185
+ str = str + " " + x .getLabel ().__str__ ()
186
+ return str
187
+
188
+ def InPreOrder (curr_node ):
189
+ nodeList = []
190
+ if curr_node is not None :
191
+ nodeList = nodeList + InPreOrder (curr_node .getLeft ())
192
+ nodeList .insert (0 , curr_node .getLabel ())
193
+ nodeList = nodeList + InPreOrder (curr_node .getRight ())
194
+ return nodeList
165
195
166
196
def testBinarySearchTree ():
167
197
'''
@@ -179,16 +209,12 @@ def testBinarySearchTree():
179
209
Example After Deletion
180
210
7
181
211
/ \
182
- 3 14
183
- / \
184
- 1 6
185
- /
186
- 4
212
+ 1 4
213
+
187
214
'''
188
215
t = BinarySearchTree ()
189
216
t .insert (8 )
190
217
t .insert (3 )
191
- t .insert (10 )
192
218
t .insert (6 )
193
219
t .insert (1 )
194
220
t .insert (10 )
@@ -197,7 +223,8 @@ def testBinarySearchTree():
197
223
t .insert (4 )
198
224
t .insert (7 )
199
225
200
- t .preShow (t .getRoot ())
226
+ #Prints all the elements of the list in order traversal
227
+ print (t .__str__ ())
201
228
202
229
if (t .getNode (6 ) is not None ):
203
230
print ("The label 6 exists" )
@@ -216,8 +243,15 @@ def testBinarySearchTree():
216
243
t .delete (13 )
217
244
t .delete (10 )
218
245
t .delete (8 )
246
+ t .delete (3 )
247
+ t .delete (6 )
248
+ t .delete (14 )
219
249
220
- t .preShow (t .getRoot ())
250
+ #Gets all the elements of the tree In pre order
251
+ #And it prints them
252
+ list = t .traversalTree (InPreOrder , t .root )
253
+ for x in list :
254
+ print (x )
221
255
222
256
if __name__ == "__main__" :
223
257
testBinarySearchTree ()
0 commit comments