-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch2.hs
103 lines (80 loc) · 2.7 KB
/
ch2.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
import Prelude hiding (lookup)
-- ==========
-- Lists
-- ==========
--
-- Exercise 2.1
-- write a function of [a] -> [[a]] that returns all the suffixes of that list in O(n) time and space.
suffixes [] = [[]]
suffixes l@(_:xs) = l : (suffixes xs)
-- reasoning:
-- note that every l of each recursive calls will be reused,
-- none of the original list's items will be copied.
-- ==========
-- Binary Search Trees
-- ==========
data Tree a = Empty | Node (Tree a) a (Tree a)
deriving (Show, Eq)
testTree = insert 1 $ insert 5 $ insert 7 $ insert 2 $ insert 4 $ insert 6 $ insert 3 Empty
member :: (Ord a) => a -> Tree a -> Bool
member _ Empty = False
member x t@(Node l v r) =
if x < v then
member x l
else if x > v then
member x r
else True
insert :: (Ord a) => a -> Tree a -> Tree a
insert x Empty = Node Empty x Empty
insert x t@(Node l v r) =
if x < v then
Node (insert x l) v r
else if x > v then
Node l v (insert x r)
else t
-- Exercise 2.2
member2 :: (Ord a) => a -> Tree a -> Bool
member2 _ Empty = False
member2 x t@(Node l v r) = member' t v
where member' Empty b = (x == b)
member' (Node ll vv rr) b =
if x < vv then
member' ll b
else
member' rr vv
-- Exercise 2.3 and 2.4
-- i can't think of a better solution...
-- Exercise 2.5
complete :: a -> Int -> Tree a
complete x 0 = Empty
complete x d = Node st x st
where st = complete x (d-1)
-- this should run in O(d) time
balanced :: a -> Int -> Tree a
balanced x 0 = Empty
balanced x sz
| even (sz-1) = let subtree = balanced x ((sz-1) `div` 2) in Node subtree x subtree
| otherwise = Node stl x str
where (stl, str) = create2 ((sz-1) `div` 2)
create2 m = (balanced x m, balanced x (m+1))
-- this should run in O(log n) time
-- Exercise 2.6
data MapNode k v = MapNode { key :: k, value :: v }
instance (Ord k) => Ord (MapNode k v) where
compare a b = (compare `on` key) a b
instance (Eq k) => Eq (MapNode k v) where
(==) a b = ((==) `on` key) a b
on f g = \a b -> f (g a) (g b)
type FiniteMap k v = Tree (MapNode k v)
empty :: FiniteMap k v
empty = Empty
bind :: (Ord k) => k -> v -> FiniteMap k v -> FiniteMap k v
bind kk vv t = if member newNode t then error "duplicate key"
else insert newNode t
where newNode = MapNode kk vv
lookup :: (Ord k) => k -> FiniteMap k v -> v
lookup _ Empty = error "not found"
lookup kk t@(Node subl (MapNode rk rv) subr) =
if kk == rk then rv
else if kk < rk then lookup kk subl
else lookup kk subr