2017-10-27 02:50:18 +08:00
|
|
|
class Record():
|
|
|
|
|
def __init__(self, record_id, parent_id):
|
|
|
|
|
self.record_id = record_id
|
|
|
|
|
self.parent_id = parent_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Node():
|
|
|
|
|
def __init__(self, node_id):
|
|
|
|
|
self.node_id = node_id
|
|
|
|
|
self.children = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def BuildTree(records):
|
|
|
|
|
root = None
|
|
|
|
|
records.sort(key=lambda x: x.record_id)
|
|
|
|
|
ordered_id = [i.record_id for i in records]
|
|
|
|
|
if records:
|
|
|
|
|
if ordered_id[-1] != len(ordered_id) - 1:
|
2018-08-13 21:16:22 -04:00
|
|
|
raise ValueError('Tree must be continuous')
|
2017-10-27 02:50:18 +08:00
|
|
|
if ordered_id[0] != 0:
|
2018-08-13 21:16:22 -04:00
|
|
|
raise ValueError('Tree must start with id 0')
|
2017-10-27 02:50:18 +08:00
|
|
|
trees = []
|
|
|
|
|
parent = {}
|
|
|
|
|
for i in range(len(ordered_id)):
|
|
|
|
|
for j in records:
|
|
|
|
|
if ordered_id[i] == j.record_id:
|
|
|
|
|
if j.record_id == 0:
|
|
|
|
|
if j.parent_id != 0:
|
2018-08-13 21:16:22 -04:00
|
|
|
raise ValueError('Root node cannot have a parent')
|
2017-10-27 02:50:18 +08:00
|
|
|
if j.record_id < j.parent_id:
|
2018-08-13 21:16:22 -04:00
|
|
|
raise ValueError('Parent id must be lower than child id')
|
2017-10-27 02:50:18 +08:00
|
|
|
if j.record_id == j.parent_id:
|
|
|
|
|
if j.record_id != 0:
|
2018-08-13 21:16:22 -04:00
|
|
|
raise ValueError('Tree is a cycle')
|
2017-10-27 02:50:18 +08:00
|
|
|
trees.append(Node(ordered_id[i]))
|
|
|
|
|
for i in range(len(ordered_id)):
|
|
|
|
|
for j in trees:
|
|
|
|
|
if i == j.node_id:
|
|
|
|
|
parent = j
|
|
|
|
|
for j in records:
|
|
|
|
|
if j.parent_id == i:
|
|
|
|
|
for k in trees:
|
|
|
|
|
if k.node_id == 0:
|
|
|
|
|
continue
|
|
|
|
|
if j.record_id == k.node_id:
|
|
|
|
|
child = k
|
|
|
|
|
parent.children.append(child)
|
|
|
|
|
if len(trees) > 0:
|
|
|
|
|
root = trees[0]
|
|
|
|
|
return root
|