diff --git a/Ch05/Code01_tree/Main.hs b/Ch05/Code01_tree/Main.hs index fff3110..fe4c152 100644 --- a/Ch05/Code01_tree/Main.hs +++ b/Ch05/Code01_tree/Main.hs @@ -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 } diff --git a/Ch05/Code02_rose/Main.hs b/Ch05/Code02_rose/Main.hs index c79a671..8b0dee3 100644 --- a/Ch05/Code02_rose/Main.hs +++ b/Ch05/Code02_rose/Main.hs @@ -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 = [] } diff --git a/Ch05/Code03_depth/Main.hs b/Ch05/Code03_depth/Main.hs index 6ff923d..312f37c 100644 --- a/Ch05/Code03_depth/Main.hs +++ b/Ch05/Code03_depth/Main.hs @@ -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 diff --git a/Ch05/Code04_breadth/Main.hs b/Ch05/Code04_breadth/Main.hs index e6b689d..e130b46 100644 --- a/Ch05/Code04_breadth/Main.hs +++ b/Ch05/Code04_breadth/Main.hs @@ -1,6 +1,7 @@ import Data.Tree import Data.List (tails) +-- Traversing a tree breadth-first main = do print $ breadthFirst someTree print $ add someTree diff --git a/Ch05/Code05_fold/Main.hs b/Ch05/Code05_fold/Main.hs index 7e2dba4..95f2d78 100644 --- a/Ch05/Code05_fold/Main.hs +++ b/Ch05/Code05_fold/Main.hs @@ -7,6 +7,7 @@ data Tree a = Node { value :: a | Null deriving Show +-- Folding through a tree main :: IO () main = print $ add someTree diff --git a/Ch05/Code06_height/Main.hs b/Ch05/Code06_height/Main.hs index 1d4893f..fcfb008 100644 --- a/Ch05/Code06_height/Main.hs +++ b/Ch05/Code06_height/Main.hs @@ -1,6 +1,7 @@ import Data.List (maximum) import Data.Tree +-- Computing the height of a tree main = do print $ height someTree diff --git a/Ch05/Code07_bst/Main.hs b/Ch05/Code07_bst/Main.hs index d2a7244..371c43b 100644 --- a/Ch05/Code07_bst/Main.hs +++ b/Ch05/Code07_bst/Main.hs @@ -1,5 +1,6 @@ import BSTree +-- Using a binary search tree main = do let tree = single 5 let nodes = [6,4,8,2,9] diff --git a/Ch05/Code08_valid/Main.hs b/Ch05/Code08_valid/Main.hs index 96c5606..ac51983 100644 --- a/Ch05/Code08_valid/Main.hs +++ b/Ch05/Code08_valid/Main.hs @@ -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 diff --git a/Ch05/Code09_avl/Main.hs b/Ch05/Code09_avl/Main.hs index 3b0ffac..e4c4c7d 100644 --- a/Ch05/Code09_avl/Main.hs +++ b/Ch05/Code09_avl/Main.hs @@ -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 \ No newline at end of file + print max diff --git a/Ch05/Code10_heap/Main.hs b/Ch05/Code10_heap/Main.hs index 5e66671..9998d6e 100644 --- a/Ch05/Code10_heap/Main.hs +++ b/Ch05/Code10_heap/Main.hs @@ -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 \ No newline at end of file + print $ weights $ iterate deleteMin heap !! 4 diff --git a/Ch05/Code11_huff/Main.hs b/Ch05/Code11_huff/Main.hs index 8991afe..39ae136 100644 --- a/Ch05/Code11_huff/Main.hs +++ b/Ch05/Code11_huff/Main.hs @@ -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) diff --git a/Ch05/Code12_huff/Main.hs b/Ch05/Code12_huff/Main.hs index 6f27161..625b757 100644 --- a/Ch05/Code12_huff/Main.hs +++ b/Ch05/Code12_huff/Main.hs @@ -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)