Skip to content

Commit 687b18e

Browse files
committed
sync with tutorial
1 parent ab1ca56 commit 687b18e

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

binary_tree.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ def bt_delete(root, data):
135135
# get node containing data
136136
node, parent = bt_lookup(root, data)
137137
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
141141
if bt_which_child(parent, node) == 'left':
142142
parent.left = None
143143
else:
144144
parent.right = None
145-
elif childs_count == 1:
145+
elif children_count == 1:
146146
# if node has 1 child
147147
# replace node by its child
148148
if node.left:
@@ -152,10 +152,12 @@ def bt_delete(root, data):
152152
node.data = node.right.data
153153
node.right = None
154154
else:
155-
# if node has 2 childs
155+
# if node has 2 children
156156
# find its successor
157+
parent = node
157158
successor = node.right
158159
while successor.left:
160+
parent = successor
159161
successor = successor.left
160162
node.data = successor.data
161163
# if the successor has a child, replace it with its child
@@ -164,7 +166,7 @@ def bt_delete(root, data):
164166
successor.data = successor.right.data
165167
successor.right = None
166168
else:
167-
node.right = None
169+
parent.left = None
168170

169171
def bt_which_child(parent, child):
170172
"""
@@ -180,12 +182,12 @@ def bt_which_child(parent, child):
180182
else:
181183
return 'right'
182184

183-
def bt_childs_count(node):
185+
def bt_children_count(node):
184186
"""
185-
Return the number of childs
187+
Return the number of children
186188
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
189191
"""
190192
if node == None:
191193
return None

tests/test_binary_tree.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def test_binary_tree(self):
2727
# test lookup
2828
node, parent = bt.lookup(9)
2929
self.assertTrue(node == None)
30+
# check if returned node and parent are correct
3031
node, parent = bt.lookup(11)
3132
self.assertTrue(node.data == 11)
3233
self.assertTrue(parent.data == 13)
@@ -47,15 +48,15 @@ def test_binary_tree(self):
4748
t.append(d)
4849
self.assertEquals(t, [7, 10, 11, 13, 14, 15, 17])
4950

50-
# delete a node with 2 childs
51+
# delete a node with 2 children
5152
bt.delete(13)
5253
# check the content of the tree inorder
5354
t = []
5455
for d in bt.tree_data():
5556
t.append(d)
5657
self.assertEquals(t, [7, 10, 11, 14, 15, 17])
5758

58-
# delete a node with 2 childs
59+
# delete a node with 2 children
5960
bt.delete(15)
6061
# check the content of the tree inorder
6162
t = []

0 commit comments

Comments
 (0)