25 lines
742 B
Haskell
25 lines
742 B
Haskell
data Tree a = Empty
|
|
| Node (Tree a) a (Tree a)
|
|
deriving Show
|
|
|
|
|
|
-- | Assuming that 0 won't occur
|
|
-- Find maximum value in a tree
|
|
maxOfTree :: Tree Int -> Int
|
|
maxOfTree Empty = 0
|
|
maxOfTree (Node l x r) = max x (max (maxOfTree l) (maxOfTree r))
|
|
|
|
-- | Replace all elements in a tree with a value
|
|
replaceElems :: Tree Int -> Int -> Tree Int
|
|
replaceElems Empty x = Empty
|
|
replaceElems (Node l _ r) x = Node (replaceElems l x) x (replaceElems r x)
|
|
|
|
-- | Replace all elements in a tree with its maximum value
|
|
maxT :: Tree Int -> Tree Int
|
|
maxT t = replaceElems t (maxOfTree t)
|
|
|
|
|
|
sampleTree = Node (Node Empty 2 Empty) 3 (Node Empty 4 Empty)
|
|
rv = maxT sampleTree
|
|
-- Node (Node Empty 4 Empty) 4 (Node Empty 4 Empty)
|