Tree-like array of data structures

Source: Internet
Author: User

Tag: Cheng represents the smallest cell i+1 tle linear structure worst case structure

1. Overview

The tree array (binary indexed tree) is a novel array structure that efficiently fetches the number of consecutive n in an array. In summary, a tree array is often used to solve the problem that the elements in the array {A} can be constantly modified, and how can the sum of several consecutive numbers be obtained quickly?

2. Basic operation of tree-like array

The complexity of element modification and continuous element summation for a traditional array (n elements) is O (1) and O (n), respectively. The tree-like array transforms the linear structure into a pseudo-tree structure (linear structures can scan the elements individually, and the tree structure allows for a jump scan), which makes the modification and summation complexity both O (LGN), greatly improving the overall efficiency.

Given a sequence (series) A, we set an array C to satisfy

C[i] = a[i–2^k+ 1] + ... + a[i]

where k is I in the binary at the end of the number of 0, I start from 1 count!

Then we call c a tree-like array.

The question below is, given I, how do I ask for 2^k?

The answer is simple:2^k=i& (i^ (i-1)), which is i& (-i)

The following explanation:

Take I=6 as an example (note: a_x indicates that the number a is an X-binary representation):

(i) _10 = (0110) _2

(i-1) _10= (0101) _2

I xor (i-1) = (0011) _2

I and (I xor (i-1)) = (0010) _2

2^k = 2

C[6] = c[6-2+1]+...+a[6]=a[5]+a[6]

The exact meaning of array C is as follows:

When we modify the value of a[i], we can go upstream from c[i] to the root node, adjust all c[on this path], the complexity of this operation in the worst case is the height of the tree is O (logn). In addition, for the first n of the sequence number and, just find all of the largest subtree before n, the root of the node C together. It is not difficult to find that the number of these subtrees is n at the binary 1 number, or the number of the power of the N expansion into 2 and the time, so the complexity of the sum operation is also O (Logn).

The tree array can quickly find any interval of sum: a[i] + a[i+1] + ... + a[j], set sum (k) = A[1]+a[2]+...+a[k], then a[i] + a[i+1] + ... + a[j] = SUM (j)-sum (i-1).

The following is a C-language implementation of a tree-like array:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 //求2^kintlowbit(intt){    returnt & ( t ^ ( t - 1 ) );}//求前n项和intsum(intend){   intsum = 0;    while(end > 0)  {     sum += in[end];     end -= lowbit(end);  }  returnsum;}//增加某个元素的大小voidplus(intpos, intnum){   while(pos <= n)  {     in[pos] += num;      pos += lowbit(pos);  }}

3, extension--two-dimensional tree-like array

One-dimensional tree arrays can easily be extended to two-dimensional, two-dimensional tree arrays as follows:

C[x][y] = SUM (A[i][j])

Among them, x-lowbit[x]+1 <= i<=x and y-lowbit[y]+1 <= J <=y

4. Application

(1) One-dimensional tree-like array:

See also: http://hi.baidu.com/lilu03555/blog/item/4118f04429739580b3b7dc74.html

(2) Two-dimensional tree-like array:

A large matrix of numbers that can be manipulated in two ways

1) Add an integer to a number in the matrix (can be positive negative)

2) query all the numbers in a sub-matrix and

Requirements for each query, output results

5. Summary

The tree array was originally discovered when designing a compression algorithm (see Resources 1), and it is now often used to maintain sub-sequences and. It is similar to the line tree (see section Tree of data structure), which is less space-saving and programming complexity than line-segment trees, but smaller in scope than segment trees (such as querying each interval minimum problem).

6. References

(1) Binary Indexed Trees:

Http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees

(2) Gaurap article "Tree-like array":

Http://www.java3z.com/cwbwebhome/article/article19/zip/treearray.zip

(3) Guo Wei article "segment Tree and tree-like array":

Http://poj.org/summerschool/1_interval_tree.pdf

----------------------------------------------------------------------------------------------more information on data structures and algorithms, See: Data structure and algorithm summary----------------------------------------------------------------------------------------------

Original articles, reproduced please specify: Reproduced from Dong's blog

This article link address: http://dongxicheng.org/structure/binary_indexed_tree/

Tree-like array of data structures

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.