-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.hs
119 lines (98 loc) · 3.01 KB
/
Set.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- implementation based on BST
module Set (
Set (..), -- instance Eq, Show
null,
size,
member,
notMember,
isSubsetOf,
insert,
delete,
deleteMax,
findMax,
union,
fromList,
empty,
toList,
filter,
map
) where
import Prelude hiding (null, filter, map)
data Set a = Empty | Tree a (Set a) (Set a)
null :: Set a -> Bool
null Empty = True
null _ = False
size :: Set a -> Int
size Empty = 0
size (Tree root left right) = 1 + (size left) + (size right)
member :: (Ord a) => a -> Set a -> Bool
member _ Empty = False
member value (Tree root left right) =
case compare value root of
EQ -> True
LT -> member value left
GT -> member value right
notMember :: (Ord a) => a -> Set a -> Bool
notMember value tree = not $ member value tree
isSubsetOf :: (Ord a) => Set a -> Set a -> Bool
isSubsetOf Empty _ = True
isSubsetOf _ Empty = False
isSubsetOf (Tree root left right) tree =
(member root tree) && (isSubsetOf left tree) && (isSubsetOf right tree)
insert :: (Ord a) => a -> Set a -> Set a
insert value Empty = (Tree value (Empty) (Empty))
insert value (Tree root left right) =
case compare value root of
EQ -> (Tree value left right)
LT -> (Tree root (insert value left) right)
GT -> (Tree root left (insert value right))
delete :: (Ord a) => a -> Set a -> Set a
delete _ Empty = Empty
delete value (Tree root left right) =
case compare value root of
EQ -> fix left right
LT -> (Tree root (delete value left) right)
GT -> (Tree root left (delete value right))
where
fix left Empty = left
fix Empty right = right
fix left right = Tree (findMax left) (delete (findMax left) left) right
findMax :: (Ord a) => Set a -> a
findMax Empty = error "No maximal element"
findMax (Tree root _ right)
| null right = root
| otherwise = findMax right
deleteMax :: (Ord a) => Set a -> Set a
deleteMax Empty = Empty
deleteMax tree = delete (findMax tree) tree
union :: (Ord a) => Set a -> Set a -> Set a
union tree Empty = tree
union Empty tree = tree
union tree (Tree root left right) =
union (union (insert root tree) left) right
empty :: Set a
empty = Empty
fromList :: (Ord a) => [a] -> Set a
fromList [] = Empty
fromList (x:xs) = insert x $ fromList xs
toList :: Set a -> [a]
toList Empty = []
toList (Tree root left right) = (toList left) ++ [root] ++ (toList right)
filter :: (Ord a) => (a -> Bool) -> Set a -> Set a
filter f = fromList.(filter' f).toList
where
filter' f [] = []
filter' f (x:xs)
| f x = x:(filter' f xs)
| otherwise = filter' f xs
map :: (Ord b) => (a -> b) -> Set a -> Set b
map f = fromList.(map' f).toList
where
map' f [] = []
map' f (x:xs) = (f x):(map' f xs)
instance Eq a => Eq (Set a) where
tree1 == tree2 = toList tree1 == toList tree2
instance Show a => Show (Set a) where
show = show.toList
instance Ord a => Ord (Set a) where
compare tree1 tree2 = compare (toList tree1) (toList tree2)