Tree array and Leaderboard

Posted by grevathi_02 on Mon, 31 Jan 2022 03:44:23 +0100

I. problem elicitation

Internet products should be designed with a ranking list, which is based on scores. The score range is [11000], and each score can have more than one person.

1. Xiao Ming's score is 300. Find out its ranking.

2. After a period of time, the scores of some users have changed, and Xiaoming's scores have also changed. Ask for his ranking.

Binary tree array

1. Concept

All positive integers can be represented in binary.

For example:

34 = 2^1 + 2 ^7

12 = 2^2 + 2^3

The binary representation of 34 , and 12 , is as follows

For an integer , i, lowbit(i) represents the integer value represented by the last binary position 1 , of , i.

lowbit(12)= 2^2 = 4

lowbit(34) = 2^1 = 2

2. Data structure diagram

Tree array is a kind of tree relational structure established on the array.

3 description

The black array represents the original array (represented by A[i]), and the red structure represents the tree array (represented by C[i]).

C[i] = A[ i - lowbit(i) + 1 ] + A[ i - lowbit(i) + 2 ] + ... + A[i];   

The mathematical meaning of the above formula is: the value of the tree element with subscript () I) = the sum (including) A[i]) lowbit(i) elements in the element array.

Based on this formula, the value of each tree element can be calculated:

C[1] = A[1];     (lowbit(1) = 1)

C[2] = A[1] + A[2];     (lowbit(2) = 2)

C[3] = A[3];      (lowbit(3) = 1)

C[4] = A[1] + A[2] + A[3] + A[4];       (lowbit(4) = 4)

C[5] = A[5];      (lowbit(5) = 1)

C[6] = A[5] + A[6];      (lowbit(6) = 1)

C[7] = A[7];          (lowbit(7) = 1)

C[8] = A[1] + A[2] + A[3] + A[4] + A[5] + A[6] + A[7] + A[8];      (lowbit(8) = 8)

III. application scenarios

With the tree array C[i], we can find the prefix sum of the original array A[i] and quickly update the array C[i] when an element in A[i] is modified.

1 , find the prefix sum of the original array , A , and

Assuming that the sum of the first # I # elements of the original array # A # is # Sum[i], the integer interval [1,i] can be expressed as [1,i-lowbit[i]] and [i-lowbit[i]+1,i].

Assuming j = i-lowbit[i], J < = I "and Sum[0] = 0, Sum[i] = Sum[j] + C[i] (Sum[j] represents the sum of the first interval and C[i] represents the sum of the second interval)

Then calculate the prefix and Sum[i] step by step until Sum[0] = 0.

2. When an element in the original array # A[i] is modified, the tree array # C is modified synchronously

When an element A[i] is modified, all tree array elements C[j] containing A[i] must be modified accordingly.

According to the tree structure above, we can get

C[k+1] = C[k] + lowbit(k)

IV. realization

1. Demand

a get the ranking of people with 10 points.

b) after a period of competition, get a ranking of 10 ¢ points.

2. Realization

package treearray;

import java.util.Random;

/**
* @className: TreeArray
* @description: Tree array
* @date: 2022/1/30
* @author: cakin
*/
public class TreeArray {
    static int lengh = 15;
    // Corresponding array element
    static int a[] = new int[lengh + 1];
    // Corresponding tree array
    static int c[] = new int[lengh + 1];

    /**
     * Function Description: calculate the lowbit of x
     *
     * @param x subscript
     * @return x Corresponding lowbit
     * @author Bei Yi
     * @date 2022/1/30
     * @description:
     */
    static int lowbit(int x) {
        return x & (-x);
    }

    /**
     * Function Description: sum the first i items of array A
     *
     * @param i A Subscript of array
     * @return Sum the first i items of array A
     * @author Bei Yi
     * @date 2022/1/30
     */
    static int sum(int i) {
        int res = 0;
        while (i > 0) {
            res += c[i];
            i -= lowbit(i);
        }
        return res;
    }

