Algorithm understanding-tree Array

Source: Internet
Author: User

Tree array is a common computing optimization method. Its complexity is generally nlog (N) and has very powerful functions, such as prefix sum, partial sum, and reverse order, DP optimization can be solved. Some people often say that it is a simplified version of the Line Segment tree, but it also has some limitations. For many Interval Update statistics problems, tree arrays are often powerless, in view of the fact that most of the materials are explained in the form of trees, I started to get started. Now I have read it again and found that I have a deeper understanding, at the same time, I also felt the magic of bitwise computing and the admiration of the person who invented the tree array.


A tree array, as its name implies, is an array, which is stored in the tree. It is easy to think of the Binary Tree in data structure, but it is not a binary tree here. To be exact, it should be a multi-tree. If you want to see it, you can go to Baidu encyclopedia to see it, where there is a picture.


The clever tree array uses the segmentation statistics method of digital statistics and the binary representation method to construct a tree without edges. I think we need to explain the multipart method and binary representation of a number array.


Let's take a look at an example:

Array a1 = 2, a2 = 1, A3 = 4, A4 = 2, A5 = 5, A6 = 3, A7 = 6, A8 = 4, A9 = 1, a10 = 2, A11 = 4, A12 = 7, A13 = 9, A14 = 8, A15 = 5, A16 = 6

1111 (B) This is the binary representation of 15. Suppose we want to calculate element 1 ~ The sum of 15, so we can divide it into 4 statistical sections, 0001 ~ 1000 (1 ~ 8), 1001 ~ 1100 (9 ~ 12), 1101 ~ 1110 (13 ~ 14), 1111 ~ 1111 (15 ~ 15), we can see that the segmentation method takes the length of 2 ^ K as a segment, and 4 segments correspond to 2 ^, 2 ^, 2 ^, 2 ^, 1 ^ 0 respectively.


After the segmentation, let's take a look at the representation of the tree array. What is stored in the tree array? Let's set the tree array to C [size], then

What does C [I] mean? For another example, we need to calculate the sum of the first 10 elements. We make C [10] = C [1010 (B)] = A [1001 (B)] + A [1010 (B)]; that is, the length of the segment divided by the last position 1, here, the last digit 1 is at 2nd bits, so it is 2 ^ 1 length, indicates the sum of the number of bitwise weights from the number of I to the first 2 ^ K (that is, the number of bitwise weights represented by the last bit 1 of the binary number of I, this is exactly the same as the segmentation method described in the preceding example. C [1010] can understand that the first two elements of the element number are 10 and <= 1010, that is, whether the sum of 2 ^ 1 length elements has the feeling of digital statistics. In fact, it is similar, but the tree array can be modified and adjusted. in simple words, the tree array is dynamic, the digital statistics are static and the tree array is cleverly designed so that the binary can represent all the connections on a multi-tree.


When we calculate C [10] = C [1010], we can first calculate the value of C [1010], that is, a [1010] + A [1001]; then we can subtract the two numbers, that is, the sum of the first eight elements, c [8] = C [1000] = A [0001] + A [0010] + ..... + A [1000] = sum [8], obviously C [2 ^ K] indicates 1 ~ 2 ^ k total. The sum of the first 10 elements is sum = C [10] + C [8];


After knowing the segment length with 2 ^ K and the meaning of tree array C, we can know that to calculate the first n sums, we must divide N into multiple 2 ^ K segments, the length cannot be the same. During statistics, you only need to count all the length segments it contains to know which tree array elements should be added, the Field Length indicates the bitwise right of the last digit 1 in the N binary, so it is okay to locate the position 1 at the end.


How can we find the last one? Our predecessors have already opened the way for us to find the bitwise lowbit of the last 1 of X = x & (-x ).

Here we only talk about statistics, but we didn't talk about how to insert data. With the help of statistics, searching becomes simple. Obviously, you only need to change the tree array containing inserted elements, suppose we want to insert a [I], which arrays contain a [I? A [I] must be included in at least one segment length. You only need to find the shortest segment length, which is obviously included in the minimum segment length. Then we only need to find the bit right of the last 1, then the next update is the X = I + lowbit (I) element. If X is updated, the update should also be included in X, and y = x + lowbit (X ); this continues until the maximum element number is exceeded.

The specific implementation code is as follows:

int lowbit(int n){return n&(-n);}int sum(int n) {int ans=0;while(n){ans+=c[n];n-=lowbit(n);}return ans;}void up(int n,int k){int i;for(i=n;i<=tem;i+=lowbit(i)){c[i]+=k;}}

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.