【发布时间】:2013-05-19 21:03:52
【问题描述】:
给定一棵二叉树,我想检查它是否具有堆属性,例如如果B是A的子节点那么key(A)>=key(B):
data Tree a = Leaf|Node a (Tree a)(Tree a)
我的功能开始如下:
isHeap :: Tree a -> Bool
isHeap Leaf = True
isHeap (Node a left right) = if (Node a)>= isHeap(left) && (Node a)>= isHeap(right) then True else False
这是错误的,因为 GHCI 告诉它无法匹配预期类型 Tree a->Tree a->Tree a 与实际类型 Bool?
我知道我错了,但我认为它在正确的轨道上。有什么想法吗?
【问题讨论】:
-
旁注:如果你发现自己写了
if Condition then True else False或if Condition then False else True,你可以简单地写Condition或not Condition。
标签: haskell