Skip to content

Commit 3c41ce1

Browse files
committed
update children count and remove which child
1 parent c840bab commit 3c41ce1

File tree

2 files changed

+12
-50
lines changed

2 files changed

+12
-50
lines changed

algorithms/binary_tree.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def insert(self, data):
1818
1919
@param data node data object to insert
2020
"""
21-
elif data < self.data:
21+
if data < self.data:
2222
if self.left == None:
2323
self.left = Node(data)
2424
else:
@@ -57,10 +57,10 @@ def delete(self, data):
5757
# get node containing data
5858
node, parent = self.lookup(data)
5959
if node != None:
60-
children_count = self.children_count(node)
60+
children_count = node.children_count()
6161
if children_count == 0:
6262
# if node has no children, just remove it
63-
if self.which_child(parent, node) == 'left':
63+
if parent.left is node:
6464
parent.left = None
6565
else:
6666
parent.right = None
@@ -139,33 +139,16 @@ def tree_data(self):
139139
yield node.data
140140
node = node.right
141141

142-
def which_child(self, parent, child):
143-
"""
144-
Test if child is parent's left child or parent's right child
145-
146-
@param parent parent node
147-
@param child parent's child node
148-
"""
149-
if parent == None:
150-
return None
151-
if parent.left == child:
152-
return 'left'
153-
else:
154-
return 'right'
155-
156-
def children_count(self, node):
142+
def children_count(self):
157143
"""
158144
Return the number of children
159145
160-
@param node node to get nb of children
161146
@returns number of children: 0, 1, 2
162147
"""
163-
if node == None:
164-
return None
165148
cnt = 0
166-
if node.left:
149+
if self.left:
167150
cnt += 1
168-
if node.right:
151+
if self.right:
169152
cnt += 1
170153
return cnt
171154

binary_tree_tutorial.txt

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Node:
197197
# get node containing data
198198
node, parent = self.lookup(data)
199199
if node != None:
200-
children_count = self.children_count(node)
200+
children_count = node.children_count()
201201
...
202202
[/code]
203203

@@ -215,57 +215,36 @@ Let's tackle the first possibility which is the easiest. We look for the node to
215215
...
216216
if children_count == 0:
217217
# if node has no children, just remove it
218-
if self.which_child(parent, node) == 'left':
218+
if parent.left is node:
219219
parent.left = None
220220
else:
221221
parent.right = None
222222
...
223223
[/code]
224224

225-
Note: children_count() returns the number of children of a node. which_child() tells if the node passed is the left child or right child of parent.
225+
Note: children_count() returns the number of children of a node.
226226

227227
Here is the function children_count:
228228

229229
[code lang="python"]
230230
class Node:
231231
...
232-
def children_count(node):
232+
def children_count(self):
233233
"""
234234
Returns the number of children
235235

236-
@param node node to get nb of children
237236
@returns number of children: 0, 1, 2
238237
"""
239238
if node == None:
240239
return None
241240
cnt = 0
242-
if node.left:
241+
if self.left:
243242
cnt += 1
244-
if node.right:
243+
if self.right:
245244
cnt += 1
246245
return cnt
247246
[/code]
248247

249-
And which_child():
250-
251-
[code lang="python"]
252-
class Node:
253-
...
254-
def which_child(parent, child):
255-
"""
256-
Test if child is parent's left child or parent's right child
257-
258-
@param parent parent node
259-
@param child parent's child node
260-
"""
261-
if parent == None:
262-
return None
263-
if parent.left == child:
264-
return 'left'
265-
else:
266-
return 'right'
267-
[/code]
268-
269248
For example, we want to remove node 1. Node 3 left child will be set to None.
270249

271250
[code lang="python"]

0 commit comments

Comments
 (0)