标签:
The structure of Segment Tree is a binary tree which each node has two attributes start
and end
denote an segment / interval.
start and end are both integers, they should be assigned in following rules:
build
method.start=A.left, end=(A.left + A.right) / 2
.start=(A.left + A.right) / 2 + 1, end=A.right
.Implement a build
method with two parameters start and end, so that we can create a corresponding segment tree with every node has the correct start andend value, return the root of this segment tree.
Segment Tree (a.k.a Interval Tree) is an advanced data structure which can support queries like:
Example
Given start=0, end=3
. The segment tree will be:
[0, 3]
/ [0, 1] [2, 3]
/ \ / [0, 0] [1, 1] [2, 2] [3, 3]
Given start=1, end=6
. The segment tree will be:
[1, 6]
/ [1, 3] [4, 6]
/ \ / [1, 2] [3,3] [4, 5] [6,6]
/ \ / [1,1] [2,2] [4,4] [5,5]
1 /** 2 * Definition of SegmentTreeNode: 3 * public class SegmentTreeNode { 4 * public int start, end; 5 * public SegmentTreeNode left, right; 6 * public SegmentTreeNode(int start, int end) { 7 * this.start = start, this.end = end; 8 * this.left = this.right = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 /** 14 *@param start, end: Denote an segment / interval 15 *@return: The root of Segment Tree 16 */ 17 public SegmentTreeNode build(int start, int end) { 18 if (start > end ) return null; 19 20 if (start == end) { 21 return new SegmentTreeNode(start, start); 22 } else { 23 SegmentTreeNode root = new SegmentTreeNode(start, end); 24 root.left = build(start, (start + end) / 2); 25 root.right = build((start + end) / 2 + 1, end); 26 return root; 27 } 28 } 29 }
The structure of Segment Tree is a binary tree which each node has two attributes start
and end
denote an segment / interval.
start and end are both integers, they should be assigned in following rules:
build
method.start=A.left, end=(A.left + A.right) / 2
.start=(A.left + A.right) / 2 + 1, end=A.right
.Implement a build
method with a given array, so that we can create a corresponding segment tree with every node value represent the corresponding interval max value in the array, return the root of this segment tree.
Segment Tree (a.k.a Interval Tree) is an advanced data structure which can support queries like:
Given [3,2,1,4]
. The segment tree will be:
[0, 3] (max = 4)
/ [0, 1] (max = 3) [2, 3] (max = 4)
/ \ / [0, 0](max = 3) [1, 1](max = 2)[2, 2](max = 1) [3, 3] (max = 4)
1 /** 2 * Definition of SegmentTreeNode: 3 * public class SegmentTreeNode { 4 * public int start, end, max; 5 * public SegmentTreeNode left, right; 6 * public SegmentTreeNode(int start, int end, int max) { 7 * this.start = start; 8 * this.end = end; 9 * this.max = max 10 * this.left = this.right = null; 11 * } 12 * } 13 */ 14 public class Solution { 15 /** 16 *@param A: a list of integer 17 *@return: The root of Segment Tree 18 */ 19 public SegmentTreeNode build(int[] A) { 20 if (A == null || A.length == 0) return null; 21 22 return build(A, 0, A.length - 1); 23 } 24 25 public SegmentTreeNode build(int[] A, int start, int end) { 26 if (start > end) return null; 27 28 if (start == end) { 29 return new SegmentTreeNode(start, end, A[start]); 30 } else { 31 SegmentTreeNode root = new SegmentTreeNode(start, end, max(A, start, end)); 32 root.left = build(A, start, (start + end) / 2); 33 root.right = build(A, (start + end) / 2 + 1, end); 34 return root; 35 } 36 } 37 38 private int max(int[] A, int start, int end) { 39 int max = A[start]; 40 41 for (int i = start + 1; i <= end; i++) { 42 max = Math.max(max, A[i]); 43 } 44 return max; 45 } 46 }
标签:
原文地址:http://www.cnblogs.com/beiyeqingteng/p/5654984.html