Segment Tree

Applications:

http://poj.org/summerschool/gw_interval_tree.pdf

  1. find range minimum/maximum

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 has start=(A.left + A.right) / 2 + 1, end=A.right.
  • if start equals to end, there will be no children for this node.

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

1. Build segment tree

/**
 * Definition of SegmentTreeNode:
 * public class SegmentTreeNode {
 *     public int start, end;
 *     public SegmentTreeNode left, right;
 *     public SegmentTreeNode(int start, int end) {
 *         this.start = start, this.end = end;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     *@param start, end: Denote an segment / interval
     *@return: The root of Segment Tree
     */
    public SegmentTreeNode build(int start, int end) {
        // write your code here
        if (start > end) {
            return null;
        }
        if (start == end) {
            return new SegmentTreeNode(start, end);
        }

        int mid = start + (end-start)/2;
        SegmentTreeNode root = new SegmentTreeNode(start, end);
        root.left = build(start, mid);
        root.right = build(mid+1, end);

        return root;
    }
}

2. Build segment tree with max value of the interval

/**
 * Definition of SegmentTreeNode:
 * public class SegmentTreeNode {
 *     public int start, end, max;
 *     public SegmentTreeNode left, right;
 *     public SegmentTreeNode(int start, int end, int max) {
 *         this.start = start;
 *         this.end = end;
 *         this.max = max
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     *@param A: a list of integer
     *@return: The root of Segment Tree
     */
    public SegmentTreeNode build(int[] A) {
        // write your code here
        if (A == null || A.length == 0) {
            return null;
        }
        return build(A, 0, A.length-1);
    }

    public SegmentTreeNode build(int[]A, int start, int end) {
        // write your code here
        if (start > end) {
            return null;
        }
        if (start == end) {
            return new SegmentTreeNode(start, end, A[start]);
        }

        int mid = start + (end-start)/2;
        SegmentTreeNode root = new SegmentTreeNode(start, end, 0);
        root.left = build(A, start, mid);
        root.right = build(A, mid+1, end);
        root.max = Math.max(root.left.max, root.right.max);
        return root;
    }
}

3. Query the max value in a given interval

Runtime 10s

/**
 * Definition of SegmentTreeNode:
 * public class SegmentTreeNode {
 *     public int start, end, max;
 *     public SegmentTreeNode left, right;
 *     public SegmentTreeNode(int start, int end, int max) {
 *         this.start = start;
 *         this.end = end;
 *         this.max = max
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     *@param root, start, end: The root of segment tree and 
     *                         an segment / interval
     *@return: The maximum number in the interval [start, end]
     */
    public int query(SegmentTreeNode root, int start, int end) {
        // write your code here
        if (root == null || start > root.end || end < root.start) {
            return 0;
        }
        if (root.start == root.end) {
            return root.max;
        }
        int mid = ((root.start + root.end) / 2);
        return Math.max(query(root.left, start, end), query(root.right, start, end));
    }
}


Runtime 5s

public class Solution {
    /**
     *@param root, start, end: The root of segment tree and 
     *                         an segment / interval
     *@return: The maximum number in the interval [start, end]
     */
    public int query(SegmentTreeNode root, int start, int end) {
        // write your code here
        if (root == null || start > root.end || end < root.start) {
            return 0;
        }
        if (root.start >= start && root.end <= end) {
            return root.max;
        }
        int mid = ((root.start + root.end) / 2);
        return Math.max(query(root.left, start, Math.min(mid, end)), query(root.right, Math.max(mid, start), end));
    }
}

4. Query the count of numbers in a given interval

/**
 * Definition of SegmentTreeNode:
 * public class SegmentTreeNode {
 *     public int start, end, count;
 *     public SegmentTreeNode left, right;
 *     public SegmentTreeNode(int start, int end, int count) {
 *         this.start = start;
 *         this.end = end;
 *         this.count = count;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     *@param root, start, end: The root of segment tree and 
     *                         an segment / interval
     *@return: The count number in the interval [start, end]
     */
    public int query(SegmentTreeNode root, int start, int end) {
        // write your code here
        if (root == null || start > end) {
            return 0;
        }

        if (start <= root.start && end >= root.end) {
            return root.count;
        }

        int mid = root.start + (root.end-root.start)/2;
        return query(root.left, start, Math.min(mid, end)) + query(root.right, Math.max(mid, start), end);
    }
}