Tree-like array (primary knowledge)

Source: Internet
Author: User

Reference: tree-like arrays

Summary: a data structure for array column prefix and can be in O (log (n)) time to get any prefix of the sequence and also at the same time on a constant, is a very powerful data structure.

know: for some time always thought that the tree array is a normal array, but it can be used to store binary search tree, and later know that the goods is actually a difficult thing. But say difficult, in fact it is also a pretty simple, good start of a thing-the key code is quite concise, but a little difficult to understand, but, with a diagram can be expressed clearly:

PS: Here to steal a map (source)

If it's not clear yet, it's normal to take a look at a specific example:
There are arrays A,a = {1, 2, 3, 4, 5, 6, 7, 8}, and then there is a bit array for storing the tree array with the values as follows:
BIT[1] = a[1];
BIT[2] = a[1]+a[2];
BIT[3] = a[3];
BIT[4] = a[1]+a[2]+a[3]+a[4];
BIT[5] = a[5];
BIT[6] = a[5]+a[6];
BIT[7] = a[7];
BIT[8] = a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8].
Then you can see that the values of the above array have another rule:
BIT[1] = a[1];
BIT[2] = a[2]+bit[1];
BIT[3] = a[3];
BIT[4] = a[4]+bit[2]+bit[3];
BIT[5] = a[5];
BIT[6] = a[6]+bit[5];
BIT[7] = a[7];
BIT[8] = a[8]+bit[4]+bit[6]+bit[7].
What do you think is the magic thing that corresponds to the eight above?
Well, actually that figure, want to express is that several formulas, from those several examples we can actually vaguely see the bit array of the law, but how does this thing use program to express?

1, first there must be a preparatory function lowbit(int), the return parameter to the binary after the last position of 1 represents the value:

int lowbit(int num){    return num&(-num);}

Some bit arithmetic operations are used here.

2, then is the bit array of constructs , equals what?

void build(){    for(int i=1;i<=n;i++)    {        bit[i]=a[i];        for(int j=i-1;j>i-lowbit(i);j-=lowbit(j))            bit[i]+=bit[j];    }}

This code is the expression of that figure, as for why, the reader to think about it, there will be a great harvest Oh!

3, next to see how this thing do high-speed to seek prefixes and :

intsum(intindex){    int res=0;    for(int i=index;i>0;i-=lowbit(i))        res+=bit[i];    return res;}

If you understand the build function above, then this code must be second-hand, here I give an example:
or the 8-bit array above, with the sum array representing the prefix of the A array and:
SUM[1] = bit[1];
SUM[2] = bit[2];
SUM[3] = bit[2]+bit[3];
SUM[4] = bit[4];
SUM[5] = bit[4]+bit[5];
SUM[6] = bit[4]+bit[6];
SUM[7] = bit[4]+bit[6]+bit[7];
SUM[8] = bit[8].
Refer to the code for a moment ...

4, modify How to break, if give A[i] added δ:
Directly on the code bar:

void add(intindex,int delta){    for(int i=index;i<=n;i+=lowbit(i))        bit[i]+=delta;}

Understand the 2, just read 3, 4!

Summary:
1, beautiful data structure, no more beautiful!
2, with the binary thinking problem!

Tree-like array (primary knowledge)

Related Article

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.