-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.hs
50 lines (37 loc) · 1.3 KB
/
bst.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- bst.hs
-- Binary Search Trees implementation
-- Binary trees
data BT a = Empty
| Fork a (BT a) (BT a) deriving (Show, Read, Eq, Ord)
instance Functor BT where
fmap f Empty = Empty
fmap f (Fork x l r) = Fork (f x) (fmap f l) (fmap f r)
instance Monad BT where
Empty >>= k = Empty
Fork n l r >>= k = Fork n (k l) (k r)
return x = Fork x Empty Empty
leaf :: a -> BT a
leaf x = Fork x Empty Empty
mirror :: BT a -> BT a
mirror Empty = error "Empty tree"
mirror (Fork x l r) = Fork x (mirror r) (mirror l)
height :: BT a -> Int
height Empty = -1
height (Fork _ l r) = 1 + max (height l) (height r)
flatten :: BT a -> [a]
flatten Empty = []
flatten (Fork x l r) = (flatten l) ++ [x] ++ (flatten r)
isBst :: Ord a => BT a -> Bool
isBst bt = isSorted (flatten bt)
isSorted :: Ord a => [a] -> Bool
isSorted [] = True
isSorted [_] = True
isSorted (x : y : xs) = x < y && isSorted (y : xs)
occurs :: Ord a => a -> BT a -> Bool
occurs _ Empty = False
occurs x (Fork y l r) = x == y || (x < y && occurs x l) || (x > y && occurs x r)
insert :: Ord a => a -> BT a -> BT a
insert x Empty = leaf x
insert x (Fork y l r) | x == y = Fork y l r
| x < y = Fork y (insert x l) r
| x > y = Fork y l (insert x r)