MCPcopy
hub / github.com/TheAlgorithms/Python / ascend_tree

Function ascend_tree

machine_learning/frequent_pattern_growth.py:213–240  ·  view source on GitHub ↗

Ascend the FP-Tree from a leaf node to its root, adding item names to the prefix path. Args: leaf_node: The leaf node to start ascending from. prefix_path: A list to store the item as they are ascended. Example: >>> data_set = [ ... ['A', 'B', 'C'],

(leaf_node: TreeNode, prefix_path: list[str])

Source from the content-addressed store, hash-verified

211
212
213def ascend_tree(leaf_node: TreeNode, prefix_path: list[str]) -> None:
214 """
215 Ascend the FP-Tree from a leaf node to its root, adding item names to the prefix
216 path.
217
218 Args:
219 leaf_node: The leaf node to start ascending from.
220 prefix_path: A list to store the item as they are ascended.
221
222 Example:
223 >>> data_set = [
224 ... ['A', 'B', 'C'],
225 ... ['A', 'C'],
226 ... ['A', 'B', 'E'],
227 ... ['A', 'B', 'C', 'E'],
228 ... ['B', 'E']
229 ... ]
230 >>> min_sup = 2
231 >>> fp_tree, header_table = create_tree(data_set, min_sup)
232
233 >>> path = []
234 >>> ascend_tree(fp_tree.children['A'], path)
235 >>> path # ascending from a leaf node 'A'
236 ['A']
237 """
238 if leaf_node.parent is not None:
239 prefix_path.append(leaf_node.name)
240 ascend_tree(leaf_node.parent, prefix_path)
241
242
243def find_prefix_path(base_pat: frozenset, tree_node: TreeNode | None) -> dict: # noqa: ARG001

Callers 1

find_prefix_pathFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected