summaryrefslogtreecommitdiff
path: root/61-69/66.hs
diff options
context:
space:
mode:
Diffstat (limited to '61-69/66.hs')
-rw-r--r--61-69/66.hs17
1 files changed, 17 insertions, 0 deletions
diff --git a/61-69/66.hs b/61-69/66.hs
new file mode 100644
index 0000000..8a3f69c
--- /dev/null
+++ b/61-69/66.hs
@@ -0,0 +1,17 @@
+data Tree a = Empty | Branch a (Tree a) (Tree a) deriving (Show, Eq)
+
+layout :: Tree Char -> Tree (Char, (Int, Int))
+
+-- layout': the second component of result is the distance of left edge to the
+-- axis of root, and the thrid is the distance of right edge.
+
+layout t = let (ret, ld, _) = layout' t x0 1
+ x0 = 1 + maximum ld in ret
+ where layout' :: Tree Char -> Int -> Int -> (Tree (Char, (Int, Int)), [Int], [Int])
+ layout' Empty x y = (Empty, [], [])
+ layout' (Branch v l r) x y = (Branch (v, (x, y)) lt rt, ld, rd)
+ where (lt, lld, lrd) = layout' l (x - hsep) (y + 1)
+ (rt, rld, rrd) = layout' r (x + hsep) (y + 1)
+ hsep = 1 + (maximum $ 0:zipWith (+) lrd rld) `div` 2
+ ld = 0:map (+ hsep) lld
+ rd = 0:map (+ hsep) rrd