haskell - How to check whether a binary tree has the heap property? -


given binary tree, want check whether has heap property e.g. if b child node of key(a)>=key(b):

data tree = leaf|node (tree a)(tree a)

i have started function follows:

isheap :: tree -> bool

isheap leaf = true

isheap (node left right) = if (node a)>= isheap(left) && (node a)>= isheap(right) true else false

this wrong ghci telling couldn't match expected type tree a->tree a->tree actual type bool?

i know going wrong, think on right tracks. ideas?

you have few problems. firstly, use >= need add ord constraint, type of isheap should be

isheap :: ord => tree -> bool 

secondly, knowing if child nodes satisfy heap property, need values of child nodes. can match on child node types e.g.

isheap :: ord => tree -> bool isheap leaf = true  isheap (node leaf leaf) = true isheap (node c1@(node b _ _) leaf) = ... isheap (node leaf c2@(node b _ _)) = ... isheap (node c1@(node b _ _) c2@(node c _ _)) = ... 

in last pattern, b , c values of child nodes, need compare value of parent (a), while c1 , c2 nodes themselves.

to answer question error, node constructor function of type

node :: -> tree -> tree -> tree 

so expression (node a) function of type tree -> tree -> tree a. when have

if (node a) >= isheap(left)

since isheap left has type bool, compiler expects left-hand-side of >= have same type. reason you're having problems writing clause don't have value of child nodes compare of parent.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -