Displaying binary tree in Haskell -


data bintree = empty | node (bintree a) (bintree a)     deriving (show) 

i'm trying figure out way display binary tree in manner such each level go down in tree, want add additional * next name of node , have them separated \n.

for example:

let x = node "parent" (node "childleft" (node "grandchildleftleft" emp emp) emp) (node "childright" emp emp) putstrln $ displaytree x 

should return:

"parent" *"childleft" **"grandchildleftleft" *"childright" 

my function (only prints 1 *):

displaytree :: show => bintree -> string displaytree emp = "" displaytree (node head emp emp) = (show head) displaytree (node head left emp) = (show head) ++ "\n*" ++ displaytree left displaytree (node head emp right) = (show head) ++ "\n*" ++ displaytree right displaytree (node head left right) = (show head) ++ "\n*" ++ displaytree left ++ "\n*" ++ displaytree right 

my displaytree function print:

"parent" *"childleft" *"grandchildleftleft" *"childright" 

i want "grandchildleftleft" have ** next instead of *.

any suggestions?

note: don't want change parameters passed function, should stay displaytree :: show => bintree -> string

i think want:

module main (main)  data bintree = empty | node (bintree a) (bintree a)     deriving (show)  showtree :: show => bintree -> int -> string showtree (empty) _ = [] showtree (node t l r) n = replicate n '*' ++ show t ++ "\n" ++ showtree l (n+1) ++ showtree r (n+1)  main :: io () main =     let x = node "parent" (node "childleft" (node "grandchildleftleft" empty empty) empty) (node "childright" empty empty)     putstrln $ showtree x 0 

note accumulator n changes indent level each recursive call.


http://ideone.com/lphcov

"parent" *"childleft" **"grandchildleftleft" *"childright" 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -