Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8576267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:53:17+00:00 2026-06-11T19:53:17+00:00

I have a tree data type: data Tree a b = Branch b (Tree

  • 0

I have a tree data type:

data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a

…and I need to make it an instance of Show, without using deriving. I have found that nicely displaying a little branch with two leaves is easy:

instance (Show a, Show b) => Show (Tree a b) where
   show (Leaf x) = show x
   show (Branch val l r) = " " ++ show val ++ "\n" ++ show l ++ "  " ++ show r

But how can I extend a nice structure to a tree of arbitrary size? It seems like determining the spacing would require me to know just how many leaves will be at the very bottom (or maybe just how many leaves there are in total) so that I can allocate all the space I need there and just work ‘up.’ I would probably need to call a size function. I can see this being workable, but is that making it harder than it is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:53:18+00:00Added an answer on June 11, 2026 at 7:53 pm

    You might study the drawTree function in the base Data.Tree module. Just shamelessly importing it would give you something like this:

    import Data.Tree hiding (Tree )
    data Tree a b = Branch b (Tree a b) (Tree a b) 
                  | Leaf a deriving (Eq,Ord,Show)
    
    toDataTree (Leaf a) = Node a []
    toDataTree (Branch b cs ds) = Node b [toDataTree cs, toDataTree ds]
    
    d = Branch "1" (Branch "11" (Leaf "111") (Leaf "112")) 
                   (Branch "12" (Leaf "121") (Leaf "122"))
    
    e = toDataTree d
    f = putStrLn $ drawTree e
    
    {-
    *Main> f
    1
    |
    +- 11
    |  |
    |  +- 111
    |  |
    |  `- 112
    |
    `- 12
       |
       +- 121
       |
       `- 122
    -}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make the following data type an instance of Show : data
I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot
I am trying to have a tree display custom data that looks like this.
I have to build a tree using the following data which comes from a
Let's say I have a binary tree data structure defined as follows type 'a
I have make some functions to retrieve data using the Github API. I have
I have a tree structure. The nodes would have a leaf node type and
I'm writing a ruby app that stores ancestry type data. I.e. Family tree. For
I am having a tree data structure that can have different types of nodes
Basically I have defined a Tree data type which is defined as follows: data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.