Skip to content

Commit

Permalink
线段树的查询操作
Browse files Browse the repository at this point in the history
  • Loading branch information
Xikl committed Jan 8, 2019
1 parent ac3058f commit 05a6930
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@ private void buildSegmentTree(int treeIndex, int left, int right) {
tree[treeIndex] = merger.apply(tree[leftChildIndex], tree[rightChildIndex]);
}

public E queryRangeClose(int queryLeft, int queryRight) {
// 边界判断
if (queryLeft < 0 || queryRight > getSize() - 1 ||
queryLeft >= getSize() || queryRight >= getSize() || queryLeft > queryRight) {
throw new IllegalArgumentException("illegal argument exception");
}
return queryRangeClose(0, 0, getSize() - 1, queryLeft, queryRight);
}

public E queryRangeClose(int treeIndex, int left, int right, int queryLeft, int queryRight) {
// 边界条件
if (left == queryLeft && right == queryRight) {
return tree[treeIndex];
}

int mid = left + (right - left) / 2;
int leftChildIndex = getLeftChildIndex(treeIndex);
int rightChildIndex = getRightChildIndex(treeIndex);

// 刚好全在 区间的 右边部分
if (queryLeft >= mid + 1) {
return queryRangeClose(rightChildIndex, mid + 1, right, queryLeft, queryRight);
// 刚好在区间的左半部分
} else if (queryRight <= mid) {
return queryRangeClose(leftChildIndex, left, mid, queryLeft, queryRight);
}

// 左右都有 一部分 注意 queryRight 和 queryLeft的值已经变化
E leftResult = queryRangeClose(leftChildIndex, left, mid, queryLeft, mid);
E rightResult = queryRangeClose(rightChildIndex, mid + 1, right, mid + 1, queryRight);
return merger.apply(leftResult, rightResult);
}

public E get(int index) {
if (index < 0 || index > data.length) {
throw new IndexOutOfBoundsException("index-out-of-bounds");
Expand All @@ -71,6 +104,6 @@ public int getRightChildIndex(int index) {

@Override
public String toString() {
return Arrays.stream(tree).map(Object::toString).collect(joining("", "", ""));
return Arrays.stream(tree).map(Object::toString).collect(joining("", "[", "]"));
}
}

0 comments on commit 05a6930

Please sign in to comment.