|
| 1 | +package dataStructures.segmentTree.arrayRepresentation; |
| 2 | + |
| 3 | +/** |
| 4 | + * Array-based implementation of a Segment Tree. |
| 5 | + */ |
| 6 | +public class SegmentTree { |
| 7 | + private int[] tree; |
| 8 | + private int[] array; |
| 9 | + |
| 10 | + /** |
| 11 | + * Constructor. |
| 12 | + * @param nums |
| 13 | + */ |
| 14 | + public SegmentTree(int[] nums) { |
| 15 | + tree = new int[4 * nums.length]; // Need to account for up to 4n nodes. |
| 16 | + array = nums; |
| 17 | + buildTree(nums, 0, nums.length - 1, 0); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Builds the tree from the given array of numbers. |
| 22 | + * Unlikely before where we capture child nodes in the helper node class, here we capture position of child nodes |
| 23 | + * in the array-representation of the tree with an additional variable. |
| 24 | + * @param nums |
| 25 | + * @param start |
| 26 | + * @param end |
| 27 | + * @param idx tells us which index of the tree array we are at. |
| 28 | + */ |
| 29 | + private void buildTree(int[] nums, int start, int end, int idx) { |
| 30 | + // recall, each node is a position in the array |
| 31 | + // explicitly track which position in the array to fill with idx variable |
| 32 | + if (start == end) { |
| 33 | + tree[idx] = nums[start]; |
| 34 | + return; |
| 35 | + } |
| 36 | + int mid = start + (end - start) / 2; |
| 37 | + int idxLeftChild = (idx + 1) * 2 - 1; // convert from 0-based to 1-based, do computation, then revert |
| 38 | + buildTree(nums, start, mid, idxLeftChild); |
| 39 | + int idxRightChild = (idx + 1) * 2 + 1 - 1; // convert from 0-based to 1-based, do computation, then revert |
| 40 | + buildTree(nums, mid + 1, end, idxRightChild); |
| 41 | + tree[idx] = tree[idxLeftChild] + tree[idxRightChild]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Queries the sum of all values in the specified range. |
| 46 | + * @param leftEnd |
| 47 | + * @param rightEnd |
| 48 | + * @return the sum. |
| 49 | + */ |
| 50 | + public int query(int leftEnd, int rightEnd) { |
| 51 | + return query(0, 0, array.length - 1, leftEnd, rightEnd); |
| 52 | + } |
| 53 | + |
| 54 | + private int query(int nodeIdx, int startRange, int endRange, int leftEnd, int rightEnd) { |
| 55 | + // this is the case when: |
| 56 | + // start end |
| 57 | + // range query: ^ ^ --> so simply capture the sum at this node! |
| 58 | + if (leftEnd <= startRange && endRange <= rightEnd) { |
| 59 | + return tree[nodeIdx]; |
| 60 | + } |
| 61 | + int rangeSum = 0; |
| 62 | + int mid = startRange + (endRange - startRange) / 2; |
| 63 | + // Consider the 3 possible kinds of range queries |
| 64 | + // start mid end |
| 65 | + // poss 1: ^ ^ |
| 66 | + // poss 2: ^ ^ |
| 67 | + // poss 3: ^ ^ |
| 68 | + if (leftEnd <= mid) { |
| 69 | + int idxLeftChild = (nodeIdx + 1) * 2 - 1; |
| 70 | + rangeSum += query(idxLeftChild, startRange, mid, leftEnd, Math.min(rightEnd, mid)); |
| 71 | + } |
| 72 | + if (mid + 1 <= rightEnd) { |
| 73 | + int idxRightChild = (nodeIdx + 1) * 2 + 1 - 1; |
| 74 | + rangeSum += query(idxRightChild, mid + 1, endRange, Math.max(leftEnd, mid + 1), rightEnd); |
| 75 | + } |
| 76 | + return rangeSum; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Updates the segment tree based on updates to the array at the specified index with the specified value. |
| 81 | + * @param idx |
| 82 | + * @param val |
| 83 | + */ |
| 84 | + public void update(int idx, int val) { |
| 85 | + if (idx > array.length) { |
| 86 | + return; |
| 87 | + } |
| 88 | + array[idx] = val; |
| 89 | + update(0, 0, array.length - 1, idx, val); |
| 90 | + } |
| 91 | + |
| 92 | + private void update(int nodeIdx, int startRange, int endRange, int idx, int val) { |
| 93 | + if (startRange == endRange) { |
| 94 | + tree[nodeIdx] = val; |
| 95 | + return; |
| 96 | + } |
| 97 | + int mid = startRange + (endRange - startRange) / 2; |
| 98 | + if (idx <= mid) { |
| 99 | + update(nodeIdx * 2 + 1, startRange, mid, idx, val); |
| 100 | + } else { |
| 101 | + update(nodeIdx * 2 + 2, mid + 1, endRange, idx, val); |
| 102 | + } |
| 103 | + tree[nodeIdx] = tree[nodeIdx * 2 + 1] + tree[nodeIdx * 2 + 2]; |
| 104 | + } |
| 105 | +} |
0 commit comments