new: Solved problems 55 to 60

This commit is contained in:
Sayantan Santra 2024-05-27 15:03:34 -05:00
parent 52b3adeb6f
commit f88a69d1ac
Signed by: SinTan1729
GPG Key ID: EB3E68BFBA25C85F
6 changed files with 119 additions and 0 deletions

View 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

View 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

View 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

View 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

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

View 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]