yingpotter

[99p]Binary(Complete w/o #66)

Jul 25th, 2011
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2.  
  3. ----- data structure of tree
  4. data Tree a = Empty | Branch a (Tree a) (Tree a)
  5.               deriving (Show, Eq)
  6.  
  7. -- problem 55
  8. cbTree :: Int -> [Tree Char]
  9. cbTree 0 = [Empty]
  10. cbTree n = [Branch 'x' lt rt | i <- [q..q+r], lt <- cbTree i, rt <- cbTree (n-1-i)]
  11.            where (q, r) = ((n-1) `div` 2, (n-1) `mod` 2)
  12.            
  13. -- problem 56
  14. mirror :: Tree a -> Tree a -> Bool
  15. mirror Empty Empty = True
  16. mirror (Branch _ lt1 rt1) (Branch _ lt2 rt2) = mirror lt1 rt2 && mirror lt2 rt1
  17. mirror _ _ = False
  18. symmetric :: Tree a -> Bool
  19. symmetric Empty = True
  20. symmetric (Branch _ lt rt) = mirror lt rt
  21.  
  22. -- problem 57
  23. add' :: Ord a => a -> Tree a -> Tree a
  24. add' x Empty = Branch x Empty Empty
  25. add' x (Branch y lt rt)
  26.    | x == y  = Branch y lt rt
  27.    | x < y   = Branch y (add' x lt) rt
  28.     | x > y   = Branch y lt (add' x rt)
  29. createbst :: Ord b => [b] -> Tree b
  30. createbst xs = foldr (add') Empty $ reverse xs
  31.  
  32. -- problem 58
  33. symcbTree n = filter symmetric $ cbTree n
  34.  
  35. -- problem 59
  36. hbhTree :: Int -> [Tree Char]
  37. hbhTree 0 = [Empty]
  38. hbhTree 1 = [Branch 'x' Empty Empty]
  39. hbhTree n = [Branch 'x' lt rt |
  40.              hl <- [n-2..n-1], hr <- [n-2..n-1], lt <- hbhTree hl, rt <- hbhTree hr, max hl hr == n-1]
  41. -- problem 60
  42. ----- predefined funcs
  43. nodecount :: Tree Char -> Int
  44. nodecount Empty = 0
  45. nodecount (Branch _ lt rt) = 1 + nodecount lt + nodecount rt
  46. height :: Tree Char -> Int
  47. height Empty = 0
  48. height (Branch _ lt rt) = 1 +  max (height lt) (height rt)
  49. ----- pre-guessed(minNode,maxHeight) funcs
  50. minNode :: Int -> Int
  51. minNode 0 = 0
  52. minNode 1 = 1
  53. minNode n = minNode (n-1) + minNode (n-2) + 1
  54. maxNode :: Int -> Int
  55. maxNode n = 2^n - 1
  56. minHeight :: Int -> Int
  57. minHeight n = ceiling $ logBase 2 $ fromIntegral(n+1)
  58. maxHeight :: Int -> Int
  59. maxHeight n = (head $filter (\x -> minNode x > n) [1..]) - 1
  60. ----- very slow solution
  61. hbnTree :: Int -> [Tree Char]
  62. hbnTree 0 = [Empty]
  63. hbnTree n = [Branch 'x' lt rt |
  64.              i <- [0..n-1], lt <- hbnTree i, rt <- hbnTree (n-1-i), abs(height lt - height rt) < 2]            
  65. ----- a quick solution
  66. hbnTree' :: Int -> [Tree Char]
  67. hbnTree' 0 = [Empty]
  68. hbnTree' n = concat $ map (filter (\x ->nodecount x == n) . hbhTree) [(minHeight n)..(maxHeight n)]
  69.  
  70. -- predefined tree4
  71. tree4 = Branch 1 (Branch 2 Empty (Branch 4 Empty Empty))
  72.                 (Branch 2 Empty Empty)
  73.  
  74. -- problem 61
  75. leafcount :: Tree a -> Int
  76. leafcount Empty = 0
  77. leafcount (Branch _ Empty Empty) =1
  78. leafcount (Branch _ lt rt) = leafcount lt + leafcount rt
  79.  
  80. -- problem 61a
  81. leaflist :: Tree a -> [a]
  82. leaflist Empty = []
  83. leaflist (Branch a Empty Empty) = [a]
  84. leaflist (Branch _ lt rt) = leaflist lt ++ leaflist rt
  85.  
  86. -- problem 62
  87. internallist :: Tree a -> [a]
  88. internallist Empty = []
  89. internallist (Branch _ Empty Empty) = []
  90. internallist (Branch a lt rt) = [a] ++ internallist lt ++ internallist rt
  91.  
  92. -- problem 62a
  93. atLevel :: Tree a -> Int -> [a]
  94. atLevel _ 0 = []
  95. atLevel Empty _ = []
  96. atLevel (Branch a _ _) 1 = [a]
  97. atLevel (Branch _ lt rt) n = atLevel lt (n-1) ++ atLevel rt (n-1)
  98.  
  99. -- problem 63
  100. cbinHeight :: Int -> Int
  101. cbinHeight n = ceiling $ logBase 2 $ fromIntegral(n+1)
  102. cbinTree :: Int -> Tree Char
  103. cbinTree 0 = Empty
  104. cbinTree 1 = Branch 'x' Empty Empty
  105. cbinTree n = Branch 'x' (cbinTree ltn) (cbinTree rtn)
  106.             where (ltn, rtn)
  107.                    | (n-1) < 3*2^(h-2)-1  = (n-2^(h-2), 2^(h-2)-1)
  108.                    | (n-1) >= 3*2^(h-2)-1  = (2^(h-1)-1 , n-2^(h-1))
  109.                    where h = cbinHeight n
  110. cmpTree :: Tree Char -> Tree Char -> Bool
  111. cmpTree Empty Empty = True
  112. cmpTree (Branch _ lt rt) Empty = False
  113. cmpTree Empty (Branch _ lt rt) = False
  114. cmpTree (Branch _ lt1 rt1) (Branch _ lt2 rt2) = (cmpTree lt1 lt2) && (cmpTree rt1 rt2)
  115. iscbinTree :: Tree Char -> Bool
  116. iscbinTree t = cmpTree t (cbinTree $nodecount t)
  117.  
  118. -- predefined tree64
  119. tree64 = Branch 'n'
  120.                (Branch 'k'
  121.                        (Branch 'c'
  122.                                (Branch 'a' Empty Empty)
  123.                                (Branch 'h'
  124.                                        (Branch 'g'
  125.                                                (Branch 'e' Empty Empty)
  126.                                                Empty
  127.                                        )
  128.                                        Empty
  129.                                )
  130.                        )
  131.                        (Branch 'm' Empty Empty)
  132.                )
  133.                (Branch 'u'
  134.                        (Branch 'p'
  135.                                Empty
  136.                                (Branch 's'
  137.                                        (Branch 'q' Empty Empty)
  138.                                        Empty
  139.                                )
  140.                        )
  141.                        Empty
  142.                )
  143. -- problem 64
  144. layouta' :: Int -> Tree Char -> Tree (Char, (Int, Int))
  145. layouta' _ Empty  = Empty
  146. layouta' n (Branch a lt rt)  = Branch (a, (horia, n)) (layouta' (n+1) lt) (layouta' (n+1) rt)
  147.                                where horia = nodecount lt +1
  148. layouta :: Tree Char -> Tree (Char, (Int, Int))
  149. layouta = layouta' 1
  150.                                    
  151. -- predefined tree65
  152. tree65 = Branch 'n'
  153.                (Branch 'k'
  154.                        (Branch 'c'
  155.                                (Branch 'a' Empty Empty)
  156.                                (Branch 'e'
  157.                                        (Branch 'd' Empty Empty)
  158.                                        (Branch 'g' Empty Empty)
  159.                                )
  160.                        )
  161.                        (Branch 'm' Empty Empty)
  162.                )
  163.                (Branch 'u'
  164.                        (Branch 'p'
  165.                                Empty
  166.                                (Branch 'q' Empty Empty)
  167.                        )
  168.                        Empty
  169.                )
  170. -- problem 65
  171. mostleftDepth :: Tree a -> Int
  172. mostleftDepth Empty = 0
  173. mostleftDepth (Branch _ lt rt) = 1+ mostleftDepth lt
  174. layoutb' :: Int -> Int -> Int -> Tree Char -> Tree (Char, (Int, Int))
  175. layoutb' _ _ _ Empty  = Empty
  176. layoutb' m n d (Branch a lt rt) = Branch (a,(m+2^d, n)) (layoutb' m (n+1) (d-1) lt) (layoutb' (m+2^d) (n+1) (d-1) rt)
  177. layoutb'' t = layoutb' 0 1 (height t -1) t
  178. parahorileft :: Int -> Tree (Char, (Int, Int)) -> Tree (Char, (Int, Int))
  179. parahorileft _ Empty = Empty
  180. parahorileft 0 t = t
  181. parahorileft n (Branch (a, (hori, verti)) lt rt) = Branch (a, (hori -n, verti)) (parahorileft n lt) (parahorileft n rt)
  182. layoutb :: Tree Char -> Tree (Char, (Int, Int))
  183. layoutb t = parahorileft (if (height t == mostleftDepth t) then 0 else 2^(height t - mostleftDepth t -1) ) $layoutb'' t
  184.  
  185. -- predefined tree66
  186. tree66 = Branch 'n'
  187.                (Branch 'k'
  188.                        Empty
  189.                        (Branch 'm' Empty Empty)
  190.                )
  191.                (Branch 'u'
  192.                        Empty
  193.                        (Branch 'p' Empty Empty)
  194.                )
  195.  
  196.  
  197. -- problem 66
  198.  
  199. -- problem 67a
  200. treeStr :: Tree Char -> String
  201. treeStr Empty = ""
  202. treeStr (Branch a Empty Empty) = [a]
  203. treeStr (Branch a lt rt) = [a] ++ "(" ++ treeStr lt ++ "," ++ treeStr rt ++ ")"
  204. countparaLeft :: String -> Int
  205. countparaLeft [] = 0
  206. countparaLeft xs = length $ findIndices (=='(') xs
  207. countparaRight :: String -> Int
  208. countparaRight [] = 0
  209. countparaRight xs = length $ findIndices (==')') xs
  210. spSearch :: String -> Int
  211. spSearch xs = head [i | i <- findIndices (==',') xs, countparaLeft (take i xs) == countparaRight (take i xs)]
  212. strTree :: String -> Tree Char
  213. strTree "" = Empty
  214. strTree [a] = Branch a Empty Empty
  215. strTree (a:y:xs)
  216.        | y == '(' && last xs == ')' = Branch a (strTree $ take sp xxs) (strTree $ drop (sp+1) xxs)
  217.                                       where xxs = init xs
  218.                                             sp = spSearch xxs
  219.                                            
  220. -- problem 68
  221. treePre :: Tree Char -> String
  222. treePre Empty = ""
  223. treePre (Branch a lt rt) = [a] ++ treePre lt ++ treePre rt
  224. treeIn :: Tree Char -> String
  225. treeIn Empty = ""
  226. treeIn (Branch a lt rt) = treeIn lt ++ [a] ++ treeIn rt
  227. preTree :: String -> Tree Char
  228. preTree "" = Empty
  229. preTree [a] = Branch a Empty Empty
  230. preTree (x:xs) = Branch x Empty (preTree xs)
  231. preinTree :: String -> String -> Tree Char
  232. preinTree "" "" = Empty
  233. preinTree [a] [b]
  234.          | a == b     = Branch a Empty Empty
  235.          | a /= b     = error "dont kidding me"
  236. preinTree (x:xs) ys
  237.          | sort (x:xs) /= sort ys  = error "dont kidding me"
  238.          | sort (x:xs) == sort ys  = Branch x lt rt
  239.                         where lt = preinTree (take sp xs) (take sp ys)
  240.                               rt = preinTree (drop sp xs) (drop (sp+1) ys)
  241.                               sp = head $ findIndices (==x) ys
  242.  
  243. -- problem 69
  244. treeDots :: Tree Char -> String
  245. treeDots Empty = "."
  246. treeDots (Branch a lt rt) = [a] ++ treeDots lt ++ treeDots rt
  247. dotsnodots :: String -> Int
  248. dotsnodots "" = 0
  249. dotsnodots (x:xs) = (if x=='.' then 1 else (-1)) + dotsnodots(xs)
  250. dotsTree :: String -> Tree Char
  251. dotsTree "" = Empty
  252. dotsTree "." = Empty
  253. dotsTree (x:xs) = Branch x (dotsTree $ take dp xs) (dotsTree $ drop dp xs)
  254.                  where dp = head $ findIndices (==1) $ map dotsnodots (inits xs)
Advertisement
Add Comment
Please, Sign In to add comment