本文介绍了Haskell树列出-预遍遍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Haskell中给出以下树结构:
Given the following tree structure in Haskell:
data Tree = Leaf Int | Node Int Tree Tree deriving Show
如何让Haskell以预定顺序返回数据列表?
How can I get Haskell to return a list of the data in pre-order?
例如给出一棵树:
Node 1 (Leaf 2) (Leaf 3)
返回类似的内容:
preorder = [1,2,3]
推荐答案
好的,很抱歉收到的答复很晚,但我的工作方式如下:
Ok, sorry about the late reply, but I got this working as follows:
preorder(Leaf n) = [n]
preorder(Node n treeL treeR) = [n] ++ preorder treeL ++ preorder treeR'code'
但这仍然不适用于我
preorder (Leaf n) = [n]
preorder (Node n a b) = n:(preorder a) ++ (preorder b)
这篇关于Haskell树列出-预遍遍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!