Java-preliminary Understanding-Common Object API (collection frame-treeset collection-two fork tree)

Source: Internet
Author: User

One.

The TreeSet collection can sort the elements in it, there are two sorting methods, and now we want to know what structure it is at the bottom, how does it determine the position of the element? It is for a reason, and its structure is highly efficient in terms of sequencing.

When it comes to sorting, talk about choosing bubbles. Select bubble For comparison times, more, because it is turned around in the ratio. For TreeSet, there's a better place to be

The elements in the example are sorted by age, showing how it is stored.

When the zhangsan,28 is saved, a zhangsan,28 is made in the container. There is only one element in the container and no comparison is made. followed by another lisi,21,lisi to mobilize their own compareto and Zhangsan touch a bit. Lisi and Zhangsan, found smaller than 28, returned a negative number, followed by the left side of 28. Compared with 28, there are only two cases, either large or small. Some people say, there is no equal situation, equality is not in. Then Zhouqi came, and the 281 ratio, bigger than 28, it and 21 do not need to be compared, it is placed directly on the right side of 28. Then 25来, and 28, than 28 small, and 29 compared, so you can improve a bit of efficiency (if the first number of storage is the most value, it is miserable), there is no need to Peng, here is the fork. 25:28 small, go to the left, bigger than 21, go to the right. Finally 24来, than 28 small, go to the left, than 21, go to the right, than 25 small, go left.

We call this structure a two-fork tree, which divides two forks from one node. This is a data structure, usually the same does not come in. How to judge the same, as long as the return 0, will not go in.

Thus, by now we can sort through the binary tree method and determine the position of the element.

What are the characteristics of the nodes in the binary tree? The first node contains a few elements, just like we say linked lists, remember the data behind. The node first remembers the data on the left, which is the small data, and the small one goes to the left. How many references are held in 21 nodes? Two, one is called left, one is called right, and the right place is the big data. More than two, there is a called the father, remember it is his father, altogether is three, but there are no three, up to three.

How does this structure look for data? Starting from 19, it is the smallest, followed up, because 19 has no left and right. To 21, supposedly should look up, but to see 21 right there, there are 25. And then about 25 or so there? Look for them in this order. I felt that the order was strange and could not show the efficiency of the binary tree.

Now say an efficiency problem, if the data stored in a lot of words, each one of the elements, have been adding, very slow, then how to do?

Binary tree is still possible, for example, according to the following, loaded five elements

How do I install it when I'm 19? The five elements are also ordered, and we want to determine the position of an element in an ordered structure, and what is the most convenient way to do it? Two-point search, not from the top level down by comparison, take five elements of the compromise element, the compromise of that element is not necessarily 28, so it each time in the inside to put elements before the first to binary the ordered elements. This is how the binary tree is raised efficiently.

Think of a problem: now with the comparator, it is powerful in the ability to sort the elements in a specified way. Can it not do the orderly action we learned before? How to deposit in, how to take out.

If the implementation how to deposit in, how to take out, that means that the first taken out is 28, then take 21, that is 21 is greater than 28, or Zhangsan first taken out, then is Lisi, that is, Lisi greater than Zhangsan, said is not the age value, but the object, 21 represents this object. Why is it? After the order, take out is the smallest (this and save time, according to what order to save the relevant bar), and then to become larger, so take the time, you must ensure that Zhangsan is less than Lisi, then this how to draw?

If so, 21 is greater than 28 and 21 is on the right side of 28. 21 How can it be greater than 28? Binary tree who cares about your age and name, is what you specify. The binary tree looks at the return value, if you say return 0, it is all the same, do not solidify it on the value. If the binary tree is the following painting, take the first 28,28 in these three numbers is the smallest, the right place is full of big, then this code how to write it?

No, there's no comparison here. All returns a positive number. Return 1 means this, 28 advanced come, Liasi and zhangsan a ratio, Lisi is bigger than Zhangsan, binary tree Regardless of other, see return value is positive or negative, or 0. Watching the output is the same as storing the result, which is ordered. If you want to reverse it, return to 1.

Do not forget the primary and secondary conditions by comparing the conditions you specify.

Java-preliminary Understanding-Common Object API (collection frame-treeset collection-two fork tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.