@@ -135,14 +135,14 @@ def bt_delete(root, data):
135
135
# get node containing data
136
136
node , parent = bt_lookup (root , data )
137
137
if node != None :
138
- childs_count = bt_childs_count (node )
139
- if childs_count == 0 :
140
- # if node has no childs , just remove it
138
+ children_count = bt_children_count (node )
139
+ if children_count == 0 :
140
+ # if node has no children , just remove it
141
141
if bt_which_child (parent , node ) == 'left' :
142
142
parent .left = None
143
143
else :
144
144
parent .right = None
145
- elif childs_count == 1 :
145
+ elif children_count == 1 :
146
146
# if node has 1 child
147
147
# replace node by its child
148
148
if node .left :
@@ -152,10 +152,12 @@ def bt_delete(root, data):
152
152
node .data = node .right .data
153
153
node .right = None
154
154
else :
155
- # if node has 2 childs
155
+ # if node has 2 children
156
156
# find its successor
157
+ parent = node
157
158
successor = node .right
158
159
while successor .left :
160
+ parent = successor
159
161
successor = successor .left
160
162
node .data = successor .data
161
163
# if the successor has a child, replace it with its child
@@ -164,7 +166,7 @@ def bt_delete(root, data):
164
166
successor .data = successor .right .data
165
167
successor .right = None
166
168
else :
167
- node . right = None
169
+ parent . left = None
168
170
169
171
def bt_which_child (parent , child ):
170
172
"""
@@ -180,12 +182,12 @@ def bt_which_child(parent, child):
180
182
else :
181
183
return 'right'
182
184
183
- def bt_childs_count (node ):
185
+ def bt_children_count (node ):
184
186
"""
185
- Return the number of childs
187
+ Return the number of children
186
188
187
- @param node node to get nb of childs
188
- @returns number of childs : 0, 1, 2
189
+ @param node node to get nb of children
190
+ @returns number of children : 0, 1, 2
189
191
"""
190
192
if node == None :
191
193
return None
0 commit comments