Do write a program to generate a hash tree according to Fast Algorithms for Mning Association, Apriori algorithm, with max leaf size 2 and output the nested list (or nested dict) of the hash tree hierarchically.
For example, the nested list is
and its corresponding hash tree is
2. Solution
I use Python without any libraries.
The following is my main function to return the result tree and print it out.
To achieve the above task, I create HashTree class which plays a key role here and implement following functions inside. The max leaf size of a hash tree is fixed as 2, so that I make a hash function first like below (3 is in below code is fixed because of the max size 2 and should be n + 1 for future work to make it flexible).
The following function finds the proper position to add a node and add it.
The following function is to find a suitable position to insert a node according to its hash value. In other words, this is to locate a leaf node which has no children or has list data set, not hash table in a hash tree to add a node.
This is for the node insert in a suitable position by linking the hash tree and the inserted node. There are two cases, need to split or not.
When the max size of leaf node reaches more than 3, it should be split.
The below function is to link the existing node with the added node. This is defined for reuse for cases; Split required or not.