【发布时间】:2018-01-26 06:49:14
【问题描述】:
在叶子和节点中具有值的二叉树定义为:
data Tree a = Leaf a
| Node a (Tree a) (Tree a)
deriving (Eq, Show)
例如,
10
/ \
/ \
8 2
/ \ / \
3 5 2 0
exTree :: Tree Int
exTree = N 10 (N 8 (H 3) (H 5))
(N 2 (H 2) (H 0))
好吧,我需要一个名为 RelationshipBinaryTree 的函数,它生成一个元组列表,其第一个组件是 x,第二个是父亲。在这棵树上,
relationshipBinaryTree :: Tree a -> [(a,a)]
relationshipBinaryTree exTree = [(10,8),(8,3),(8,5),(10,2),(2,2),(2,0)]
另外,我需要在 Data.Tree hackage (https://hackage.haskell.org/package/containers-0.5.11.0/docs/Data-Tree.html) 中定义它
我希望你能帮助我,因为我不太了解树和图表。
我试过了
relationshipBinaryTree :: Tree a -> [(a,a)]
relationshipBinaryTree (L _) = []
relationshipBinaryTree (N _ (Tree i) (Tree d)) = relationshipBinaryTree (N _) ++ relationshipBinaryTree (Tree i) ++ relationshipBinaryTree (Tree d)
【问题讨论】:
-
到目前为止你尝试过什么?你哪里出了问题?
-
relationshipBinaryTree :: 树 a -> [(a,a)] ;关系二叉树 (L_) = [] ; relationshipBinaryTree (N _ (Tree i) (Tree d)) = relationshipBinaryTree (N _) ++ relationshipBinaryTree (Tree i) ++ relationshipBinaryTree (Tree d)
标签: haskell tree functional-programming