99 questions/Solutions/69

From HaskellWiki
< 99 questions‎ | Solutions
Revision as of 02:34, 14 July 2010 by Wapcaplet (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Dotstring representation of binary trees.

We consider again binary trees with nodes that are identified by single lower-case letters, as in the example of problem P67. Such a tree can be represented by the preorder sequence of its nodes in which dots (.) are inserted where an empty subtree (nil) is encountered during the tree traversal. For example, the tree shown in problem P67 is represented as 'abd..e..c.fg...'. First, try to establish a syntax (BNF or syntax diagrams) and then write a predicate tree_dotstring/2 which does the conversion in both directions. Use difference lists.

data Tree a = Empty | Branch a (Tree a) (Tree a) deriving (Show, Eq)
                                                                                                                                                                                                                                                                                                                                                                                                                                                   
ds2tree []      = (Empty,"")                                                                                                                                   
ds2tree ('.':xs) = (Empty,xs)                                                                                                                                  
ds2tree (x:xs)  = (Branch x left right, rest2)                                                                                                                 
  where (left,rest) = ds2tree xs                                                                                                                               
        (right,rest2) = ds2tree rest                                                                                                                           
                                                                                                                                                               
tree2ds Empty = "."                                                                                                                                            
tree2ds (Branch x l r) = x:(tree2ds l ++ tree2ds r)

The tree2ds function is straightforward: Empty trees become a dot, and non-empty trees become their label, with left and right trees appended.

ds2tree is a bit trickier because you have to know which part of the string belongs to which subtree. Ds2tree returns the part of the string that hasn't been consumed yet.