Algorithm 从列表的列表中构建树
我有一个列表,看起来像:{{f,h,I},{b,e,f,g},{a,b,c,d} 我需要使用以下规则构建一棵树:Algorithm 从列表的列表中构建树,algorithm,tree,Algorithm,Tree,我有一个列表,看起来像:{{f,h,I},{b,e,f,g},{a,b,c,d} 我需要使用以下规则构建一棵树: 对于每个列表,第一个元素是根 其余的元素是子元素 对于本例,树的外观如下: a b c d e f g h i 你能帮我写这个的算法吗 谢谢! 这是一个简单的递归过程 如果列表包含一个列表,首先递归地处理该列表,然后用它的第一个元素(它的根)替换它 现在,列表只包含字母(表示节点) a。将第一个字母
- 对于每个列表,第一个元素是根
- 其余的元素是子元素
a
b c d
e f g
h i
你能帮我写这个的算法吗
谢谢! 这是一个简单的递归过程
def make_into_tree(l):
for e in l:
if type(e) == list:
e = make_into_tree(e)
root = e[0]
smaller = sorted(all letters smaller than e[0])
for each s in smaller (except for first):
make s a right child of its predecessor
smaller_root = smaller[0]
make smaller_root left child of root
larger = sorted(all letter larger than e[0])
for each l in larger (except for first):
make l a right child of its predecessor
larger_root = smaller[0]
make larger_root right child of root
return root
谢谢,但是你能写一个伪代码吗?因为我不明白你的全部想法?