-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @Auther: lss | ||
* @Date: 2019/2/26 20:54 | ||
* @Description: | ||
*/ | ||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
09-Segment-Tree/01-Segment-Tree-Basics/src/SegmentTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @Auther: lss | ||
* @Date: 2019/2/26 20:53 | ||
* @Description: | ||
*/ | ||
public class SegmentTree<E> { | ||
|
||
private E[] tree; | ||
private E[] data; | ||
|
||
public SegmentTree(E[] arr) { | ||
|
||
data = (E[]) new Object[arr.length]; | ||
for (int i = 0; i < arr.length; i++) | ||
data[i] = arr[i]; | ||
|
||
tree = (E[]) new Object[4 * arr.length]; | ||
} | ||
|
||
public int getSize() { | ||
return data.length; | ||
} | ||
|
||
public E get(int index) { | ||
if (index < 0 || index >= data.length) | ||
throw new IllegalArgumentException("Index in illegal"); | ||
return data[index]; | ||
} | ||
|
||
// 返回完全二叉树的数组表示中,一个索引所表示的元素的左孩子节点的索引 | ||
private int leftChild(int index) { | ||
return 2 * index + 1; | ||
} | ||
|
||
// 返回完全二叉树的数组表示中,一个索引所表示的元素的右孩子节点的索引 | ||
private int rightChild(int index) { | ||
return 2 * index + 2; | ||
} | ||
|
||
} |