mirror of
https://github.com/BinRoot/Haskell-Data-Analysis-Cookbook.git
synced 2024-10-26 08:19:43 +03:00
Added comments to Ch05
This commit is contained in:
parent
84ed7233dc
commit
2680bc1f26
@ -4,6 +4,7 @@ data Tree a = Node { value :: a
|
||||
| Leaf
|
||||
deriving Show
|
||||
|
||||
-- Creating a binary tree
|
||||
main = do
|
||||
let n1 = Node { value = 1, left = Leaf, right = Leaf }
|
||||
let n2 = Node { value = 2, left = Leaf, right = Leaf }
|
||||
|
@ -1,7 +1,8 @@
|
||||
data Tree a = Node { value :: a
|
||||
, children :: [Tree a] }
|
||||
deriving Show
|
||||
|
||||
|
||||
-- Creating a rose tree
|
||||
main = do
|
||||
let n1 = Node { value = 1, children = [] }
|
||||
let n2 = Node { value = 2, children = [] }
|
||||
|
@ -1,6 +1,7 @@
|
||||
import Data.Tree (rootLabel, subForest, Tree(..))
|
||||
import Data.List (tails)
|
||||
|
||||
-- Traversing a tree depth-first
|
||||
main = do
|
||||
print $ depthFirst someTree
|
||||
print $ add someTree
|
||||
|
@ -1,6 +1,7 @@
|
||||
import Data.Tree
|
||||
import Data.List (tails)
|
||||
|
||||
-- Traversing a tree breadth-first
|
||||
main = do
|
||||
print $ breadthFirst someTree
|
||||
print $ add someTree
|
||||
|
@ -7,6 +7,7 @@ data Tree a = Node { value :: a
|
||||
| Null
|
||||
deriving Show
|
||||
|
||||
-- Folding through a tree
|
||||
main :: IO ()
|
||||
main = print $ add someTree
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import Data.List (maximum)
|
||||
import Data.Tree
|
||||
|
||||
-- Computing the height of a tree
|
||||
main = do
|
||||
print $ height someTree
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import BSTree
|
||||
|
||||
-- Using a binary search tree
|
||||
main = do
|
||||
let tree = single 5
|
||||
let nodes = [6,4,8,2,9]
|
||||
|
@ -15,6 +15,7 @@ someTree = root
|
||||
n3 = Node 3 Null Null
|
||||
n4 = Node 4 Null Null
|
||||
|
||||
-- Verifying a binary search tree
|
||||
valid :: Ord t => Tree t -> Bool
|
||||
valid (Node v l r) = leftValid && rightValid
|
||||
where leftValid = if notNull l then valid l && value l <= v else True
|
||||
|
@ -1,9 +1,10 @@
|
||||
import Data.Tree.AVL
|
||||
import Data.COrdering
|
||||
|
||||
-- Using an AVL balanced binary tree
|
||||
main = do
|
||||
let avl = asTree fstCC [4,2,1,5,3,6]
|
||||
let min = tryReadL avl
|
||||
let max = tryReadR avl
|
||||
print min
|
||||
print max
|
||||
print max
|
||||
|
@ -1,9 +1,10 @@
|
||||
import MinHeap
|
||||
|
||||
-- Using a min-heap
|
||||
main = do
|
||||
let heap = foldr (\x -> insert x x) empty [11, 5, 3, 4, 8]
|
||||
print $ weights heap
|
||||
print $ weights $ iterate deleteMin heap !! 1
|
||||
print $ weights $ iterate deleteMin heap !! 2
|
||||
print $ weights $ iterate deleteMin heap !! 3
|
||||
print $ weights $ iterate deleteMin heap !! 4
|
||||
print $ weights $ iterate deleteMin heap !! 4
|
||||
|
@ -7,6 +7,7 @@ import Data.Map (fromList, (!))
|
||||
|
||||
freq xs = map (\x -> (head x, length x)) . group . sort $ xs
|
||||
|
||||
-- Encoding a string using Huffman codes
|
||||
main = do
|
||||
rsp <- simpleHTTP (getRequest "http://norvig.com/big.txt")
|
||||
html <- fmap (takeWhile isAscii) (getResponseBody rsp)
|
||||
|
@ -7,6 +7,7 @@ import Data.Map (fromList, (!))
|
||||
|
||||
freq xs = map (\x -> (head x, length x)) . group . sort $ xs
|
||||
|
||||
-- Decoding a Huffman code
|
||||
main = do
|
||||
rsp <- simpleHTTP (getRequest "http://norvig.com/big.txt")
|
||||
html <- fmap (takeWhile isAscii) (getResponseBody rsp)
|
||||
|
Loading…
Reference in New Issue
Block a user