Skip to content

Commit 4d7ed14

Browse files
Init with batchedqueue, start of dequeue
0 parents  commit 4d7ed14

File tree

9 files changed

+189
-0
lines changed

9 files changed

+189
-0
lines changed

‎BatchedDeque.hs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module BatchedDeque(BatchedDeque(..)) where
2+
3+
import Prelude hiding (head, tail)
4+
import Deque
5+
{-
6+
empty :: q a
7+
isEmpty :: q a -> Bool
8+
9+
-- For working with front of queue
10+
cons :: a -> q a -> q a
11+
head :: q a -> a
12+
tail :: q a -> q a
13+
14+
-- For working with end of queue
15+
snoc :: q a -> a -> q a
16+
last :: q a -> a
17+
init :: q a -> q a
18+
-}
19+
20+
data BatchedDeque a = BD [a] [a]
21+
22+
split (BD f r)
23+
| null f = BD ()
24+
where reversed x = reverse x
25+
26+
instance Deque BatchedDeque where
27+
-- Route r nodes to front when f is empty
28+
empty = BD [] []
29+
isEmpty (BD f r) = null f && null r
30+
31+
cons x (BD f r) = BD (x:f) r
32+
33+
head (BD [] r) = error "empty queue"
34+
head (BD (f:fs) r) = f
35+
36+
tail (BD [] r) = error "empty queue"
37+
tail (BD [f] r) = BD (reverse r) []
38+
tail (BD (f:fs) r) = BD fs r
39+
40+
snoc (BD [] r) x = BD [x] r
41+
snoc (BD f r) x = BD f (x:r)
42+
43+
last (BD [] r) = error "empty queue"
44+
last (BD )

‎BatchedQueue.hs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module BatchedQueue where
2+
3+
import Prelude hiding (head, tail)
4+
import Queue
5+
6+
data BatchedQueue a = BQ [a] [a]
7+
deriving (Show, Eq)
8+
9+
instance Queue BatchedQueue where
10+
empty = BQ [] []
11+
isEmpty (BQ f r) = null f
12+
13+
snoc (BQ [] r) x = BQ [x] r
14+
snoc (BQ f r) x = BQ f (x:r)
15+
16+
head (BQ [] _) = error "empty queue"
17+
head (BQ (x:xs) r) = x
18+
19+
tail (BQ [] _) = error "empty queue"
20+
tail (BQ [x] r) = BQ (reverse r) []
21+
tail (BQ (x:xs) r) = BQ xs r

‎BinomialHeap.hs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module BinomialHeap (BinomialHeap(..)) where
2+
3+
import Heap
4+
5+
data Tree a = Node Int a [Tree a]
6+
data BinomialHeap a = BH [Tree a]
7+
8+
rank (Node r x c) = r
9+
root (Node r x c) = x
10+
11+
link t1@(Node r x1 c1) t2@(Node _ x2 c2)
12+
| x1 <= x2 = Node (r + 1) x1 (t2 : c1)
13+
| otherwise = Node (r + 1) x2 (t1 : c2)
14+
15+
insertTree t [] = [t]
16+
insertTree t ts@(t' : ts')
17+
| rank t < rank t' = t : ts
18+
| otherwise = insertTree (link t t') ts'
19+
20+
merge ts1 [] = ts1
21+
merge [] ts2 = ts2
22+
merge ts1@(t1 : ts1') ts2@(t2 : ts2')
23+
| rank t1 < rank t2 = t1 : merge ts1' ts2
24+
| rank t2 < rank t1 = t2 : merge ts1' ts2
25+
| otherwise = insertTree (link t1 t2) (mrg ts1' ts2')
26+
27+
removeMinTree [] = error "empty heap"
28+
removeMinTree [t] = (t, [])
29+
removeMinTree (t:ts)
30+
| root t < root t' = (t, ts)
31+
| otherwise = (t', t : ts')
32+
where (t', ts') = removeMinTree ts
33+
34+
instance Heap Binomial Heap where
35+
empty = BH []
36+
isEmpty (BH ts) = null ts
37+
38+
insert x (BH ts) = BH (insTree (Node 0 x []) ts)
39+
merge (BH ts1) (BH ts2) = BH (merge ts1 ts2)
40+
41+
findMin (BH ts) = root t
42+
where (t, _) = removeMinTree ts
43+
44+
deleteMin (BH ts) = BH (merge (reverse ts1) ts2)
45+
where (Node _ x ts1, ts2) = removeMinTree ts

‎Deque.hs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Deque (Deque(..)) where
2+
3+
class Deque q where
4+
empty :: q a
5+
isEmpty :: q a -> Bool
6+
7+
-- For working with front of queue
8+
cons :: a -> q a -> q a
9+
head :: q a -> a
10+
tail :: q a -> q a
11+
12+
-- For working with end of queue
13+
snoc :: q a -> a -> q a
14+
last :: q a -> a
15+
init :: q a -> q a

‎Heap.hs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Heap (Heap(..)) where
2+
3+
class Heap h where
4+
empty :: Ord a => h a
5+
isEmpty :: Ord a => h a -> Bool
6+
7+
insert :: Ord a => a -> h a -> h a
8+
merge :: Ord a => h a -> h a -> h a
9+
10+
findMin :: Ord a => h a -> a
11+
deleteMin :: Ord a => h a -> h a

‎LeftistHeap.hs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module LeftistHeap (LeftistHeap(..)) where
2+
3+
import Heap
4+
data LeftistHeap a = E
5+
| T Int a (LeftistHeap a) (LeftistHeap a)
6+
7+
-- The rank is defined as the distance from a node to an (empty) leaf
8+
rank E = 0
9+
rank (T r _ _ _) = r
10+
11+
-- makeT is a helper that checks the rank of the left and right subtrees.
12+
-- If the left subtree has a higher rank, it returns a new tree with a rank
13+
-- incremented from the left subtree.
14+
makeT x a b | rank a >= rank b = T (rank b + 1) x a b
15+
| otherwise = T (rank a + 1) x b a
16+
17+
instance Heap LeftistHeap where
18+
empty = E
19+
isEmpty E = True
20+
isEmpty _ = False
21+
22+
insert x h = merge (T 1 x E E) h
23+
24+
merge h E = h
25+
merge E h = h
26+
merge h1@(T _ x a1 b1) h2@(T _ y a2 b2)
27+
| x <= y = makeT x a1 (merge b1 h2)
28+
| otherwise = makeT y a2 (merge h1 b2)
29+
30+
findMin E = error "empty heap"
31+
findMin (T _ x a b) = x
32+
33+
deleteMin E = error "empty heap"
34+
deleteMin (T _ x a b) = merge a b

‎Main.hs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main where
2+
3+
import BatchedQueue
4+
5+
main :: IO ()
6+
main = putStrLn "Hello, Haskell!"

‎Queue.hs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Queue (Queue(..)) where
2+
3+
import Prelude hiding (head, tail)
4+
5+
class Queue q where
6+
empty :: q a
7+
isEmpty :: q a -> Bool
8+
9+
snoc :: q a -> a -> q a
10+
head :: q a -> a
11+
tail :: q a -> q a

‎Setup.hs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

0 commit comments

Comments
 (0)