Skip to content

Instantly share code, notes, and snippets.

@abhijeet-talaulikar
Last active October 28, 2021 04:56
Show Gist options
  • Save abhijeet-talaulikar/96185b8981f557a3f18f43b5c06ad21a to your computer and use it in GitHub Desktop.
Save abhijeet-talaulikar/96185b8981f557a3f18f43b5c06ad21a to your computer and use it in GitHub Desktop.
insert_tree.py
def insert_tree(prefix, suffix, current_root, fp_tree, node_link_dict, transaction_count):
# Check if route to the prefix node exists in tree
# If exists, get the node
new_root = tree_contains_node(prefix, current_root, fp_tree)
if new_root == False:
# If route doesn't exist, create new child node to current root
new_root = {'name':prefix, 'count':transaction_count, 'parent': current_root}
# Add the link to node link dictionary
node_link_dict[prefix].append(new_root)
# Add new node to tree
fp_tree.append(new_root)
else:
# If route exists, increase its count by 1
new_root['count'] += transaction_count
# If reached the end of transaction, stop
if len(suffix) == 0:
return
# Recursively call itself
insert_tree(suffix[0], suffix[1:], new_root, fp_tree, node_link_dict, transaction_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment