Skip to content

Instantly share code, notes, and snippets.

@BinRoot
Created February 19, 2013 04:43
Show Gist options
  • Select an option

  • Save BinRoot/4983189 to your computer and use it in GitHub Desktop.

Select an option

Save BinRoot/4983189 to your computer and use it in GitHub Desktop.
Haskell Trees by James C. Sun
-- Mastered by James C. Sun
module BinaryTree
(Tree(..), makeTree, add, printTree)
where
data Tree = Tree Int Tree Tree | EMPTYTREE deriving (Show, Eq)
printTree :: Tree -> IO()
printTree tree = putStrLn (printSubTree tree 0 0)
nodeLinks = ["", "/", "\\"]
printSubTree :: Tree -> Int -> Int -> String
printSubTree EMPTYTREE _ _ = ""
printSubTree (Tree root leftSubTree rightSubTree) n link = (printSubTree leftSubTree (n+3) 1)
++ (replicate n ' ')
++ (nodeLinks !! link)
++ (show root) ++ "\n"
++ (printSubTree rightSubTree (n+3) 2)
makeTree :: Int -> Tree
makeTree n
| n < 0 = EMPTYTREE
| otherwise = Tree n (makeTree (n - 1)) (makeTree (n - 2))
add :: Tree -> Int
add EMPTYTREE = 0
add (Tree root leftSubTree rightSubTree) = root + (add leftSubTree) + (add rightSubTree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment