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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:45:56+00:00 2026-06-17T09:45:56+00:00

Is the computational complexity of TreeSet methods in Java, same as that of an

  • 0

Is the computational complexity of TreeSet methods in Java, same as that of an AVLTree?

Specifically, I want to know the computational complexity of the following methods:
1.add
2.remove
3.first
4.last
5. floor
6. higher

Java Doc for method description: http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html

For an AVL Tree, there are all O(logn)? Whats the complexity of the above TreeSet Methods?

  • 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-17T09:45:57+00:00Added an answer on June 17, 2026 at 9:45 am

    EDIT: It should be clarified that the time order usually refers to the number of comparisons. Some operations have no comparisons, so the time order could be taken from the number of sub-tasks

    The code below prints the following in Java 8

    O(1) comparisons, however the number of indirections could be O(ln N)
    Performing 10,000 first operations -> 0.0 comparisons/operation
    Performing 10,000 last operations -> 0.0 comparisons/operation
    Performing 10,000 size operations -> 0.0 comparisons/operation
    Performing 10,000 isEmpty operations -> 0.0 comparisons/operation
    Performing 10,000 comparator operations -> 0.0 comparisons/operation
    Performing 10,000 pollFirst operations -> 0.0 comparisons/operation
    Performing 10,000 pollLast operations -> 0.0 comparisons/operation
    Performing 10,000 headSet operations -> 1.0 comparisons/operation
    O(ln N) comparisons
    Performing 10,000 add operations -> 12.9 comparisons/operation
    Performing 10,000 ceiling operations -> 12.9 comparisons/operation
    Performing 10,000 contains operations -> 12.9 comparisons/operation
    Performing 10,000 floor operations -> 12.9 comparisons/operation
    Performing 10,000 higher operations -> 13.9 comparisons/operation
    Performing 10,000 lower operations -> 13.9 comparisons/operation
    Performing 10,000 remove operations -> 10.9 comparisons/operation
    O(N ln N) comparisons
    Performing 10,000 equals operations -> 128853.0 comparisons/operation
    

    the code

    public class TreeSetOrderMain {
        public static void main(String[] args) {
            System.out.println("O(1) comparisons, however the number of indirections could be O(ln N)");
            testOrder("first", (s, i) -> s.first());
            testOrder("last", (s, i) -> s.last());
            testOrder("size", (s, i) -> s.size());
            testOrder("isEmpty", (s, i) -> s.isEmpty());
            testOrder("comparator", (s, i) -> s.comparator());
            testOrder("pollFirst", (s, i) -> s.pollFirst());
            testOrder("pollLast", (s, i) -> s.pollLast());
            testOrder("headSet", TreeSet::headSet);
    
            System.out.println("O(ln N) comparisons");
            testOrder("add", TreeSet::add);
            testOrder("ceiling", TreeSet::ceiling);
            testOrder("contains", TreeSet::contains);
            testOrder("floor", TreeSet::floor);
            testOrder("higher", TreeSet::higher);
            testOrder("lower", TreeSet::lower);
            testOrder("remove", TreeSet::remove);
    
            System.out.println("O(N ln N) comparisons");
            Set<Long> set = LongStream.range(0, 10_000).mapToObj(i -> i).collect(Collectors.toSet());
            testOrder("equals", (s, i) -> s.equals(set));
        }
    
        static void testOrder(String desc, BiConsumer<TreeSet<Long>, Long> op) {
            final LongComparator comparator = new LongComparator();
            TreeSet<Long> longs = new TreeSet<>(comparator);
            final int count = 10_000;
            for (long i = 0; i < count; i++)
                longs.add(i);
            comparator.count = 0;
            for (long i = 0; i < count; i++)
                op.accept(longs, i);
            System.out.printf("Performing %,d %s operations -> %.1f comparisons/operation%n",
                    count, desc, (double) comparator.count / count);
        }
    
        static class LongComparator implements Comparator<Long> {
            long count = 0;
    
            @Override
            public int compare(Long o1, Long o2) {
                count++;
                return Long.compare(o1, o2);
            }
        }
    }
    

    Operations which work on a single element are all O(ln n) comparisons except first and last which are O(1) comparisons or O(ln N) node search time.

    comparator(), iterator(), clear(), first(), isEMpty(), size(), last(), pollFirst(), pollLast(), headSet() are O(1)

    add(), ceiling(), contains(), floor(), higher(), lower(), remove(), subSet(), tailSet() are O(ln N)

    clone(), hashCode(), toArray() and toString() are O(n) for operations, but no comaprisons

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

Sidebar

Related Questions

Premise: This Wikipedia page suggests that the computational complexity of Schoolbook long division is
That is, the computational complexity. Does it have to count all the elements? Does
In Matters Computational I found this interesting linear search implementation (it's actually my Java
I have a computational geometry problem that I feel should have a relatively simple
When using a computational expression, the first definition works but the second does not
I am computational scientist that work with large amount of simulation data and often
I have a computational intensive project that is highly parallelizable: basically, I have a
How can I specify computational complexity of an algorithm in Big-O notation whose execution
Is there any computational difference between these two methods of checking equality between three
I have designed a user interface for my computational software using swing (Java). I

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.