Data structure tree-like array

Source: Internet
Author: User

I. RELATED definitions

Tree-like array

    • Gets the number of consecutive n in the array and
    • To modify the value of a point in an array
    • Complexity of Time: O (LOGN)

Summary: The strength of a tree array is to maintain queries on arrays (for example, to modify the value of a point, to find the sum of a range). Of course, when the data size is very small, it is very easy to modify the value of a point, the complexity is O (1), but for the sum of an interval to sweep again, the complexity is O (N), if the real-time array of M modified or summed, the worst case complexity is O (m*n), This is not going to come when the scale increases! The same complexity of the tree array is O (M*logn).

Second, the algorithm description

List the knowledge definitions that will appear:

    • Lowbit (k): The k's binary high 1 all empty , leaving only the lowest bit of 1 (that is, only one of the lowest relative number of 1)
    • Solution Lowbit (k): Lowbit (k) =k&-k
    • Role of Lowbit (k): Contacting arrays A and Arrays C
    • CK: from AK (including AK) to the left continuously for the number of lowbit (k) and
    • Go to the tail: the tail should be removed 1 are removed to change to a higher 1, remember that each transformation must have a high 1 generation
    • Go-to-tail implementation: K + = Lowbit (k)

The picture is as follows (figure A on the left and Figure B on the right):

"Building Relationships"

Is the figure a much like a tree? Yes, that's why it's called a tree-like array. A, the A array is the array we want to maintain and query, but in fact we do not use an array of a in the whole process, you can think of it as a decoration! The C array is the center of attention and manipulation of our whole process. Let's take a look at the rules of the C array, where c8=c4+c6+c7+a8,c6=c5+a6 ... ... Do not have to tangle how to do, we just know the approximate rules of the C array can be, it is easy to know that C8 represents the A1~a8 and , but C6 is to show A5~a6 and , why will it produce such a difference? Or why the people who invented it were treated differently? The answer is, this will make the operation easier! See this believe some people have some feeling, why is the complexity of the log? As can be seen,C8 can be seen as the left half of a1~a8 and + right half and , while the left half and is determined by the C4, the right half is actually the same rule to divide a5~a8 into two ... continue to be divided into a split until not , you can look at the B chart. What do you think? Is it a bit of a two-point taste? Yes, the tree-like array is a clever use of two points, it is not mysterious, the key is its ingenious!

How does it do it in a constant split? Say this before I say a thing called Lowbit,Lowbit (k) is the K of the binary of the high 1 all empty, leaving only the lowest bit of 1, such as 10 of the binary is 1010, then Lowbit (Ten) =lowbit (1010) = (0010) 2. In this lowbit is often used in the following, here is a very convenient way to implement, the more common method lowbit (k) =k&-k, this is a bitwise operation, we know that a number plus a minus is the number of binary negation + 1, such as-10 of the binary is -1010=0101+1=0110, and then with 1010&0110, the answer is 0010! Understand the method of solving lowbit can be, continue below. In the following discussion of the decimal has no meaning (the world is a binary, the person should be subjective to build a decimal), the following all the number is not specifically described as a binary.

Above so many words say lowbit, haven't said its use, it is to contact a array and C array! CK indicates that the number of lowbit (k) is continuously calculated from AK to the left. For example c[0110]=a[0110]+a[0101], that is, starting from 0110 (including 0110) to the left to calculate the number of lowbit (0110) and, (because Lowbit (0110) = 0010) that is, starting from 0110 (including 0110) to the left to calculate 0010 (that is, 2) the number of the and, the 0010 numbers are a[0110] and a[0101]. because Lowbit (0110) = 0010, you can see actually only the low 1 function, because obviously can write c[0010]=a[0010]+a[0001], this is why any number of us only care about its lowbit, Because the high point does not work (based on our dichotomy it must be so!) ), unless the rest of the high-level is 0, then itself is lowbit.

"Update location data"

Now that the relationship is set up, and see how to achieve a a location data changes, it will not be directly changed (at the beginning, a does not exist), it each change in fact to maintain the C array should be the nature , because the sum to be used later. and maintenance is very simple, such as changing the a[0011], we are going to modify the c[0011],c[0100],c[1000], it is very easy to see from the picture, but you may ask, what is the inevitable connection between them? Every time the solution is not always take the picture to see it? In fact, the change from the 0011-->0100-->1000 is to carry out the "tail" Operation , that is, the tail should be removed 1 removed, switch to a higher 1, remember that each transformation must have a high 1 generation , so 0100 is not able to change to 0101, because there is no new high 1 generation, the transformation process happens to be able to use our lowbit,K + + lowbit (k).

Well, now that the updates are in order, there may be a new question: Why does it have to be this kind of relationship? This is to be investigated before we say C8 can be regarded as the left half of the a1~a8 and the + right half and ... Of the content , why c[0011] will affect c[0100] without affecting c[0101], which is what was said before c[0100] The solution is actually such a segmented interval c[0001]~c[0001] and interval c[0011 ]~C[0011] and , the number is too small, may not quite understand, in such as c[0100] will affect c[1000], why? Because c[1000] can be regarded as 0001~0100 and plus 0101~1000 , but the number of 0101-bit changes and will directly affect the c[1000], because its tail 1 can not be jumped two in the jump at two high-order 1, is through the c[ 0110] indirectly, however, c[0100] can jump one level to produce a high 1.

Perhaps the above said you are more around, then you just need to pay attention to:C of the constituent nature (in fact, the group nature) determines c[0011] only directly affect c[0100], and c[0100] only directly affect [], and the relationship between the following table is also must be k + = Lowbit (k).

This is where we write the code to update the maintenance tree :

void Add (int k,int num) {while (k<=n) {tree[k]+=num;k + = k&-k; K&-k is Lowbit (k)}}

With the foundation above, it is easier to say the sum .

For example, the 0001~0110 and directly c[0100]+c[0110], the analysis method and the above is reversed, and the wording is reversed, the specific will not repeat:

int read (int k)//1~k interval and {int sum=0;while (k) {sum + = tree[k];k-= k&-k;} return sum;}

Iii. Summary of the blog

    1. The tree-like array is grouped by two pairs of arrays;
    2. Maintenance and query are all the complexity of O (LOGN);
    3. Lowbit here is just a technique, the key is to understand the composition of the C array law ;
    4. The process of analysis the binary system must be popular, as the mind of the Decimal

Data structure tree-like array

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.