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

无闻i 2017-11-28 03:09:51


# 数据格式:
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 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
真糨糊 2017-11-28
引用 2 楼 njg_jh 的回复:

# 数据格式:
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 这句里的空格' '改成全角的空格‘ ’,效果会更好些
  • 打赏
  • 举报
回复
真糨糊 2017-11-28

# 数据格式:
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 = '')
  • 打赏
  • 举报
回复
无闻i 2017-11-28
需求

# 计算树前面应该输出的符号  │, ├, └
得出如下格式:

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 脚本语言社区接受专栏投稿(专栏会在顶部创建专属你的栏目),投稿需满足以下要求:

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

本社区开通招聘专栏,发布招聘信息请联系版主,发布者需要保证招聘信息真实有效,CSDN 平台和版主不对招聘内容负责!

联系方式:私聊版主、发送邮件、QQ联系等均可: