2
鉴于以下数据结构,创建该给定GenTree
的功能,把它变成BinTree
:功能把一个GenTree到二叉树
- 每个为了
NodeG
在二叉树一个NodeB
节点匹配; NodeB
的左边儿子匹配NodeG
的第一个儿子;- 的
NodeB
右子是如下NodeG
(这意味着,为了NodeG
的父母的童装之间的下一个节点)
视觉示例(GenTree
左,BinTree
右)
1 1
/| | \ /\
2 3 4 5 2 E
/|\ /\
6 7 8 E 3
/\
E 4
/\
6 5
/\
E 7
/\
E 8
data GenTree a = EmptyG | NodeG a [GenTree a]
deriving (Show)
data BinTree a = EmptyB | NodeB (BinTree a) a (BinTree a)
deriving (Show)
。我无法弄清楚如何使主函数的辅助函数(aux)工作。
g2b :: (GenTree a) -> (BinTree a)
g2b EmptyG = EmptyB
g2b (NodeG x ts) = NodeB (aux ts) x EmptyB
aux :: [GenTree a] -> (BinTree a)
aux [] = EmptyB
aux (NodeG x xs) : xss = NodeB (aux xs) x (aux xss) ((NodeG x xs) xss)
的最后一行代码是不工作,和一个我不明白
在我看来,你的问题有点不确定。 “GenTree”比“BinTree”更大,因为他们有更多的东西:NodeG有很多孩子,而NodeB只有两个孩子。当你遇到不适合NodeB的'NodeG'时,你的代码应该做什么?你需要想出一些扁平化策略 –
在问题的深入解释中增加了更多内容,并且提供了函数应该如何工作的可视化示例 –