Skip to content

Commit

Permalink
SegmentTree basics come true.
Browse files Browse the repository at this point in the history
  • Loading branch information
liusishan committed Feb 26, 2019
1 parent 1f43551 commit bfdebcf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 09-Segment-Tree/01-Segment-Tree-Basics/src/Main.java
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 09-Segment-Tree/01-Segment-Tree-Basics/src/SegmentTree.java
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;
}

}

0 comments on commit bfdebcf

Please sign in to comment.