首页 > 其他 > 详细

Segment Tree Build I & II

时间:2016-07-09 00:36:30      阅读:252      评论:0      收藏:0      [点我收藏+]

Segment Tree Build I

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:

  • The root‘s start and end is given by build method.
  • The left child of node A has start=A.left, end=(A.left + A.right) / 2.
  • The right child of node A hasstart=(A.left + A.right) / 2 + 1, end=A.right.
  • if start equals to end, there will be no children for this node.

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.

Clarification

Segment Tree (a.k.a Interval Tree) is an advanced data structure which can support queries like:

  • which of these intervals contain a given point
  • which of these points are in a given interval

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 }

Segment Tree Build II

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:

  • The root‘s start and end is given by buildmethod.
  • The left child of node A hasstart=A.left, end=(A.left + A.right) / 2.
  • The right child of node A hasstart=(A.left + A.right) / 2 + 1, end=A.right.
  • if start equals to end, there will be no children for this node.

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.

Clarification

Segment Tree (a.k.a Interval Tree) is an advanced data structure which can support queries like:

  • which of these intervals contain a given point
  • which of these points are in a given interval
Example

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)

分析:
和上题一样,多了一个max而已。
 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 }

 

Segment Tree Build I & II

原文:http://www.cnblogs.com/beiyeqingteng/p/5654984.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!