Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,26 @@
|
|||
data Tree a = Nil
|
||||
| Node a (Tree a) (Tree a)
|
||||
deriving Show
|
||||
|
||||
insert :: Ord a => a -> Tree a -> Tree a
|
||||
insert x Nil = Node x Nil Nil
|
||||
insert x (Node y leftBranch rightBranch)
|
||||
| x < y = Node x (insert y rightBranch) leftBranch
|
||||
| otherwise = Node y (insert x rightBranch) leftBranch
|
||||
|
||||
merge :: Ord a => Tree a -> Tree a -> Tree a
|
||||
merge Nil t = t
|
||||
merge t Nil = t
|
||||
merge tx@(Node vx lx rx) ty@(Node vy ly ry)
|
||||
| vx < vy = Node vx (merge lx rx) ty
|
||||
| otherwise = Node vy tx (merge ly ry)
|
||||
|
||||
fromList :: Ord a => [a] -> Tree a
|
||||
fromList = foldr insert Nil
|
||||
|
||||
toList :: Ord a => Tree a -> [a]
|
||||
toList Nil = []
|
||||
toList (Node x l r) = x : toList (merge l r)
|
||||
|
||||
mergeSort :: Ord a => [a] -> [a]
|
||||
mergeSort = toList . fromList
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
ghci> heapSort [9,5,8,2,1,4,6,3,0,7]
|
||||
[0,1,2,3,4,5,6,7,8,9]
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import Data.Graph.Inductive.Internal.Heap(
|
||||
Heap(..),insert,findMin,deleteMin)
|
||||
|
||||
-- heapsort is added in this module as an example application
|
||||
|
||||
build :: Ord a => [(a,b)] -> Heap a b
|
||||
build = foldr insert Empty
|
||||
|
||||
toList :: Ord a => Heap a b -> [(a,b)]
|
||||
toList Empty = []
|
||||
toList h = x:toList r
|
||||
where (x,r) = (findMin h,deleteMin h)
|
||||
|
||||
heapSort :: Ord a => [a] -> [a]
|
||||
heapSort = (map fst) . toList . build . map (\x->(x,x))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
*Main> heapsort [[6,9],[2,13],[6,8,14,9],[10,7],[5]]
|
||||
[[2,13],[5],[6,8,14,9],[6,9],[10,7]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue