Segment Tree | Set 2 (Range Minimum Query) | GeeksforGeeks



We have an array arr[0 . . . n-1]. We should be able to efficiently find the minimum value from index qs (query start) to qe (query end) where 0 <= qs <= qe <= n-1. The array is static (elements are not deleted and inserted during the series of queries).
With segment tree, preprocessing time is O(n) and time to for range minimum query is O(Logn). The extra space required is O(n) to store the segment tree.
Representation of Segment trees
1. Leaf Nodes are the elements of the input array.
2. Each internal node represents minimum of all leaves under it.
An array representation of tree is used to represent Segment Trees. For each node at index i, the left child is at index 2*i+1, right child at 2*i+2 and the parent is at \lfloor (i-1)/2  \rfloor.
public class RMQTree {
    int[] data;
    int[] tree;
     
    int nextPowerOf2( int n ){
        int p = 1;
        for( ; p < n; p <<= 1 );
        return p;
    }
     
    void init( int root, int f, int t ){
        if( f == t ){
            tree[root] = f; 
        }
        else{
            int m = (f+t)/2, l = left(root), r = right(root);
            init( l, f, m );
            init( r, m+1, t );
            tree[root] = ( data[tree[l]] < data[tree[r]] ) ? tree[l] : tree[r];
        }
    }
    RMQTree( int[] a ){
        data = a;
        int n = a.length;
        int p = nextPowerOf2( n );
        tree = new int[2*p-1];
        init( 0, 0, n-1 );
    }
    int parent( int i ){
        return (i-1)/2;
    }
    int left( int i ){
        return 2*i+1;
    }
    int right( int i ){
        return 2*i+2;
    }
 
    int minimum( int root, int low, int high, int f, int t ){
        // return
        //  the index of the minimal value of the array within the range a[f, ..., t],
        //  provided the portion of the array within the range a[low,...,high]
        if( t < low || high < f )
            return -1;
        if( f <= low && high <= t )
            return tree[root];
        int l = left(root);
        int r = right(root);
        int m = (low+high)/2;
        int l_min = minimum(l, low, m, f, t );
        int r_min = minimum(r, m+1, high, f, t );
        if( l_min==-1 )return r_min;
        if( r_min==-1 )return l_min;
        return data[l_min] < data[r_min] ? l_min : r_min;
    }
    int minimum( int f, int t ){
        return minimum( 0, 0, data.length-1, f, t );
    }
}
Read full article from Segment Tree | Set 2 (Range Minimum Query) | GeeksforGeeks

No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts