Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- import random
- import encodings
- class Node:
- def __init__(self, parent=None, children=[]):
- self.parent = parent
- self.children = children
- self.info = None
- self.name = random.randint(100000,999999)
- def isLeaf(self):
- return self.children is None
- def assign_parent(self, parent):
- self.parent = parent
- def __str__(self):
- return 'node name: ' + str(self.name) + ' ' + str(self.info) + ' # children: ' + str(len(self.children))\
- + ' parents: ' + str(self.parent)
- class Tree:
- def __init__(self):
- self.root = Node()
- def add_node(self, parent, node):
- node.assign_parent(parent)
- parent.children.append(node)
- class RNGTree(Tree):
- def __init__(self, seed=42):
- #self.rng = random.Random(seed)
- super(RNGTree, self).__init__()
- self.hash = lambda x: hashlib.md5(x.encode()).hexdigest()
- self.root.info = {'value': self.hash(str(seed))}
- def add(self, parent, id):
- n = Node()
- n.info = {'value': self.hash(str(parent.info['value'] * id))}
- self.add_node(parent, n)
- def __str__(self):
- res = []
- res.append(str(self.root))
- for child in self.root.children:
- res.append(str(child))
- return '\n'.join(res)
- t = RNGTree()
- for i in range(3):
- t.add(t.root, i)
- print(t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement