Added comments to Ch05

This commit is contained in:
Nishant Shukla 2014-06-21 11:10:20 -04:00
parent 84ed7233dc
commit 2680bc1f26
12 changed files with 15 additions and 3 deletions

View File

@ -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 }

View File

@ -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 = [] }

View File

@ -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

View File

@ -1,6 +1,7 @@
import Data.Tree
import Data.List (tails)
-- Traversing a tree breadth-first
main = do
print $ breadthFirst someTree
print $ add someTree

View File

@ -7,6 +7,7 @@ data Tree a = Node { value :: a
| Null
deriving Show
-- Folding through a tree
main :: IO ()
main = print $ add someTree

View File

@ -1,6 +1,7 @@
import Data.List (maximum)
import Data.Tree
-- Computing the height of a tree
main = do
print $ height someTree

View File

@ -1,5 +1,6 @@
import BSTree
-- Using a binary search tree
main = do
let tree = single 5
let nodes = [6,4,8,2,9]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)