1

Just out of curiosity, i wanted t know how TreeSet maintains order. Of course by comparing new element to add to previous elements either by element object's comparator or any custom comparator. But there could be better approach than comparing all. Lets see some code:

    TreeSet<String> tset= new TreeSet<>(new comparator<String>());
    tset.add("america");
    tset.add("britain");
    tset.add("india");

    tset.add("checksolvakia");
    tset.add("china");
    tset.add("sri_lanka");
    tset.add("zimbabwe");

    for(String str:tset){
        System.out.println(str);
    }

In above code new comparator<String>() is just an custom comparator that do normal string comparisons.

The output drom above code is:
 i have been called times:1
values America America
 i have been called times:2
values Britain America
 i have been called times:3
values india America
 i have been called times:4
values india Britain
 i have been called times:5
values checksolvakia Britain
 i have been called times:6
values checksolvakia india
 i have been called times:7
values china Britain
 i have been called times:8
values china india
 i have been called times:9
values china checksolvakia
 i have been called times:10
values sri_lanka Britain
 i have been called times:11
values sri_lanka china
 i have been called times:12
values sri_lanka india
 i have been called times:13
values zimbabwe Britain
 i have been called times:14
values zimbabwe china
 i have been called times:15
values zimbabwe india
 i have been called times:16
values zimbabwe sri_lanka
america
britain
checksolvakia
china
india
sri_lanka
zimbabwe

Now the questions:
1. why america was compared to america??
2.why everyone else was not compared to america?? is it set as Root??
even if its is set as root why no comparison to root. 3. there could be less comparisons in say zimbabwe could be compared to something not Britain(ie not from very start).

3
  • What JDK are you using? Commented Jul 16, 2013 at 18:23
  • 3
    1. TreeMap compares an object to itself if it is the first element in the map, to verify that the object is comparable with the specified comparator -- in previous JDK versions, there were some unpleasant bugs with null here, for example, or with raw types causing elements of the wrong type to be allowed into the map. Commented Jul 16, 2013 at 18:25
  • thanks louis !! using jdk 7 btw! Commented Jul 16, 2013 at 19:06

1 Answer 1

3

This post could use a lot of polish. A general answer to your question would be the binary nature of sorted trees. We would expect that a value, at max, should only have to be compared to Log(N) entries in the tree, to have its proper place found, assuming a balanced tree. This fact alone answers all of your questions, except the first. A value shouldn't be compared to itself the first value inserted into a tree should be set as root, but in a balanced tree it is important to note, that this doesn't mean that it will be the root node forever. The root node is the "middle" value. Why america is compared to itself is likely an edge case with your comparator, and the behavior of an empty tree, I like louis's comment on this issue.

enter image description here

If you wish to insert a twelve how many comparisons would have to be made? What comparisons would they be? How would this change if the tree were balanced? When you can answer these questions, you will have answered your own question, until then descriptions in words are likely to be unhelpful.

Below is the contents of the tree, after each individual insertion.

America

America
    Britain

    Brit
Amer    Indi

    Brit
Amer        Indi
        Chec    

            Brit
Amer                Chin
              Chec          India
                                SriLanka        

            Brit
Amer                Chin
              Chec          SriLanka
                        India       Zimbabwe

Notice that we are doing our best to keep it balanced, by using what are called "rotations". What this means is that we never completely "re-balance" the tree(a very expensive operation), but when we can avoid creating one long "branch" by shifting nodes at the point of insertion, we do so. Hence we have America over there by itself, never being compared to anything, once it is on it's own on the left side of the tree. Essentially, once an object has two children, it is set in stone, never to be moved. So what we have is "On average" Log(N) comparisons, but we can still end up with mildly unbalanced trees, and end up in scenarios where we have on worse case N/2 comparisons. The larger your data set gets, the harder this scenario is to create, though it is fairly easy to create mini branches of unbalance, for example in this scenario, if America was the "lowest" value, it would never be moved. But the rest of the tree could be perfectly balanced.

Sign up to request clarification or add additional context in comments.

7 Comments

We would expect that a value, at max, should only have to be compared to Log(N) >> this should not be the reason i guess
Britain is compared to America, as is India, but values after that are not, because they don't need to be. If we know something is "greater" than India, we know it is also "greater" than both america and Britain, and therfore we have avoided unnecessary comparisons. Zimbabwe is compared to many more things because of the specific layout of the tree at this time, you have run into a scenario where the parents have lined up in an interesting way to make these comparisons necessary.
INdependent of whether you guess "this should not be the reason" it is, and it is your lack of understanding of sorted binary trees that is at fault. I added an image to help you understand why the logic I presented is as so, and how the balanced, vs unbalanced nature can modify this expectation.
hey man!! thanks you just made my messy day a meaningful day. thanks brother basically i knew nothing about Trees but now i do .thanks again. Tree is a hell of a topic , isn't it??
Yes! If you understand nothing in Computer Science but binary(or tertary, n-) trees you can tackle many of it's most important issues... sorting being an excellent example. XML, HTML, etc... all use trees. Excellent, very simple, yet very complex data structures!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.