diff options
[FP/P12] Reworked levels tro start from 0
1 files changed, 8 insertions, 6 deletions
| -rw-r--r-- | Year 2/Functional Programming/12-Practice-05.01.2023.hs | 14 |
diff --git a/Year 2/Functional Programming/12-Practice-05.01.2023.hs b/Year 2/Functional Programming/12-Practice-05.01.2023.hs index 637f6ee..047f6ca 100644 --- a/Year 2/Functional Programming/12-Practice-05.01.2023.hs +++ b/Year 2/Functional Programming/12-Practice-05.01.2023.hs @@ -7,9 +7,12 @@ size :: BTree a -> Int size Nil = 0 size (Node n bt1 bt2) = 1 + size bt1 + size bt2 +cntheight :: BTree a -> Int +cntheight Nil = 0 +cntheight (Node n bt1 bt2) = 1 + max (height bt1) (height bt2) + height :: BTree a -> Int -height Nil = 0 -height (Node n bt1 bt2) = 1 + max (height bt1) (height bt2) +height bt = cntheight bt - 1 sumTree :: BTree Int -> Int sumTree Nil = 0 @@ -41,9 +44,8 @@ t2 = Node 5 (Node 3 Nil Nil) -- / \ getLevel :: Int -> BTree a -> [a] getLevel _ Nil = [] -getLevel 0 _ = [] getLevel l (Node n bt1 bt2) - | l == 1 = [n] + | l == 0 = [n] | otherwise = getLevel (l - 1) bt1 ++ getLevel (l - 1) bt2 -- Ex3 @@ -65,8 +67,8 @@ main :: IO() main = do print("t1: " ++ show (size t1) ++ " " ++ show (height t1) ++ " " ++ show(sumTree t1) ++ " " ++ show(sumLeaves t1) ++ " " ++ show(inorder t1)) print("t2: " ++ show (size t2) ++ " " ++ show (height t2) ++ " " ++ show(sumTree t2) ++ " " ++ show(sumLeaves t2) ++ " " ++ show(inorder t2)) - print(show(getLevel 1 t1) ++ show(getLevel 2 t1) ++ show(getLevel 3 t1)) - print(show(getLevel 1 t2) ++ show(getLevel 2 t2) ++ show(getLevel 3 t2)) + print(show(getLevel 0 t1) ++ show(getLevel 1 t1) ++ show(getLevel 2 t1)) + print(show(getLevel 0 t2) ++ show(getLevel 1 t2) ++ show(getLevel 2 t2)) print(average t1) print(average t2) print(inorder(mirrorTree t1)) |