    /**
     * Function Description: modify the element of element A[i] and add val value
     *
     * @param i   A Subscript of
     * @param val A Added value
     * @author Bei Yi
     * @date 2022/1/30
     */
    static void update(int i, int val) {
        while (i <= lengh) {
            c[i] += val;
            i += lowbit(i);
        }
        return;
    }

    public static void main(String[] args) {
        // initialization
        for (int i = 1; i <= lengh; i++) {
            a[i] = new Random().nextInt(10) + 1;
            update(i, a[i]);
        }
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();
        for (int i : c) {
            System.out.print(i + " ");
        }
        System.out.println();
        // Ranking of people with 10 points
        System.out.println("Ranking of people with 10 points:");
        System.out.println(sum(lengh) - sum(10) + 1);

        System.out.println("=============After a period of hard work=======================");
        // After a period of hard work, the ranking has changed
        for (int i = 1; i <= lengh; i++) {
            int count = new Random().nextInt(3);
            a[i] += count;
            update(i, count);
        }
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();
        for (int i : c) {
            System.out.print(i + " ");
        }
        System.out.println();
        // Ranking of people with 10 points
        System.out.println("Ranking of people with 10 points:");
        System.out.println(sum(lengh) - sum(10) + 1);
    }
}

3. Test

0 2 9 9 10 3 9 8 1 8 5 6 3 5 9 10
0 2 11 9 30 3 12 8 51 8 13 6 22 5 14 10
 Ranking of people with 10 points:
34
=============After a period of hard work=======================
0 4 9 11 10 4 11 9 2 8 6 6 4 6 9 11
0 4 13 11 34 4 15 9 60 8 14 6 24 6 15 11
 Ranking of people with 10 points:
37

The tide washes away the sand. If you don't advance, you will fall back.

Change lengh# to 100.

0 1 10 3 10 2 4 9 7 10 7 2 9 5 7 1 4 5 2 9 3 7 5 5 3 10 8 10 7 8 2 9 4 3 2 9 5 3 3 2 7 8 8 2 2 7 1 5 4 4 4 1 3 8 5 1 10 9 3 2 5 4 5 2 7 8 10 9 7 1 5 10 7 10 9 6 9 6 6 5 10 7 5 10 1 9 3 7 2 7 7 6 3 8 7 6 2 3 2 6 6
0 1 11 3 24 2 6 9 46 10 17 2 28 5 12 1 91 5 7 9 19 7 12 5 39 10 18 10 35 8 10 9 188 3 5 9 19 3 6 2 34 8 16 2 20 7 8 5 71 4 8 1 12 8 13 1 36 9 12 2 19 4 9 2 332 8 18 9 34 1 6 10 57 10 19 6 34 6 12 5 118 7 12 10 23 9 12 7 44 7 14 6 23 8 15 6 208 3 5 6 17
 Ranking of people with 10 points:
495
=============After a period of hard work=======================
0 3 11 3 10 4 5 11 7 10 9 2 11 7 7 2 5 6 4 11 5 9 5 5 4 11 10 12 7 9 2 11 6 5 4 11 6 4 3 3 9 8 8 4 4 8 2 5 5 6 5 3 4 10 5 1 11 11 3 2 7 6 5 3 7 8 10 10 8 1 6 10 8 12 11 6 9 8 8 7 12 7 6 11 1 11 4 7 4 8 8 7 3 9 8 6 3 4 3 8 8
0 3 14 3 27 4 9 11 54 10 19 2 32 7 14 2 107 6 10 11 26 9 14 5 49 11 21 12 40 9 11 11 224 5 9 11 26 4 7 3 45 8 16 4 24 8 10 5 89 6 11 3 18 10 15 1 45 11 14 2 23 6 11 3 402 8 18 10 36 1 7 10 61 12 23 6 38 8 16 7 134 7 13 11 25 11 15 7 51 8 16 7 26 9 17 6 237 4 7 8 23
 Ranking of people with 10 points:
590

Five references

Detailed explanation of tree array - Xenny - blog Park

Topics: Algorithm architecture