如何定义subTree方法

时间:2013-12-15 12:52:47

标签: haskell

在这个def中,subTree功能会起作用吗? 我有点困惑。 现在,如果树中的节点包含字符串“s”,则带有字符串“s”的叶子可以被视为子树。

data Tree = Leaf String | Node Tree String Tree

subTree :: Tree -> Tree -> Bool
subTree (Leaf s) t = inTree s t
subTree (Node t1 s1 t2) (Leaf s2) = False
subTree (Node t1 s1 t2) (Node t3 s2 t4) = 
   ((s1 == s2) && (subTree t1 t3) && (subTree t2 t4)) ||
   subTree (Node t1 s1 t2) t3 ||
   subTree (Node t1 s1 t2) t4

inTree :: String -> Tree -> Bool
inTree s1 (Leaf s2) = (s1 == s2)
inTree s1 (Node t1 s2 t2) = (s1 == s2) || inTree s1 t1 || inTree s1 t2

1 个答案:

答案 0 :(得分:1)

*Main> subTree (Leaf 1) (Node (Leaf 0) 1 (Leaf 9))
True
*Main> subTree (Leaf 1) (Node (Leaf 1) 4 (Leaf 9))
True
*Main> subTree (Leaf 1) (Node (Leaf 0) 4 (Leaf 9))
False
*Main> 

我将String更改为Integer

根据子树的def,这是否正确?