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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:43:08+00:00 2026-05-14T05:43:08+00:00

I have a merge function which takes time O(log n) to combine two trees

  • 0

I have a merge function which takes time O(log n) to combine two trees into one, and a listToTree function which converts an initial list of elements to singleton trees and repeatedly calls merge on each successive pair of trees until only one tree remains.

Function signatures and relevant implementations are as follows:

merge :: Tree a -> Tree a -> Tree a --// O(log n) where n is size of input trees
singleton :: a -> Tree a            --// O(1)
empty :: Tree a                     --// O(1)
listToTree :: [a] -> Tree a         --// Supposedly O(n)

listToTree = listToTreeR . (map singleton)

listToTreeR :: [Tree a] -> Tree a
listToTreeR []     = empty
listToTreeR (x:[]) = x
listToTreeR xs     = listToTreeR (mergePairs xs)

mergePairs :: [Tree a] -> [Tree a]
mergePairs []       = []
mergePairs (x:[])   = [x]
mergePairs (x:y:xs) = merge x y : mergePairs xs

This is a slightly simplified version of exercise 3.3 in Purely Functional Data Structures by Chris Okasaki.

According to the exercise, I shall now show that listToTree takes O(n) time. Which I can’t. 🙁

There are trivially ceil(log n) recursive calls to listToTreeR, meaning ceil(log n) calls to mergePairs.

The running time of mergePairs is dependent on the length of the list, and the sizes of the trees. The length of the list is 2^h-1, and the sizes of the trees are log(n/(2^h)), where h=log n is the first recursive step, and h=1 is the last recursive step. Each call to mergePairs thus takes time (2^h-1) * log(n/(2^h))

I’m having trouble taking this analysis any further. Can anyone give me a hint in the right direction?

  • 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-14T05:43:09+00:00Added an answer on May 14, 2026 at 5:43 am

    It’s almost there. You already know the expression is

    so the only problem is to evaluate this sum. Using log(AB) = log A + log B and log 2N = N we have

    With help of calculators, we can find that X = O(2m) = O(n), which is expected.

    (If you want to compute this yourself, search for “Geometric series”, or approximate the sum using an integral.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background : I currently have a Web Forms, ASP.NET 3.5/C# application which I'm interested
I have a static method 'findAll' on a model which basically gets all rows
I have a RoR app running rails 2.3.8 which makes use of the Rails
I've created several helper functions which I use when creating templates for Wordpress. An
i'm trying to write a script which would process certain files. The data are
I am using a web application framework (Symfony 1.3.6), which follows the MVC pattern.
I have replicated a stripped-down version of my code that has recently been re-written
I have the following code (I know that this code is not optimized but
I'll start with an example snippet: self.addwidget({ box: ns.box({ text: 'Foobar', fromTop: ~~(Math.random()*window.innerHeight), fromLeft:
So I want to extend for example this RecursiveIterator from the SPL with the

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.