Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segment tree java implementation [closed]

Do you know a good implementation of a (binary) segment tree in Java?

like image 408
talg Avatar asked Apr 25 '09 18:04

talg


People also ask

Is Fenwick tree same as segment tree?

Segment trees can be stored also be stored implicitly (just like a heap), and this will take up 2n space. Fenwick trees are an online data structure, meaning that you can add elements to the end, just like an array, and it will still work. Segment trees do not have this property by default.

Why is segment tree size 4 * n?

Properties of Segment Tree The height of the segment tree is O(log n) because while moving from the root to the leaves at every level, the length of the segments is reduced by half. The total number of nodes involved in the segment tree is 4*n.

What is the time complexity of segment tree?

Time Complexity A Segment tree can contain a maximum of 4*n+1 nodes (1 based indexing). As we visit every node once while building the Segment Tree. Hence the time complexity of the build function is O(n). The time complexity for a range minimum query is also the same as that in a simple segment tree.


2 Answers

public class SegmentTree {
    public static class STNode {
        int leftIndex;
        int rightIndex;
        int sum;
        STNode leftNode;
        STNode rightNode;
    }

    static STNode constructSegmentTree(int[] A, int l, int r) {
        if (l == r) {
            STNode node = new STNode();
            node.leftIndex = l;
            node.rightIndex = r;
            node.sum = A[l];
            return node;
        }
        int mid = (l + r) / 2;
        STNode leftNode = constructSegmentTree(A, l, mid);
        STNode rightNode = constructSegmentTree(A, mid+1, r);
        STNode root = new STNode();
        root.leftIndex = leftNode.leftIndex;
        root.rightIndex = rightNode.rightIndex;
        root.sum = leftNode.sum + rightNode.sum;
        root.leftNode = leftNode;
        root.rightNode = rightNode;
        return root;
    }

    static int getSum(STNode root, int l, int r) {
        if (root.leftIndex >= l && root.rightIndex <= r) {
            return root.sum;
        }
        if (root.rightIndex < l || root.leftIndex > r) {
            return 0;
        }
        return getSum(root.leftNode, l, r) + getSum(root.rightNode, l, r);
    }

    /**
     * 
     * @param root
     * @param index index of number to be updated in original array 
     * @param newValue
     * @return difference between new and old values
     */
    static int updateValueAtIndex(STNode root, int index, int newValue) {
        int diff = 0;
        if(root.leftIndex==root.rightIndex && index == root.leftIndex) {
            // We actually reached to the leaf node to be updated
            diff = newValue-root.sum;
            root.sum=newValue;
            return diff;
        }
        int mid = (root.leftIndex + root.rightIndex) / 2;
        if (index <= mid) {
            diff= updateValueAtIndex(root.leftNode, index, newValue);
        } else {
            diff= updateValueAtIndex(root.rightNode, index, newValue);
        }
        root.sum+=diff;
        return diff;
    }
}
like image 163
Puneet Jaiswal Avatar answered Nov 11 '22 04:11

Puneet Jaiswal


This has been implemented within the open source Layout Management SW Package project

Here is a link to the sub package

You might find the code useful. I have neither verified it nor run it and I cannot find the license the code is provided under from a quick search of the code and website so Caveat Emptor.

You may be able to contact the authors but the last activity appears to have been August 2008.

like image 42
ShuggyCoUk Avatar answered Nov 11 '22 04:11

ShuggyCoUk