module Lab09a where
data Color = Red | Green Float | Blue
data IntTree = ILeaf Int | INode IntTree IntTree
instance Show IntTree where
show (ILeaf x) = ""
show (INode left right) = "" ++ show left ++ show right ++ ""
data Tree a = Leaf a | Node (Tree a) (Tree a)
instance (Show a) => Show (Tree a) where
show (Leaf x) = ""
show (Node left right) = "" ++ show left ++ show right ++ ""
tree :: Tree Char
tree = Node (Node (Leaf 'a') (Node (Leaf 'b') (Leaf 'c'))) (Leaf 'd')
addParanthesis :: Show a => a -> [Char]
addParanthesis x = "(" ++ show x ++ ")"
treeDepth :: Tree a -> Int
treeDepth (Leaf _) = 1
treeDepth (Node l r) = 1 + max (treeDepth l) (treeDepth r)
labelTree :: Tree a -> Tree (a, Int)
labelTree t = fst (go 0 t)
where
go :: Int -> Tree a -> (Tree (a, Int), Int)
go n (Leaf x) = (Leaf (x, n), n + 1)
go n (Node l r) = let (newLeft, n') = go n l
(newRight, n'') = go n' r
a = 5
in (Node newLeft newRight, n'')