# Segment Tree

## Faster Range Queries

For this article we want to introduce the idea of a Segment Tree.
Segment Trees allow us to quickly perform range queries as well as range updates.
Suppose we had an array and we wanted to know the sum of a particular range of numbers as well as update the array when necessary.
Normally, if we were to just use an array updating would take `O(1)`

time but a sum query could take up to `O(n)`

as it could entail looping through the entire array.
Segment Trees make both operations a `O(log(n))`

operation.

## Array to Tree

Segment trees work by breaking down the array into a binary tree where each node represents a segment of the array.
Each node in the binary tree is created by taking the existing segment, cutting it in half and distributing it to the children nodes.
Here is a graphic to give you an idea of how this tree looks like.
Note that every node has `[i,j]`

displayed which shows the interval covered by that particular node in the tree.

## Query and Update

Suppose we want to compute the sum of an array on an interval. After building the tree we can compute this query by moving down the tree until the segment represented by our tree is completely within the bounds of the interval. We then take all our segments and compute the sum of the segments to compute the sum of the interval.

Updating our tree works in a similar fashion. Suppose we want to update a particular point in the array. This would mean we recursively work our way down to the leaf node that contains only that node and update it. Then when we resolve the recursive stack we make sure to update all the parent nodes that contain that segment to the new value.

Now that we visually have a good understanding of what our data structure will look like, let's try putting it into some code.
An implementation detail that can simplify things is that we can actually use a linear array to represent our tree.
We can make a new left node by taking our current node and doing `2 * n`

if `n`

is our current node and `2 * n + 1`

to represent the right node.
For this example we will assume we want to calculate the sum of the array on an interval.
It can also be noted that both `1`

-idexed and `0`

-indexed arrays can both work for segment trees and it is mostly up to personal preference.
BUT, the segment tree must be `1`

-indexed.

## For the Interview

Segment tree is only useful for problems involving range queries. The implementation is rather tricky to get right. However, knowing the existence and concept of this data structure would likely impress the interviewer. It's good to know but definitely focus on the more core patterns if you are short on time. Consider this an extra credit.

## Implementation

```
1class segment_tree:
2 def __init__(self, arr):
3 self.tree = [0] * (4 * len(arr))
4 for i in range(len(arr)):
5 self.update(1, 0, len(arr) - 1, i, arr[i])
6
7 def update(self, cur, cur_left, cur_right, idx, val):
8 # make sure we reach leaf node when the left interval equals right interval and return the value located in the tree
9 if cur_left == cur_right and cur_left == idx:
10 self.tree[cur] = val
11 else:
12 # compute value of the midpoint where we cut the segment in half
13 cur_mid = (cur_left + cur_right) // 2
14 # remember n * 2 is left child node and n * 2 + 1 is the right child node
15 if idx <= cur_mid:
16 self.update(cur * 2, cur_left, cur_mid, idx, val)
17 else:
18 self.update(cur * 2 + 1, cur_mid + 1, cur_right, idx, val)
19 # after updating the values, compute the new value for the node
20 self.tree[cur] = self.tree[cur * 2] + self.tree[cur * 2 + 1]
21
22 def query(self, cur, cur_left, cur_right, query_left, query_right):
23 # if our current left interval is greater than the queried right interval it means we are out of range
24 # similarly, if the current right interval is less than the queried left interval we are out of range and in both cases return 0
25 if cur_left > query_right or cur_right < query_left:
26 return 0
27 # check if we are in range, if we are return the current interval
28 elif query_left <= cur_left and cur_right <= query_right:
29 return self.tree[cur]
30 # this means part of our interval is in range but part of our interval is not in range, we must therefore query both children
31 cur_mid = (cur_left + cur_right) // 2
32 return self.query(cur * 2, cur_left, cur_mid, query_left, query_right) + self.query(cur * 2 + 1, cur_mid + 1, cur_right, query_left, query_right)
33
```

