|
| 1 | +### Segment Trees |
| 2 | + |
| 3 | +- A Segment Tree is a data structure that stores information about array intervals as a tree. |
| 4 | +- This allows answering range queries over an array efficiently, while still being flexible enough to allow quick modification of the array. |
| 5 | + |
| 6 | +> Segment Tree is useful when the array has many "update" operations in intervals. [Read more](https://cp-algorithms.com/data_structures/segment_tree.html) |
| 7 | +
|
| 8 | +Video Tutorial - https://www.youtube.com/watch?v=2bSS8rtFym4&ab_channel=TECHDOSE |
| 9 | + |
| 10 | +#### C++ Code |
| 11 | +<img width="820" src="https://user-images.githubusercontent.com/27401142/182177229-6db97527-7130-403a-ae90-0032b5e4f39c.png"> |
| 12 | + |
| 13 | +```cpp |
| 14 | +void updateValue(vector<int>& st, int start, int end, int updatingIdx, int diff, int idx) { |
| 15 | + // To be updated idx is outside range |
| 16 | + if (updatingIdx < start || updatingIdx > end) |
| 17 | + return; |
| 18 | + |
| 19 | + //Idx is in the range of this node, update the value |
| 20 | + // of this node and its children |
| 21 | + st[idx] += diff; |
| 22 | + if (start != end) { |
| 23 | + int mid = start + (end - start) / 2; |
| 24 | + updateValue(st, start, mid, updatingIdx, diff, 2 * idx + 1); |
| 25 | + updateValue(st, mid + 1, end, updatingIdx, diff, 2 * idx + 2); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +int getSum(vector<int>& st, int start, int end, int searchStartIdx, int searchEndIdx, int idx) { |
| 30 | + // Case 1: Full overlaping condition |
| 31 | + // Segment of the node is part of the range |
| 32 | + // --searchStartIdx-- start ---- end ---searchEndIdx--- |
| 33 | + if (searchStartIdx <= start && searchEndIdx >= end) |
| 34 | + return st[idx]; |
| 35 | + |
| 36 | + // Case 2: NO-overlap condition |
| 37 | + // ---end -- searchStartIdx-- |
| 38 | + // ---searchEndIdx -- start-- |
| 39 | + if (end < searchStartIdx || start > searchEndIdx) |
| 40 | + return 0; |
| 41 | + |
| 42 | + // Case 3: Partial Overlap |
| 43 | + int mid = start + (end - start) / 2; |
| 44 | + return getSum(st, start, mid, searchStartIdx, searchEndIdx, 2 * idx + 1) + |
| 45 | + getSum(st, mid + 1, end, searchStartIdx, searchEndIdx, 2 * idx + 2); |
| 46 | +} |
| 47 | + |
| 48 | +int fillSTValues(vector<int>& nums, vector<int>& st, int start, int end, int idx) { |
| 49 | + // If there is only single element in array, store in st |
| 50 | + // and return it |
| 51 | + if (start == end) { |
| 52 | + st[idx] = nums[start]; |
| 53 | + return nums[start]; |
| 54 | + } |
| 55 | + |
| 56 | + // Split at any pivot into left and right subtrees |
| 57 | + // and store their sum into current node |
| 58 | + int mid = start + (end - start) / 2; |
| 59 | + st[idx] = fillSTValues(nums, st, start, mid, 2 * idx + 1) + |
| 60 | + fillSTValues(nums, st, mid + 1, end, 2 * idx + 2); |
| 61 | + |
| 62 | + return st[idx]; |
| 63 | +} |
| 64 | + |
| 65 | +void buildST(vector<int>& nums, vector<int>& st) { |
| 66 | + //allocate memory for segement tree |
| 67 | + int n = nums.size(); |
| 68 | + int x = (int)(ceil(log2(n))); |
| 69 | + |
| 70 | + //Maximum size of segment tree |
| 71 | + int max_size = 2 * (int)pow(2, x) - 1; |
| 72 | + |
| 73 | + st.resize(max_size); |
| 74 | + fillSTValues(nums, st, 0, n - 1, 0); |
| 75 | +} |
| 76 | + |
| 77 | +int main() { |
| 78 | + vector<int> nums = {1, 3, 5, 7, 9, 11}; |
| 79 | + int n = nums.size(); |
| 80 | + |
| 81 | + //build segment tree |
| 82 | + vector<int> st; |
| 83 | + buildST(nums, st); |
| 84 | + |
| 85 | + //Find sum in range - output = 32 |
| 86 | + int searchStartIdx = 2, searchEndIdx = 5; |
| 87 | + cout << getSum(st, 0, n - 1, searchStartIdx, searchEndIdx, 0) << endl; |
| 88 | + |
| 89 | + //Update value |
| 90 | + int newValue = 7, updatingIdx = 2; |
| 91 | + int diff = newValue - nums[updatingIdx]; |
| 92 | + updateValue(st, 0, n - 1, diff, updatingIdx, 0); |
| 93 | + |
| 94 | + //Find sum range again - output = 34 |
| 95 | + cout << getSum(st, 0, n - 1, searchStartIdx, searchEndIdx, 0) << endl; |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | +``` |
0 commit comments