Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
- {- constructors -}
- treeByList :: (Ord a) => [a] -> Tree a
- treeByList x = foldr treeInsert EmptyTree x
- {--}
- printTreeHelper :: (Show t) => Tree t -> Int -> [Char]
- printTreeHelper EmptyTree margin = ""
- printTreeHelper (Node a EmptyTree EmptyTree) margin = (replicate margin ' ') ++ (show a)
- printTreeHelper (Node a left right) margin = (replicate margin ' ') ++ (show a) ++ "\n" ++ (printTreeHelper left (margin + 3)) ++ "\n" ++ (printTreeHelper right (margin + 3))
- printTree :: (Show t) => Tree t -> [Char]
- printTree a = printTreeHelper a 0
- {- methods -}
- inTree :: (Ord a) => a -> Tree a -> Bool
- inTree _ EmptyTree = False
- inTree x (Node a left right)
- | x == a = True
- | x < a = inTree x left
- | x > a = inTree x right
- treeInsert :: (Ord a) => a -> Tree a -> Tree a
- treeInsert x EmptyTree = (Node x EmptyTree EmptyTree)
- treeInsert x (Node a left right)
- | x == a = Node a left right
- | x > a = Node a left (treeInsert x right)
- | x < a = Node a (treeInsert x left) right
- treeGetMin :: (Ord a) => Tree a -> a
- treeGetMin (Node a EmptyTree right) = a
- treeGetMin (Node a left right) = treeGetMin left
- treeErase :: (Ord a) => a -> Tree a -> Tree a
- treeErase x EmptyTree = EmptyTree
- treeErase x (Node a left right)
- | x > a = (Node a left (treeErase x right))
- | x < a = (Node a (treeErase x left) right)
- treeErase x (Node a EmptyTree right) = right
- treeErase x (Node a left EmptyTree) = left
- treeErase x (Node a left right) = Node (treeGetMin right) left (treeErase (treeGetMin right) right)
- {--}
- elems = [7, 5, 3, 1, 6, 2, 4]
- mytree = treeByList elems
- main = putStrLn $ printTree (foldr treeErase mytree [4, 3, 2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement