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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:05:10+00:00 2026-06-16T21:05:10+00:00

When looking through the docs of Data.Set , I saw that insertion of an

  • 0

When looking through the docs of Data.Set, I saw that insertion of an element into the tree is mentioned to be O(log(n)). However, I would intuitively expect it to be O(n*log(n)) (or maybe O(n)?), as referential transparency requires creating a full copy of the previous tree in O(n).

I understand that for example (:) can be made O(1) instead of O(n), as here the full list doesn’t have to be copied; the new list can be optimized by the compiler to be the first element plus a pointer to the old list (note that this is a compiler – not a language level – optimization). However, inserting a value into a Data.Set involves rebalancing that looks quite complex to me, to the point where I doubt that there’s something similar to the list optimization. I tried reading the paper that is referenced by the Set docs, but couldn’t answer my question with it.

So: how can inserting an element into a binary tree be O(log(n)) in a (purely) functional language?

  • 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-16T21:05:12+00:00Added an answer on June 16, 2026 at 9:05 pm

    There is no need to make a full copy of a Set in order to insert an element into it. Internally, element are stored in a tree, which means that you only need to create new nodes along the path of the insertion. Untouched nodes can be shared between the pre-insertion and post-insertion version of the Set. And as Deitrich Epp pointed out, in a balanced tree O(log(n)) is the length of the path of the insertion. (Sorry for omitting that important fact.)

    Say your Tree type looks like this:

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

    … and say you have a Tree that looks like this

    let t = Node 10 tl (Node 15 Leaf tr')
    

    … where tl and tr' are some named subtrees. Now say you want to insert 12 into this tree. Well, that’s going to look something like this:

    let t' = Node 10 tl (Node 15 (Node 12 Leaf Leaf) tr')
    

    The subtrees tl and tr' are shared between t and t', and you only had to construct 3 new Nodes to do it, even though the size of t could be much larger than 3.


    EDIT: Rebalancing

    With respect to rebalancing, think about it like this, and note that I claim no rigor here. Say you have an empty tree. Already balanced! Now say you insert an element. Already balanced! Now say you insert another element. Well, there’s an odd number so you can’t do much there.

    Here’s the tricky part. Say you insert another element. This could go two ways: left or right; balanced or unbalanced. In the case that it’s unbalanced, you can clearly perform a rotation of the tree to balance it. In the case that it’s balanced, already balanced!

    What’s important to note here is that you’re constantly rebalancing. It’s not like you have a mess of a tree, decided to insert an element, but before you do that, you rebalance, and then leave a mess after you’ve completed the insertion.

    Now say you keep inserting elements. The tree’s gonna get unbalanced, but not by much. And when that does happen, first off you’re correcting that immediately, and secondly, the correction occurs along the path of the insertion, which is O(log(n)) in a balanced tree. The rotations in the paper you linked to are touching at most three nodes in the tree to perform a rotation. so you’re doing O(3 * log(n)) work when rebalancing. That’s still O(log(n)).

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

Sidebar

Related Questions

I was looking through the docs for instance_variable_set and saw that the sample code
I was looking through the docs and stumbled onto mysql_real_escape_string() and I'm not understanding
Looking through a few SQL implementations, I noticed that most DBMSs support defining an
Looking through the documentation for CFDataRef I can't see anything that will compress a
I've been looking into some tutorials on using Core Data, and I've seen consists
After looking through the docs on App Engine and the StringListProperty or the ListProperty
I'm looking through the docs for the jQuery UI Datepicker ( http://jqueryui.com/demos/datepicker/ ), but
After looking through Paypal docs for a solution for this circumstance, I came up
Today while researching for a project I was looking through the docs for Dictionary<TKey,
Heroku recently began supporting Java apps. Looking through the docs, it seems to resemble

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.