Haskell Tree删除节点/计算所有可能的路径

时间:2017-05-23 10:41:38

标签: haskell tree

我有这些结构:

data Tree = Leaf Points | Branch Points [Tree]
deriving(Eq,Show)

data Design = Design Tree Tree Int
deriving(Eq,Show)

数据类型Tree是数据类型Design的一部分,而Design包含两个Trees

 type Points = [(Int,Int,Int,Int)]

 design = Design mtree btree 180

我必须找到所有可能的路径:

numberPaths :: Design -> Int

并从Integer更改第三个Points,从design更改180:

type Points = [(Int,Int,Int,Int)]
design = Design mtree btree 180

changeInt :: Design -> Design

1 个答案:

答案 0 :(得分:0)

计算路径

要查找树中的路径数,我们递归地下降树以计算每个节点的子路径数,然后将它们相加。因此除了使用numberPaths作为接口之外,我们还需要另一个类型签名为Tree -> Int的辅助函数。我们称之为descend

descend :: Tree -> Int 

我们将Leaf计为1,将Branch计为其子树的路径数之和。所以descend变为:

descend :: Tree -> Int 
descend (Leaf _)            = 1 
descend (Branch _ subtress) = foldr (\tree sum -> sum + descend tree) 0 subtrees 

函数numberPaths只需要调用descend来计算其两棵树。

numberPaths :: Design -> Int 
numberPaths (Design a b _) = descend a + descend b 

Chang Int

changeInt的类型签名表示没有引入Int值来更改Design类型。我认为这没有意义,所以我冒昧地将其改为Int -> Design -> Design

changeInt的实现遵循类似的模式。为了更改树,我们递归地沿着树的每个级别向下走。我们还引入了辅助函数changePoints

changeTree :: Int -> Tree -> Tree
changeTree n (Leaf points)            = Leaf $ changePoints n points
changeTree n (Branch points subtrees) = Branch points' subtrees'
    where points'   = changePoints n points 
          subtrees' = map (changeTree n) subtrees

changePoints :: Int -> Points -> Points 
changePoints n points = map (\(x, y, _, z) -> (x, y, n, z)) points 

changeInt只需要调用changeTree即可完成工作。

changeInt :: Int -> Design -> Design 
changeInt n (Design mtree btree _) = Design mtree' btree' n
    where mtree' = changeTree n mtree 
          btree' = changeTree n btree