Javatpoint Logo
Javatpoint Logo

Segment Tree (Range Maximum Query with Node Update)

Segment Trees are an important data structure in competitive programming and algorithmic problem-solving. We'll go deeper into Segment Trees in this extensive talk, concentrating notably on Range Maximum Query (RMQ) and node update procedures. These procedures enable quick querying and updating of data inside a certain range, providing a valuable tool for effectively tackling a wide range of issues.

  • Segment Trees are a hierarchical data structure that allows for querying and updating actions over intervals or segments inside an array or list. Each node in the tree represents a section of the array, whereas the leaves represent individual items. This structure enables efficient processing of aggregate data across specified segments.

Maximum Range Query (RMQ)

A typical issue is RMQ, which requires determining the greatest value inside a certain range of array members. Consider the following array A = [5, 7, 2, 4, 9, 1]. In this scenario, an RMQ query for the range [1, 4] would return the largest value inside that range, which is 9 (found at index 4).

Update Operations on Nodes

Node update activities include changing the values of array members and then efficiently updating the Segment Tree to reflect these changes. In the context of RMQ, this may involve changing the value of a specific array element and then updating the Segment Tree to preserve consistency.

Segment Tree Structure for RMQ with Node Update

A Segment Tree optimized for RMQ with node updates has a structure that is similar to the one used for other segment-based queries. Each node in the tree keeps the maximum value in its related array segment. This hierarchical structure allows for the quick traversal and retrieval of maximum values within specified ranges.

Segment Tree Representation

A Segment Tree can be represented as an array. The tree's nodes each represent a part of the original array. In the case of RMQ with node updates, each node stores the maximum value inside its associated segment. The root node represents the complete array, whereas the leaf nodes are individual elements.

An array is frequently used to implement the tree, where:

  • The root is represented by index 0.
  • The left and right children of node i are represented by the indexes 2*i + 1 and 2*i + 2.
  • The maximum value for each segment is stored in the tree array.

Segment Tree construction

Segment Tree building is a recursive technique that constructs the tree from the bottom up. Here are the steps for building the Segment Tree:

  1. Initialization: Create an array tree of sufficient size to hold the segment tree nodes.
  2. Function of Construction:
    • Build the segment tree using the recursive function build(node, start, end).
    • For each node node, the segment [start, end] is as follows:
    • If start == end, set the array element at index start to tree[node].
    • In every case, compute the midway index mid = (start + end) // 2.
    • For the left child, recursively call build(2 * node + 1, start, mid).
    • For the correct child, recursively call build(2 * node + 2, mid + 1, end).
    • Update tree[node] with the maximum value between tree[2 * node + 1] and tree[2 * node + 2].

Implementation

Output:

Segment Tree (Range Maximum Query with Node Update)





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA