-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
module Year2013.Exam where | ||
|
||
type BinHeap a = [BinTree a] | ||
|
||
data BinTree a = Node a Int (BinHeap a) | ||
deriving (Eq, Ord, Show) | ||
|
||
-------------------------------------------------------------- | ||
-- PART I | ||
|
||
key :: BinTree a -> a | ||
key | ||
= undefined | ||
|
||
rank :: BinTree a -> Int | ||
rank | ||
= undefined | ||
|
||
children :: BinTree a -> [BinTree a] | ||
children | ||
= undefined | ||
|
||
combineTrees :: Ord a => BinTree a -> BinTree a -> BinTree a | ||
combineTrees | ||
= undefined | ||
|
||
-------------------------------------------------------------- | ||
-- PART II | ||
|
||
extractMin :: Ord a => BinHeap a -> a | ||
extractMin | ||
= undefined | ||
|
||
mergeHeaps :: Ord a => BinHeap a -> BinHeap a -> BinHeap a | ||
mergeHeaps | ||
= undefined | ||
|
||
insert :: Ord a => a -> BinHeap a -> BinHeap a | ||
insert | ||
= undefined | ||
|
||
deleteMin :: Ord a => BinHeap a -> BinHeap a | ||
deleteMin | ||
= undefined | ||
|
||
remove :: Eq a => a -> BinHeap a -> BinHeap a | ||
remove | ||
= undefined | ||
|
||
removeMin :: Ord a => BinHeap a -> (BinTree a, BinHeap a) | ||
removeMin | ||
= undefined | ||
|
||
binSort :: Ord a => [a] -> [a] | ||
binSort | ||
= undefined | ||
|
||
-------------------------------------------------------------- | ||
-- PART III | ||
|
||
toBinary :: BinHeap a -> [Int] | ||
toBinary | ||
= undefined | ||
|
||
binarySum :: [Int] -> [Int] -> [Int] | ||
binarySum | ||
= undefined | ||
|
||
------------------------------------------------------ | ||
-- Some sample trees... | ||
|
||
t1, t2, t3, t4, t5, t6, t7, t8 :: BinTree Int | ||
-- Note: t7 is the result of merging t5 and t6 | ||
|
||
-- t1 to t4 appear in Figure 1... | ||
t1 = Node 4 0 [] | ||
t2 = Node 1 1 [Node 5 0 []] | ||
t3 = Node 2 2 [Node 8 1 [Node 9 0 []], | ||
Node 7 0 []] | ||
t4 = Node 2 3 [Node 3 2 [Node 6 1 [Node 8 0 []], | ||
Node 10 0 []], | ||
Node 8 1 [Node 9 0 []], | ||
Node 7 0 []] | ||
|
||
-- t5 and t6 are on the left of Figure 2; t7 is on the | ||
-- right | ||
t5 = Node 4 2 [Node 6 1 [Node 8 0 []], | ||
Node 10 0 []] | ||
t6 = Node 2 2 [Node 8 1 [Node 9 0 []], Node 7 0 []] | ||
t7 = Node 2 3 [Node 4 2 [Node 6 1 [Node 8 0 []], Node 10 0 []], | ||
Node 8 1 [Node 9 0 []], | ||
Node 7 0 []] | ||
|
||
-- An additional tree... | ||
t8 = Node 12 1 [Node 16 0 []] | ||
|
||
------------------------------------------------------ | ||
-- Some sample heaps... | ||
|
||
h1, h2, h3, h4, h5, h6, h7 :: BinHeap Int | ||
-- Two arbitrary heaps for testing... | ||
h1 = [t2, t7] | ||
h2 = [Node 1 2 [Node 12 1 [Node 16 0 []], | ||
Node 5 0 []], | ||
Node 2 3 [Node 4 2 [Node 6 1 [Node 8 0 []], | ||
Node 10 0 []], | ||
Node 8 1 [Node 9 0 []], | ||
Node 7 0 []]] | ||
|
||
-- h3 is shown in Figure 3... | ||
h3 = [t1, t2, t4] | ||
|
||
-- Two additional heaps, used below. They are shown | ||
-- in Figure 4(a)... | ||
|
||
h4 = [t2, t5] | ||
h5 = [t1, t8] | ||
|
||
-- h6 is the result of merging h4 and h5, shown in Figure 4(b)... | ||
h6 = [Node 4 0 [], | ||
Node 1 3 [Node 4 2 [Node 6 1 [Node 8 0 []], | ||
Node 10 0 []], | ||
Node 12 1 [Node 16 0 []], | ||
Node 5 0 []]] | ||
|
||
-- h7 is shown in Figure 5... | ||
h7 = [Node 4 3 [Node 4 2 [Node 12 1 [Node 16 0 []], | ||
Node 5 0 []], | ||
Node 6 1 [Node 8 0 []], | ||
Node 10 0 []]] | ||
|
||
|