Verifty if a tree is BST or not using Haskell -
hello new haskell , trying write function check if tree binary search tree or not .
below code :-
data tree = node int tree tree | leaf int deriving (eq,show) val :: tree -> int val (leaf i) = isbst :: tree -> bool isbst (leaf i) = true isbst (node x l r) = if (x > (val l )) && (x < (val r)) true else false
but following error when execute .
<interactive>:10:1: not in scope: ‘isbst’
can please me fix or me write method determine if tree bst or not using haskell
.
can please make code exhaustive
?
your immediate problem named function isbst
(with lower case s
, t
), tried use isbst
(with capital s
, t
).
beyond val
function not exhaustive , cause exception when called node
argument. you'll need add case accepts node
, returns value.
additionally isbst
function ever @ root of tree, return true
if value of root between values of subtrees without caring whether subtrees sorted. work should invoke recursively on subtrees.
your function return false
if value of node same of 1 of subtrees. doesn't matter if tree constructed in such way never contain duplicated, otherwise should fixed.
Comments
Post a Comment