【发布时间】:2014-06-24 21:20:00
【问题描述】:
我想将一个字符串列表转换成三叉树。该树应该具有以下数据类型
datatype ttree = node of string*ttree list*string|leaf of string*string*string
例如,
["<root>","<a3>","<b2>","Father","</b2>","<b3>","Mother","</b3>","</a3>","<a1>","<ffx>","AAA",...] : string list
应该转换成
val test =
node
("<root>",
[node
("<a3>",
[leaf ("<b2>","Father","<b2>"),leaf ("<b3>","Mother","<b3>")],
"<a3>"),
node
("<a1>",[leaf ("<ffx>","AAA","<ffx>"),leaf ("<ff>","BBB","<ff>")],
"<a1>")],"<root>") : ttree
【问题讨论】:
-
fun listToTree nil = node("",[],"") | listToTree (x::xs) = let val n = length(xs)-1 val l = List.last xs val dx = List.take(xs,n) in node(x,listToTree(dx),l) end;这是我的想法,但看起来太天真了,没有做我想做的事@Tayacan -
您将需要一个帮助函数,该函数知道您当前在“内部”哪个标签,但您不需要
length、take或last。
标签: xml-parsing tree sml smlnj