Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb816ca

Browse files
author
Eric Naeseth
committed
Merge children in when we remove a node.
1 parent 3258031 commit bb816ca

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

fp_growth.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ def conditional_tree_from_paths(paths, minimum_support):
224224
# Finally, remove the nodes corresponding to the item for which this
225225
# conditional tree was generated.
226226
for node in tree.nodes(condition_item):
227-
node.parent.remove(item)
228-
227+
if node.parent is not None: # the node might already be an orphan
228+
node.parent.remove(node)
229+
229230
return tree
230231

231232
class FPNode(object):
@@ -266,6 +267,17 @@ def remove(self, child):
266267
del self._children[child.item]
267268
child.parent = None
268269
self._tree._removed(child)
270+
for sub_child in child.children:
271+
try:
272+
# Merger case: we already have a child for that item, so
273+
# add the sub-child's count to our child's count.
274+
self._children[sub_child.item]._count += sub_child.count
275+
sub_child.parent = None # it's an orphan now
276+
except KeyError:
277+
# Turns out we don't actually have a child, so just add
278+
# the sub-child as our own child.
279+
self.add(sub_child)
280+
child._children = {}
269281
else:
270282
raise ValueError("that node is not a child of this node")
271283
except KeyError:

0 commit comments

Comments
 (0)