new: Solved problems 55 to 60
This commit is contained in:
parent
52b3adeb6f
commit
f88a69d1ac
6 changed files with 119 additions and 0 deletions
13
Problems 54A-60/problem_55.hs
Normal file
13
Problems 54A-60/problem_55.hs
Normal file
|
@ -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
|
11
Problems 54A-60/problem_56.hs
Normal file
11
Problems 54A-60/problem_56.hs
Normal file
|
@ -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
|
21
Problems 54A-60/problem_57.hs
Normal file
21
Problems 54A-60/problem_57.hs
Normal file
|
@ -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
|
25
Problems 54A-60/problem_58.hs
Normal file
25
Problems 54A-60/problem_58.hs
Normal file
|
@ -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
|
13
Problems 54A-60/problem_59.hs
Normal file
13
Problems 54A-60/problem_59.hs
Normal file
|
@ -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)
|
36
Problems 54A-60/problem_60.hs
Normal file
36
Problems 54A-60/problem_60.hs
Normal file
|
@ -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]
|
Loading…
Reference in a new issue