From e0fff7c4014512bf88b23136dbbb426d9d9b7511 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 30 May 2017 21:57:26 -0400 Subject: finish vol 7 --- 61-69/66.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 61-69/66.hs (limited to '61-69/66.hs') 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 -- cgit v1.2.3