# [算法] 请教高手计算树型前面的对应符号

``````
# 数据格式：
list = [
{'id':1, 'title':'t1', 'parent_id':0},
{'id':2, 'title':'t2', 'parent_id':0},
{'id':3, 'title':'t1_1', 'parent_id':1},
{'id':4, 'title':'t1_2', 'parent_id':1},
{'id':5, 'title':'t1_2_1', 'parent_id':4},
{'id':6, 'title':'t2_1', 'parent_id':2},
{'id':7, 'title':'t2_2', 'parent_id':2},
{'id':8, 'title':'t2_1_1', 'parent_id':6},

# 计算树排序与深度：
list_tree = []
def list_order(pid, list, level=0):
for x in list:
if pid == x['parent_id']:
x['level'] = level
list_tree.append(x)
list_order(x['id'], list, level+1)

list_order(0, list, 0)

for x in list_tree:
print(x['title'], x['level'])

# 需求：
# 请看二楼 (csdn发表主题不可使用特殊符号)
``````

...全文
101 3 点赞 打赏 收藏 举报

3 条回复

``````
# 数据格式：
list=[
{'id':1,'title':'t1','parent_id':0},
{'id':2,'title':'t2','parent_id':0},
{'id':3,'title':'t1_1','parent_id':1},
{'id':4,'title':'t1_2','parent_id':1},
{'id':5,'title':'t1_2_1','parent_id':4},
{'id':6,'title':'t2_1','parent_id':2},
{'id':7,'title':'t2_2','parent_id':2},
{'id':8,'title':'t2_1_1','parent_id':6},
]
# 计算树排序与深度：
list_tree = []
def get_nodeByID(node_id):
for x in list:
if x['id'] == node_id:
return x
return None

def list_order(pid, list, level=0):
for x in list:
if pid == x['parent_id']:
x['level'] = level
if pid == 0:
x['parent'] = None
else:
x['parent'] = get_nodeByID(pid)
x['hasnextbrother'] = False
for y in list_tree:
if y['parent_id'] == x['parent_id']:
y['hasnextbrother'] = True
list_tree.append(x)
list_order(x['id'], list, level+1)

list_order(0, list, 0)

def get_prefix(node):
prefix = ''
if node['parent'] != None:
if node['hasnextbrother'] == True:
prefix = '├'
else:
prefix = '└'
node = node['parent']
while node['parent'] != None:
if node['hasnextbrother'] == True:
prefix = '│' + prefix
else:
prefix = ' ' + prefix
node = node['parent']
return prefix

for x in list_tree:
print(get_prefix(x), x['title'], sep = '')
``````
prefix = ' ' + prefix 这句里的空格' '改成全角的空格‘　’，效果会更好些
• 打赏
• 举报

``````
# 数据格式：
list=[
{'id':1,'title':'t1','parent_id':0},
{'id':2,'title':'t2','parent_id':0},
{'id':3,'title':'t1_1','parent_id':1},
{'id':4,'title':'t1_2','parent_id':1},
{'id':5,'title':'t1_2_1','parent_id':4},
{'id':6,'title':'t2_1','parent_id':2},
{'id':7,'title':'t2_2','parent_id':2},
{'id':8,'title':'t2_1_1','parent_id':6},
]
# 计算树排序与深度：
list_tree = []
def get_nodeByID(node_id):
for x in list:
if x['id'] == node_id:
return x
return None

def list_order(pid, list, level=0):
for x in list:
if pid == x['parent_id']:
x['level'] = level
if pid == 0:
x['parent'] = None
else:
x['parent'] = get_nodeByID(pid)
x['hasnextbrother'] = False
for y in list_tree:
if y['parent_id'] == x['parent_id']:
y['hasnextbrother'] = True
list_tree.append(x)
list_order(x['id'], list, level+1)

list_order(0, list, 0)

def get_prefix(node):
prefix = ''
if node['parent'] != None:
if node['hasnextbrother'] == True:
prefix = '├'
else:
prefix = '└'
node = node['parent']
while node['parent'] != None:
if node['hasnextbrother'] == True:
prefix = '│' + prefix
else:
prefix = ' ' + prefix
node = node['parent']
return prefix

for x in list_tree:
print(get_prefix(x), x['title'], sep = '')
``````
• 打赏
• 举报

``````
# 计算树前面应该输出的符号  │, ├, └
``````

``````
t1
├t1_1
└t1_2
└t1_2_1
t2
├t2_1
│ └t2_1_1
└t2_2

``````
• 打赏
• 举报

3.7w+

JavaScript，VBScript，AngleScript，ActionScript，Shell，Perl，Ruby，Lua，Tcl，Scala，MaxScript 等脚本语言交流。

2017-11-28 03:09

CSDN 脚本语言社区接受专栏投稿（专栏会在顶部创建专属你的栏目），投稿需满足以下要求：

• 脚本语言技术相关；
• 文章持续更新，保持活跃；
• 内容清晰明了，干货为主；
• 文章排版有序，有条有理。