C Program to Implement Segment Tree

C
// C program to demonstrate Segment Tree implementation

#include <stdio.h>

#define MAX_SIZE 1000

int segmentTree[MAX_SIZE];

// Function to build the segment tree
void buildSegmentTree(int arr[], int node, int start,
                      int end)
{
    if (start == end) {
        segmentTree[node] = arr[start];
        return;
    }
    int mid = (start + end) / 2;
    buildSegmentTree(arr, 2 * node, start, mid);
    buildSegmentTree(arr, 2 * node + 1, mid + 1, end);
    segmentTree[node]
        = segmentTree[2 * node] + segmentTree[2 * node + 1];
}

// Function to query the segment tree
int query(int node, int start, int end, int l, int r)
{
    if (r < start || end < l)
        return 0;
    if (l <= start && end <= r)
        return segmentTree[node];
    int mid = (start + end) / 2;
    return query(2 * node, start, mid, l, r)
           + query(2 * node + 1, mid + 1, end, l, r);
}

// Function to update the segment tree
void update(int node, int start, int end, int idx, int val)
{
    if (start == end) {
        segmentTree[node] = val;
        return;
    }
    int mid = (start + end) / 2;
    if (idx <= mid)
        update(2 * node, start, mid, idx, val);
    else
        update(2 * node + 1, mid + 1, end, idx, val);
    segmentTree[node]
        = segmentTree[2 * node] + segmentTree[2 * node + 1];
}

// Driver code
int main()
{
    int arr[] = { 1, 3, 5, 7, 9, 11 };
    int n = sizeof(arr) / sizeof(arr[0]);
    buildSegmentTree(arr, 1, 0, n - 1);
    printf("Sum of elements in range [1, 3] is %d\n",
           query(1, 0, n - 1, 1, 3));
    update(1, 0, n - 1, 2, 10);
    printf("Sum of elements in range [1, 3] after update "
           "is %d\n",
           query(1, 0, n - 1, 1, 3));
    return 0;
}

Output
Sum of elements in range [1, 3] is 15
Sum of elements in range [1, 3] after update is 20

Segment Tree in C

A Segment Tree is a data structure in C that helps us quickly perform operations (like finding the sum, minimum, or maximum) on a range of elements in an array. It’s like a tree that stores information about parts of the array in each node.

In this article, we will learn what are segement trees, how they work and how to implement them in C language.

Similar Reads

Segment Trees in C

A Segment Tree is a data structure that stores information about a range of elements in its nodes. It is mostly used to handle range queries with updates in an efficient manner. For example, we can perform a range summation of an array between the range L to R while also modifying the array from range L to R all in log (N) time complexity...

Basic Operations on Segement Trees in C

The basic processes provided by segment trees can be outlined as the construction, query, and update....

Implementation of Basic Operations

Algorithm of Build Segement Tree...

C Program to Implement Segment Tree

C // C program to demonstrate Segment Tree implementation #include #define MAX_SIZE 1000 int segmentTree[MAX_SIZE]; // Function to build the segment tree void buildSegmentTree(int arr[], int node, int start, int end) { if (start == end) { segmentTree[node] = arr[start]; return; } int mid = (start + end) / 2; buildSegmentTree(arr, 2 * node, start, mid); buildSegmentTree(arr, 2 * node + 1, mid + 1, end); segmentTree[node] = segmentTree[2 * node] + segmentTree[2 * node + 1]; } // Function to query the segment tree int query(int node, int start, int end, int l, int r) { if (r < start || end < l) return 0; if (l <= start && end <= r) return segmentTree[node]; int mid = (start + end) / 2; return query(2 * node, start, mid, l, r) + query(2 * node + 1, mid + 1, end, l, r); } // Function to update the segment tree void update(int node, int start, int end, int idx, int val) { if (start == end) { segmentTree[node] = val; return; } int mid = (start + end) / 2; if (idx <= mid) update(2 * node, start, mid, idx, val); else update(2 * node + 1, mid + 1, end, idx, val); segmentTree[node] = segmentTree[2 * node] + segmentTree[2 * node + 1]; } // Driver code int main() { int arr[] = { 1, 3, 5, 7, 9, 11 }; int n = sizeof(arr) / sizeof(arr[0]); buildSegmentTree(arr, 1, 0, n - 1); printf("Sum of elements in range [1, 3] is %d\n", query(1, 0, n - 1, 1, 3)); update(1, 0, n - 1, 2, 10); printf("Sum of elements in range [1, 3] after update " "is %d\n", query(1, 0, n - 1, 1, 3)); return 0; }...

Applications of Segment Trees

Segment Trees are used to answer range queries like finding the minimum, maximum, sum, greatest common divisor, least common multiple, etc., in an array in logarithmic time.Segment Trees can handle updates and modifications in the data structure.Segment Trees can be used with a technique called Lazy Propagation to perform range updates in logarithmic time.Segment Trees are used to solve the Range Minimum Query problem, which aims to find the minimum element from a range in an array.Segment Trees can also solve the Range Maximum Query problem, which aims to find the maximum element from a range in an array.Segment Trees can be used to find the count of distinct elements in a range.Segment Trees can be used to find the K-th number in a sequence....

Conclusion

In summary, a Segment Tree is a versatile data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array....