-3

I believe that Java TreeSet has a balanced tree implementation which can perform tree sort(a lot like the quick sort) quickly. My recently-encountered problem shows that it seems to be performing more comparisons that I expect.

Does every balanced tree perform like Java's TreeSet on sort(insert), or is my performance testing approach wrong?

My code:

package expri.johnny.treeSortCompTimes;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetCompTimeTest {
    public static void main(String[] args) {
        int size=1000;
        ArrayList<Integer> ay =new ArrayList<>(size);
        int treeHeight =   (int) (Math.log (size)/Math.log (2));
        System.out.println("tree height: "+treeHeight); 
        System.out.println("when size is "+ size + ",i expected compare tmies are: "+((treeHeight+1)*treeHeight/2)); 

        for(int i =0,l=size-1;i<=l;i++){
            ay.add(i);
        }


        Collections.shuffle(ay);
        System.out.println(ay);
        CountComp cc =new CountComp();
        TreeSet<Integer> ts =new TreeSet<>(cc);
        ts.addAll(ay);
        cc.speakCount();
    }

    private static class CountComp implements Comparator<Integer>{
        int counter =0;
        @Override
        public int compare(Integer o1, Integer o2) {
            this.counter++;
            int df = o1-o2;
            if(df>0){
                return 1;
            }else if(df==0){
                return 0;
            }else{
                return -1;
            }

        }
        public void speakCount(){
            System.out.println("total compared times: "+this.counter); 
            //when size is 100,i expected compare tmies are: 21 
                //total compared times: 545
            //when size is 1000,i expected compare tmies are: 45
                //however, the real total compared times: 8783
        }
    }
}

well , that is what is in my mind before:

10 compares for add
10 compares for add
10 compares for add
10 compares for add
10 compares for add
10 ...
10
10
10
10
10
10 -997th , what i thought was 7 
10 -998th  , what i thought  was 8 
10 - 999th , what i thought  was 9
10 - 1000th  , what i thought was 10 
8547

i make big laughing material this time , hahaha!

Johnny
  • 387
  • 2
  • 12
  • You add 1000 elements and you expect only 45 comparisons? – Sotirios Delimanolis Jul 01 '16 at 16:41
  • and the "Collections.sort(ay, cc);" method ,produce the smiler result. – Johnny Jul 01 '16 at 16:41
  • @SotiriosDelimanolis: The docs do say that you're guaranteed [log(n) time to `add` elements](https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html), but I'm not entirely sure if this factoid is what the OP is referencing. – Makoto Jul 01 '16 at 16:43
  • To clarify log(N) is for adding 1 element. To add N elements, it's thus N*log(N). Same as to sort a list of N. – JB Nizet Jul 01 '16 at 16:45
  • but the TreeSet grow in time , not the "Size:N" at the first time. the log(N) would be vary – Johnny Jul 01 '16 at 16:49
  • Also consider that as elements are added the size of the tree grows. – copeg Jul 01 '16 at 16:49
  • 1
    @Johnny the complexity is still O(n*log(n)). It's not an exact value. It's a complexity. – JB Nizet Jul 01 '16 at 17:01
  • since some problem i don't understand before still contribute negative reputation points to me, squeezing me towards the edge of "ask question button" lock down situation, is there any way to delete such question post for all good intention . i can't find the delete button , does the negative point post just a mark as i am stupid? or my stupidity help any one ? or the negative points actually helping some one ? – Johnny Mar 01 '17 at 08:04

2 Answers2

1

You are correct when assume that insert will use log(n) comparison. But you insert n elements that give us n*log(n) comparison. For 1000 elements it give us 9965 comparison.

n here is total amount of elements.

Your formula is totally wrong and I don't know where you got it.

talex
  • 17,973
  • 3
  • 29
  • 66
1

The depth of the tree is dynamic as items are added. To estimate the number of comparisons that will be made, iterate over the size of elements to be added and calculate the number of comparisons that will be made for each element based upon the size of the tree at that time.

int sum = 0;
for(int i = 1; i <= size; i++){
    sum += (int)Math.ceil(Math.log(i)/Math.log(2));
}
System.out.println(sum);

Which yields 8987 for 1000 elements.

copeg
  • 8,290
  • 19
  • 28
  • 10 10 10 10 10 10 10 10 10 10 10 10 -997th , what i thought is 7 10 -998th , what i thought is 8 10 - 999th , what i thought is 9 10 - 1000th , what i thought is 10 8547 – Johnny Jul 01 '16 at 17:10