```
1public static class SegmentTree {
2 int [] tree;
3
4 public SegmentTree(int [] arr) {
5 tree = new int [4 * arr.length];
6 for (int i = 0; i < arr.length; i++) {
7 update(1, 0, arr.length - 1, i, arr[i]);
8 }
9 }
10
11 void update(int cur, int curLeft, int curRight, int idx, int val) {
12 // make sure we reach leaf node when the left interval equals right interval and return the value located in the tree
13 if (curLeft == curRight && curLeft == idx) {
14 tree[cur] = val;
15 }
16 else {
17 // compute value of the midpoint where we cut the segment in half
18 int curMid = (curLeft + curRight) / 2;
19 // remember n * 2 is left child node and n * 2 + 1 is the right child node
20 if (idx <= curMid) update(cur * 2, curLeft, curMid, idx, val);
21 else update(cur * 2 + 1, curMid + 1, curRight, idx, val);
22 // after updating the values, compute the new value for the node
23 tree[cur] = tree[cur * 2] + tree[cur * 2 + 1];
24 }
25 }
26
27 int query(int cur, int curLeft, int curRight, int queryLeft, int queryRight) {
28 // if our current left interval is greater than the queried right interval it means we are out of range
29 // similarly, if the current right interval is less than the queried left interval we are out of range and in both cases return 0
30 if (curLeft > queryRight || curRight < queryLeft) {
31 return 0;
32 }
33 // check if we are in range, if we are return the current interval
34 else if (queryLeft <= curLeft && curRight <= queryRight) {
35 return tree[cur];
36 }
37 // this means part of our interval is in range but part of our interval is not in range, we must therefore query both children
38 int curMid = (curLeft + curRight) / 2;
39 return query(cur * 2, curLeft, curMid, queryLeft, queryRight) + query(cur * 2 + 1, curMid + 1, curRight, queryLeft, queryRight);
40 }
41}
42
```

```
1class SegmentTree {
2 constructor(arr) {
3 this.tree = Array(4 * arr.length).fill(0);
4 for (let i = 0; i < arr.length; i++) {
5 this.update(1, 0, arr.length - 1, i, arr[i]);
6 }
7 }
8
9 update(cur, curLeft, curRight, idx, val) {
10 // make sure we reach leaf node when the left interval equals right interval and return the value located in the tree
11 if (curLeft === curRight && curLeft === idx) {
12 this.tree[cur] = val;
13 } else {
14 const curMid = Math.floor((curLeft + curRight) / 2);
15 // n * 2 is the left child node and n * 2 + 1 is the right child node
16 if (idx <= curMid) {
17 this.update(cur * 2, curLeft, curMid, idx, val);
18 } else {
19 this.update(cur * 2 + 1, curMid + 1, curRight, idx, val);
20 }
21 // compute the new value for the node
22 this.tree[cur] = this.tree[cur * 2] + this.tree[cur * 2 + 1];
23 }
24 }
25
26 query(cur, curLeft, curRight, queryLeft, queryRight) {
27 // if our current left interval is greater than the queried right interval, it means we are out of range
28 // similarly, if the current right interval is less than the queried left interval, we are out of range, and in both cases return 0
29 if (curLeft > queryRight || curRight < queryLeft) return 0;
30 // check if we are in range, if we are return the current interval
31 else if (queryLeft <= curLeft && curRight <= queryRight) {
32 return this.tree[cur];
33 }
34 // this means part of our interval is in range but part of our interval is not in range, we must therefore query both children
35 const curMid = Math.floor((curLeft + curRight) / 2);
36 return this.query(cur * 2, curLeft, curMid, queryLeft, queryRight) + this.query(cur * 2 + 1, curMid + 1, curRight, queryLeft, queryRight);
37 }
38}
39
```

```
1struct SegmentTree {
2 int* tree;
3
4 SegmentTree(std::vector<int> arr) {
5 tree = new int[4 * arr.size()]();
6 for (int i = 0; i < arr.size(); i++) {
7 update(1, 0, arr.size() - 1, i, arr[i]);
8 }
9 }
10
11 void update(int cur, int cur_left, int cur_right, int idx, int val) {
12 // make sure we reach leaf node when the left interval equals right interval and return the value located in the tree
13 if (cur_left == cur_right && cur_left == idx) {
14 tree[cur] = val;
15 } else {
16 // compute value of the midpoint where we cut the segment in half
17 int cur_mid = (cur_left + cur_right) / 2;
18 // remember n * 2 is left child node and n * 2 + 1 is the right child node
19 if (idx <= cur_mid) {
20 update(cur * 2, cur_left, cur_mid, idx, val);
21 } else {
22 update(cur * 2 + 1, cur_mid + 1, cur_right, idx, val);
23 }
24 // after updating the values, compute the new value for the node
25 tree[cur] = tree[cur * 2] + tree[cur * 2 + 1];
26 }
27 }
28
29 int query(int cur, int cur_left, int cur_right, int query_left, int query_right) {
30 // if our current left interval is greater than the queried right interval it means we are out of range
31 // similarly, if the current right interval is less than the queried left interval we are out of range and in both cases return 0
32 if (cur_left > query_right || cur_right < query_left) return 0;
33 // check if we are in range, if we are return the current interval
34 else if (query_left <= cur_left && cur_right <= query_right) {
35 return tree[cur];
36 }
37 // this means part of our interval is in range but part of our interval is not in range, we must therefore query both children
38 int cur_mid = (cur_left + cur_right) / 2;
39 return query(cur * 2, cur_left, cur_mid, query_left, query_right) + query(cur * 2 + 1, cur_mid + 1, cur_right, query_left, query_right);
40 }
41};
42
```

Still not clear? Submit the part you don't understand to our editors.