diff --git a/Problems 54A-60/problem_55.hs b/Problems 54A-60/problem_55.hs new file mode 100644 index 0000000..751c713 --- /dev/null +++ b/Problems 54A-60/problem_55.hs @@ -0,0 +1,13 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +cbalTree :: Int -> [Tree Char] +cbalTree 0 = [Empty] +cbalTree n = + if even (n - 1) + then [Branch 'x' l r | l <- tree_even, r <- tree_even] + else [Branch 'x' l r | l <- tree_odd1, r <- tree_odd2] ++ [Branch 'x' l r | l <- tree_odd2, r <- tree_odd1] + where + tree_even = cbalTree $ (n - 1) `div` 2 + tree_odd1 = cbalTree $ (n - 1) `div` 2 + tree_odd2 = cbalTree $ (n - 1) `div` 2 + 1 diff --git a/Problems 54A-60/problem_56.hs b/Problems 54A-60/problem_56.hs new file mode 100644 index 0000000..986eeb7 --- /dev/null +++ b/Problems 54A-60/problem_56.hs @@ -0,0 +1,11 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +isMirror :: Tree a -> Tree a -> Bool +isMirror Empty Empty = True +isMirror (Branch _ l1 r1) (Branch _ l2 r2) = isMirror l1 r2 && isMirror r1 l2 +isMirror _ _ = False + +symmetric :: (Eq a) => Tree a -> Bool +symmetric Empty = True +symmetric (Branch _ l r) = isMirror l r diff --git a/Problems 54A-60/problem_57.hs b/Problems 54A-60/problem_57.hs new file mode 100644 index 0000000..5409c36 --- /dev/null +++ b/Problems 54A-60/problem_57.hs @@ -0,0 +1,21 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +isMirror :: Tree a -> Tree a -> Bool +isMirror Empty Empty = True +isMirror (Branch _ l1 r1) (Branch _ l2 r2) = isMirror l1 r2 && isMirror r1 l2 +isMirror _ _ = False + +symmetric :: (Eq a) => Tree a -> Bool +symmetric Empty = True +symmetric (Branch _ l r) = isMirror l r + +add :: (Ord a) => a -> Tree a -> Tree a +add x Empty = Branch x Empty Empty +add x t@(Branch v l r) = case compare x v of + LT -> Branch v (add x l) r + GT -> Branch v l (add x r) + EQ -> t + +construct :: (Ord a) => [a] -> Tree a +construct = foldl (flip add) Empty diff --git a/Problems 54A-60/problem_58.hs b/Problems 54A-60/problem_58.hs new file mode 100644 index 0000000..b22f2e6 --- /dev/null +++ b/Problems 54A-60/problem_58.hs @@ -0,0 +1,25 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +isMirror :: Tree a -> Tree a -> Bool +isMirror Empty Empty = True +isMirror (Branch _ l1 r1) (Branch _ l2 r2) = isMirror l1 r2 && isMirror r1 l2 +isMirror _ _ = False + +symmetric :: (Eq a) => Tree a -> Bool +symmetric Empty = True +symmetric (Branch _ l r) = isMirror l r + +cbalTree :: Int -> [Tree Char] +cbalTree 0 = [Empty] +cbalTree n = + if even (n - 1) + then [Branch 'x' l r | l <- tree_even, r <- tree_even] + else [Branch 'x' l r | l <- tree_odd1, r <- tree_odd2] ++ [Branch 'x' l r | l <- tree_odd2, r <- tree_odd1] + where + tree_even = cbalTree $ (n - 1) `div` 2 + tree_odd1 = cbalTree $ (n - 1) `div` 2 + tree_odd2 = cbalTree $ (n - 1) `div` 2 + 1 + +symCbalTrees :: Int -> [Tree Char] +symCbalTrees = filter symmetric . cbalTree diff --git a/Problems 54A-60/problem_59.hs b/Problems 54A-60/problem_59.hs new file mode 100644 index 0000000..f8fc332 --- /dev/null +++ b/Problems 54A-60/problem_59.hs @@ -0,0 +1,13 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +hbalTree :: a -> Int -> [Tree a] +hbalTree _ 0 = [Empty] +hbalTree x 1 = [Branch x Empty Empty] +hbalTree x n = + [Branch x l r | l <- minustwocase, r <- minusonecase] + ++ [Branch x l r | l <- minusonecase, r <- minusonecase] + ++ [Branch x l r | l <- minusonecase, r <- minustwocase] + where + minusonecase = hbalTree x (n - 1) + minustwocase = hbalTree x (n - 2) diff --git a/Problems 54A-60/problem_60.hs b/Problems 54A-60/problem_60.hs new file mode 100644 index 0000000..926438a --- /dev/null +++ b/Problems 54A-60/problem_60.hs @@ -0,0 +1,36 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +maxNodes :: Int -> Int +maxNodes = subtract 1 . (2 ^) + +minNodesSeq :: [Int] +minNodesSeq = 0 : 1 : zipWith ((+) . (1 +)) minNodesSeq (tail minNodesSeq) + +minNodes :: Int -> Int +minNodes = (!!) minNodesSeq + +maxHeight :: Int -> Int +maxHeight n = subtract 1 $ length $ takeWhile (<= n) minNodesSeq + +minHeight :: Int -> Int +minHeight = ceiling . logBase 2 . fromIntegral . (+ 1) + +countNodes :: Tree a -> Int +countNodes Empty = 0 +countNodes (Branch _ l r) = countNodes l + countNodes r + 1 + +hbalTree :: a -> Int -> [Tree a] +hbalTree _ 0 = [Empty] +hbalTree x 1 = [Branch x Empty Empty] +hbalTree x n = + [Branch x l r | l <- minustwocase, r <- minusonecase] + ++ [Branch x l r | l <- minusonecase, r <- minusonecase] + ++ [Branch x l r | l <- minusonecase, r <- minustwocase] + where + minusonecase = hbalTree x (n - 1) + minustwocase = hbalTree x (n - 2) + +hbalTreeNodes :: a -> Int -> [Tree a] +hbalTreeNodes _ 0 = [Empty] +hbalTreeNodes x n = concatMap (filter ((n ==) . countNodes) . hbalTree x) [minHeight n .. maxHeight n]