Skip to content

Instantly share code, notes, and snippets.

@weskerfoot
Created November 3, 2013 22:17
Show Gist options
  • Save weskerfoot/7295519 to your computer and use it in GitHub Desktop.
Save weskerfoot/7295519 to your computer and use it in GitHub Desktop.
data RoseTree a = RTree {
rRoot :: a,
rBranches :: [RoseTree a] }
deriving (Show, Eq)
data BinTree a = Branch {
broot :: a,
bLeft :: (BinTree a),
bRight :: (BinTree a) } |
BLeaf
deriving (Show, Eq)
listToTree [] = BLeaf
listToTree (x:xs) =
let x' = listToTree $ rBranches x
in Branch (rRoot x) x' (listToTree xs)
roseToBin (RTree r bs) = Branch r (listToTree bs) BLeaf
exampleRose = RTree "A"
[RTree "B"
[RTree "H"
[RTree "N" [], RTree "O" []],
RTree "I" [],
RTree "J" []],
RTree "C" [],
RTree "D" [],
RTree "E"
[RTree "K"
[RTree "P" []],
RTree "L"
[RTree "Q" []]],
RTree "F" [],
RTree "G"
[RTree "M" []]]
main = print $ roseToBin exampleRose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment