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 6903991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:59:39+00:00 2026-05-27T07:59:39+00:00

I would like to represent a tree of the following shape in Haskell: /\

  • 0

I would like to represent a “tree” of the following shape in Haskell:

   /\                            
  /\/\
 /\/\/\
/\/\/\/\
` ` ` ` `

/ and \ are the branches and ` the leaves. You can see that starting at any node, following the left path, then the right gets you to the same node as following the right path then the left. You should be able to label the leaves, apply a function of the two decendants at each node, and propagate this information to the root in O(n^2) time. My naive efforts are giving me an exponential run time. Any hints?

  • 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-05-27T07:59:40+00:00Added an answer on May 27, 2026 at 7:59 am

    It is certainly possible to construct a tree with shared nodes. For example, we could just define:

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

    and then carefully construct a value of this type as in

    tree :: Tree Int
    tree = Node t1 t2
      where
        t1 = Node t3 t4
        t2 = Node t4 t5
        t3 = Leaf 2
        t4 = Leaf 3
        t5 = Leaf 5
    

    to achieve sharing of subtrees (in this case t4).

    However, as this form of sharing is not observable in Haskell, it is very hard to maintain: for example if you traverse a tree to relabel its leaves

    relabel :: (a -> b) -> Tree a -> Tree b
    relabel f (Leaf x) = Leaf (f x)
    relabel f (Node l r) = Node (relabel f l) (relabel f r)
    

    you loose sharing. Also, when doing a bottom-up computation such as

    sum :: Num a => Tree a -> a
    sum (Leaf n) = n
    sum (Node l r) = sum l + sum r
    

    you end up not taking advantage of sharing and possibly duplicate work.

    To overcome these problems, you can make sharing explicit (and hence observable) by encoding your trees in a graph-like manner:

    type Ptr = Int
    data Tree' a = Leaf a | Node Ptr Ptr
    data Tree a = Tree {root :: Ptr, env :: Map Ptr (Tree' a)}
    

    The tree from the example above can now be written as

    tree :: Tree Int
    tree = Tree {root = 0, env = fromList ts}
      where
        ts = [(0, Node 1 2), (1, Node 3 4), (2, Node 4 5),
              (3, Leaf 2), (4, Leaf 3), (5, Leaf 5)]
    

    The price to pay is that functions that traverse these structures are somewhat cumbersome to write, but we can now define for example a relabeling function that preserves sharing

    relabel :: (a -> b) -> Tree a -> Tree b
    relabel f (Tree root env) = Tree root (fmap g env)
      where
        g (Leaf x)   = Leaf (f x)
        g (Node l r) = Node l r
    

    and a sum function that doesn’t duplicate work when the tree has shared nodes:

    sum :: Num a => Tree a -> a
    sum (Tree root env) = fromJust (lookup root env')
      where
        env' = fmap f env
        f (Leaf n) = n
        f (Node l r) = fromJust (lookup l env') + fromJust (lookup r env')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement a TreeWritable class to represent a Tree structure. I
I have a terrain map which i would like to represent some data in.
I would like to be able to use the same drawable to represent both:
I would like the user to add several pins to a map to represent
I have an array of integers which represent a RGB image and would like
I need to represent huge data(37 columns,1000+ rows) in web. I would like to
Would like a for loop in jquery so that: For every hover_link: show hidden
I am attempting to dynamically build up an expression tree so that I can
It's easy enough to represent a tree or list in haskell using algebraic data
I would like to create a cascading tree/list of N number of children for

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.