Skip to content

Commit

Permalink
[hs] tree pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-Watters committed Mar 25, 2024
1 parent e953c4a commit 014e2fc
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions haskell/ShowTree.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- | An ASCII art show function for binary trees.

module ShowTree where

import Data.List

data Tree a = Leaf | Node (Tree a) a (Tree a)

data WidthTree a = Leaf | Node (Tree a) a (Tree a)

showTree :: Int -> Tree a -> String
showTree pre (Leaf) = (replicate pre ' ') ++ "x"
showTree pre (Node l x r) = intercalate "\n" (map (replicate pre ' ' ++) ["x", " / \\ ", (showTree pre l ++ " " ++ showTree pre r)])

instance Show a => Show (Tree a) where
show = showTree 0

0 comments on commit 014e2fc

Please sign in to comment.