import re class Node:
'''
容器,用来存储前后节点信息
'''
__slot__=[]
def __init__(self,val,next_,pre,name,no):
self.val = val
self.next = next_
self.pre = pre
self.name = name
self.no = no class Tree:
def __init__(self,li):
self.root = Node(-1,[],None,'',-1)
cur_node = self.root
for no,i,name in li:
name = re.sub(r'[ ]+','',name)
while i<cur_node.val:
cur_node = cur_node.pre
node_ = Node(i,[],cur_node,name,no)
if i>cur_node.val:
cur_node.next.append(node_)
node_.name = cur_node.name+'_'+node_.name if cur_node.name!='' else node_.name
elif i == cur_node.val:
node_.pre = cur_node.pre
cur_node.pre.next.append(node_)
node_.name = cur_node.pre.name+'_'+node_.name if cur_node.pre.name!='' else node_.name
cur_node = node_
def newNameDict(self):
if self.root.next:
res = dict()
queue = [self.root]
while queue:
cur = queue.pop(-1)
if cur.no==-1:
pass
else:
res[cur.no] = cur.name
if cur.next:
for i in cur.next:
queue.append(i)
return res pass