哈希树的简单实现A simple implementation of Hash Tree

# coding=utf-8"""
A simple implementation of Hash Tree
哈希树的简单实现
"""
from functools import reduceclass HashTreeNodeobject):def __init__self, name=''):self.val = 0self.name = nameself.level = 0self.children = {}def addBagself, bag):"""Note that bag must be sorted"""if bag:node = self.children.getbag[0], HashTreeNodename=bag[0]))node.addBagbag[1:])self.children[bag[0]] = nodeself.level = lenbag)def countself, transaction):"""count the child who matches bag, suppose that current node matches"""if self.level == 0:self.val += 1elif self.level == 1:for t in transaction:if t in self.children: self.children[t].val += 1else:for i in range0, lentransaction)):t = transaction[i]if t in self.children:self.children[t].counttransaction[i:])def getself, theta):"""if self.level == 0:return [[self.name]] if self.val >= theta else Noneelse:children_res = [self.children[i].gettheta) for i in sortedself.children.keys))]total = reducelambda res, x: res + x, filterlambda x: x, children_res), [])return maplambda c: [self.name] + c, total)"""return [[c.name for c in items] for items in self.getNodestheta)]def getNodesself, theta):if self.level == 0:return [[self]] if self.val >= theta else Noneelse:children_res = [self.children[i].getNodestheta) for i in sortedself.children.keys))]total = reducelambda res, x: res + x, [x for x in children_res if x], [])return [[self] + c for c in total]def __str__self):return '%s : %s)' % self.name, '; '.join[stri) for i in listself.children.values))]))def sameNodenode1, node2):return node1.name == node2.namedef sameNodesnodes1, nodes2):func = lambda n: n.namereturn listmapfunc, nodes1)) == listmapfunc, nodes2))class HashTreeobject):"""Note that all bags must be sorted"""def __init__self, bags):self.root = HashTreeNode)self.root.val = 0for b in bags:if b: self.root.addBagb)def countself, transactions):for t in transactions: self.root.countt)def getself, theta):res = [c[1:] for c in self.root.gettheta)]return [] if res == [[]] else resdef getNodesself, theta):res = [c[1:] for c in self.root.getNodestheta)]return [] if res == [[]] else resdef __str__self):return strself.root)if __name__ == '__main__':to_count = [[1, 2], [2, 4], [1, 3], [1, 5], [3, 4], [2, 7], [6, 8]]tree = HashTreeto_count)transactions = [[1, 2, 3], [1, 2, 4], [2, 4, 6, 8], [1, 3, 5, 7]]tree.counttransactions)print'Frequency with transactions', transactions)printtree.get2))printtree.get1))

 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注