
Untitled
By: a guest on
Aug 1st, 2012 | syntax:
None | size: 1.67 KB | hits: 7 | expires: Never
Monad BinTree
> import Data.Maybe
> import Control.Monad
> data BinTree a = Empty | Branch {left :: BinTree a , val :: a, right:: BinTree a} deriving Show
> isBST :: BinTree a -> Bool
> isBST Empty = True
> isBST (Branch Empty b Empty) = True
> isBST (Branch a b Empty) = if (fromJust (largest a) > b) then False else isBST a
> isBST (Branch Empty b c) = if (b > fromJust (smallest c)) then False else isBST c
> isBST (Branch a b c) = if isBST (Branch a b Empty) && isBST (Branch Empty b c) then True else False
> largest :: Ord a => BinTree a -> Maybe a
> largest Empty = Nothing
> largest (Branch Empty b Empty) = Just b
> largest (Branch a b Empty) = if largest a < b then Just b else Just (largest a)
> largest (Branch Empty b c) = if largest c < b then Just b else Just (largest c)
> largest (Branch a b c) = if largest c > largest a then Just largest (Branch Empty b (Branch Empty (largest c) Empty)) else Just largest (Branch (Branch Empty (largest a) Empty) b Empty)
> smallest :: Ord a => BinTree a -> Maybe a
> smallest Empty = Nothing
> smallest (Branch Empty b Empty) = Just b
> smallest (Branch a b Empty) = if smallest a > b then Just b else Just (smallest a)
> smallest (Branch Empty b c) = if smallest c > b then Just b else Just (smallest c)
> smallest (Branch a b c) = if smallest c < smallest a then Just smallest (Branch Empty b (Branch Empty (smallest c) Empty)) else Just smallest (Branch (Branch Empty (smallest a) Empty) b Empty)
addBST :: BinTree a -> BinTree a -> a -> BinTree a
removeLargestBST :: BinTree a -> (a, BinTree a)
removeLargestBST Empty = ((), Empty)
removeLargestBST
removeSmallestBST :: BinTree a -> (a, BinTree a)