From 9a20c34c8e9fd00128d4cdc394017b0b6f04958e Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 24 Feb 2025 21:57:08 -0600 Subject: [PATCH] new: Solved problems 61 to 63 --- Problems 61-69/problem_61.hs | 8 ++++++++ Problems 61-69/problem_61a.hs | 8 ++++++++ Problems 61-69/problem_62.hs | 8 ++++++++ Problems 61-69/problem_62b.hs | 8 ++++++++ Problems 61-69/problem_63.hs | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 Problems 61-69/problem_61.hs create mode 100644 Problems 61-69/problem_61a.hs create mode 100644 Problems 61-69/problem_62.hs create mode 100644 Problems 61-69/problem_62b.hs create mode 100644 Problems 61-69/problem_63.hs diff --git a/Problems 61-69/problem_61.hs b/Problems 61-69/problem_61.hs new file mode 100644 index 0000000..20ffd8c --- /dev/null +++ b/Problems 61-69/problem_61.hs @@ -0,0 +1,8 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +countLeaves :: Tree a -> Int +countLeaves Empty = 0 +countLeaves (Branch _ Empty Empty) = 1 +countLeaves (Branch _ branch1 branch2) = + countLeaves branch1 + countLeaves branch2 diff --git a/Problems 61-69/problem_61a.hs b/Problems 61-69/problem_61a.hs new file mode 100644 index 0000000..aab47a7 --- /dev/null +++ b/Problems 61-69/problem_61a.hs @@ -0,0 +1,8 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +leaves :: Tree a -> [a] +leaves Empty = [] +leaves (Branch a Empty Empty) = [a] +leaves (Branch _ branch1 branch2) = + leaves branch1 ++ leaves branch2 diff --git a/Problems 61-69/problem_62.hs b/Problems 61-69/problem_62.hs new file mode 100644 index 0000000..9ba1e05 --- /dev/null +++ b/Problems 61-69/problem_62.hs @@ -0,0 +1,8 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +internals :: Tree a -> [a] +internals Empty = [] +internals (Branch _ Empty Empty) = [] +internals (Branch a branch1 branch2) = + (a : internals branch1) ++ internals branch2 diff --git a/Problems 61-69/problem_62b.hs b/Problems 61-69/problem_62b.hs new file mode 100644 index 0000000..086c61f --- /dev/null +++ b/Problems 61-69/problem_62b.hs @@ -0,0 +1,8 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +atLevel :: Tree a -> Int -> [a] +atLevel Empty _ = [] +atLevel (Branch a _ _) 1 = [a] +atLevel (Branch _ branch1 branch2) n = + atLevel branch1 (n - 1) ++ atLevel branch2 (n - 1) diff --git a/Problems 61-69/problem_63.hs b/Problems 61-69/problem_63.hs new file mode 100644 index 0000000..e754c99 --- /dev/null +++ b/Problems 61-69/problem_63.hs @@ -0,0 +1,32 @@ +data Tree a = Empty | Branch a (Tree a) (Tree a) + deriving (Show, Eq) + +completeBinaryTreeNumbered :: Int -> Int -> Tree Int +completeBinaryTreeNumbered m n = + Branch m branch1 branch2 + where + branch1 = + if 2 * m <= n + then completeBinaryTreeNumbered (2 * m) n + else Empty + branch2 = + if 2 * m + 1 <= n + then completeBinaryTreeNumbered (2 * m + 1) n + else Empty + +useSameCharLabel :: Char -> Tree a -> Tree Char +useSameCharLabel _ Empty = Empty +useSameCharLabel c (Branch _ branch1 branch2) = + Branch c branch1' branch2' + where + branch1' = useSameCharLabel c branch1 + branch2' = useSameCharLabel c branch2 + +completeBinaryTree :: Int -> Tree Char +completeBinaryTree n = useSameCharLabel 'x' $ completeBinaryTreeNumbered 1 n + +isCompleteBinaryTree :: Tree a -> Bool +isCompleteBinaryTree Empty = True +isCompleteBinaryTree (Branch _ _ Empty) = True +isCompleteBinaryTree (Branch _ branch1 branch2) = + isCompleteBinaryTree branch1 && isCompleteBinaryTree branch2