Skip to content

Commit c77bb2c

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Segment tree + segtree tests
1 parent c762538 commit c77bb2c

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

src/main/java/com/williamfiset/algorithms/datastructures/segmenttree/RangeQueryPointUpdateSegmentTree.java

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
package com.williamfiset.algorithms.datastructures.segmenttree;
1010

1111
public class RangeQueryPointUpdateSegmentTree {
12+
// TODO(william): make the members of this class private
1213

13-
// Tree values.
14-
// TODO(william): make these members private
14+
// Tree segment values.
1515
Integer[] t;
1616

1717
// The number of values in the original input values array.
@@ -33,7 +33,6 @@ public RangeQueryPointUpdateSegmentTree(int[] values) {
3333
t = new Integer[N];
3434

3535
buildTree(0, 0, n - 1, values);
36-
3736
// System.out.println(java.util.Arrays.toString(values));
3837
// System.out.println(java.util.Arrays.toString(t));
3938
}
@@ -116,39 +115,37 @@ private long sumQuery2(int i, int tl, int tr, int l, int r) {
116115
}
117116
}
118117

119-
public void set(int i, int value) {
120-
// update(i, 0, n-1, value);
121-
}
122-
118+
// Updates the segment tree to reflect that index `i` in the original `values` array was updated
119+
// to `newValue`.
123120
public void update(int i, int newValue) {
124121
update(0, i, 0, n - 1, newValue);
125122
}
126123

127124
/**
128-
* Update a point in the segment tree by doing a binary search, updating the leaf node and
129-
* re-computing all the segment values on the callback.
125+
* Update a point segment to a new value and update all affected segments.
126+
*
127+
* <p>Do this by performing a binary search to find the interval containing the point, then update
128+
* the leaf segment with the new value, and re-compute all affected segment values on the
129+
* callback.
130130
*
131131
* @param at the index of the current segment in the tree
132-
* @param to the target position to update left endpoint for the range query
133-
* @param tl the left endpoint that the of the current segment
134-
* @param tr the right endpoint that the of the current segment
135-
* @param r the target right endpoint for the range query
132+
* @param pos the target position to update
133+
* @param tl the left segment endpoint
134+
* @param tr the right segment endpoint
135+
* @param newValue the new value to update
136136
*/
137-
private void update(int at, int to, int tl, int tr, int newValue) {
138-
if (tl > tr) {
139-
return;
140-
}
141-
if (tl == tr) { // or `tl == to && tr == to`
137+
private void update(int at, int pos, int tl, int tr, int newValue) {
138+
if (tl == tr) { // `tl == pos && tr == pos` might be clearer
142139
t[at] = newValue;
143140
return;
144141
}
145142
int tm = (tl + tr) / 2;
146-
// Dig into the left segment
147-
if (to <= tm) {
148-
update(2 * at + 1, to, tl, tm, newValue);
149-
// Dig into the right segment
143+
// The point index `pos` is contained within the left segment [tl, tm]
144+
if (pos <= tm) {
145+
update(2 * at + 1, pos, tl, tm, newValue);
146+
// The point index `pos` is contained within the right segment [tm+1, tr]
150147
} else {
151-
update(2 * at + 2, to, tm + 1, tr, newValue);
148+
update(2 * at + 2, pos, tm + 1, tr, newValue);
152149
}
153150
// Re-compute the segment value of the current segment on the callback
154151
t[at] = t[2 * at + 1] + t[2 * at + 2];
@@ -164,15 +161,5 @@ public static void main(String[] args) {
164161
System.out.println(st.sumQuery(1, 1));
165162
System.out.println(st.sumQuery(0, 1));
166163
System.out.println(st.sumQuery(0, 2));
167-
168-
// for (int i = 1; i < 500; i++) {
169-
// // System.out.println();
170-
// int[] values = new int[i];
171-
// java.util.Arrays.fill(values, 1);
172-
// RangeQueryPointUpdateSegmentTree st = new RangeQueryPointUpdateSegmentTree(values);
173-
// }
174-
// for (int i = 1; i < 20; i++) {
175-
// System.out.printf("%d -> %d\n", i, nextPowerOf2(i));
176-
// }
177164
}
178165
}

0 commit comments

Comments
 (0)