-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtree.py
37 lines (27 loc) · 1010 Bytes
/
tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from collections import namedtuple
class Exemplar:
__slots__ = ("label", "attributes", "count")
def __init__(self, label, attributes, count):
self.label = label
self.attributes = attributes
self.count = count
BooleanTreeNode = namedtuple("BooleanTreeNode", "attribute value gt lt")
FinalTreeNode = namedtuple("FinalTreeNode", "label count")
# I like namedtuples, maybe too much.
def tree2json(node):
# NB we don't save the counts - they're not needed at runtime.
if hasattr(node, "label"):
return {"label": node.label}
return {
"attribute": node.attribute,
"value": node.value,
"gt": tree2json(node.gt),
"lt": tree2json(node.lt),
}
def json2tree(node):
if "label" in node:
return FinalTreeNode(node["label"], 0)
return BooleanTreeNode(node["attribute"],
node["value"],
json2tree(node["gt"]),
json2tree(node["lt"]))