×

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array.

Native Approach

 It is simple to complete these activities. One may compute the cumulative sum of the elements located between index x and index y by running a for-loop from x to y. When we look at how long it will take to get the total, we obtain O(n) time.

A[j] = p can also be used to update the value at the j-th index, and this operation takes O(1) time. As a result, the total time complexity to do both tasks is O(n) + O(1) = O (n).

Other Approach

The alternative strategy is to first make a prefix array, then finish the aforementioned activities. The sum of the items from index x to index y requires O(1) time once the prefix array has been created. However, it will now take O(n) time to update the jth index. This is due to the fact that updating any index requires updating the prefix array, which takes O(1) time. It follows that the total time complexity is O(1) + O(n) = O(n), which is identical to the naïve method. Since both ways take the same amount of time overall, we must find a way to make it take less time, and the solution is to use a segment tree. The sum from index x to index y is computed in the segment tree in O(log(n)) time. The segment tree requires an O(log(n)) amount of time to update the value at the jth index. O(log(n)) + O(log(n)) = O(2 *(log(n)), which is smaller than O, is the result (n).

Segment Tree Representation

The array's elements are represented by the leaf nodes. By combining their child nodes, the internal nodes of the tree are produced. A node at index j has a left child at 2 * j + 1, a right child at 2 * j + 2, and a parent at (j - 1) / 2 since we are using an array to represent the segment tree.

Pseudo Code for Segment Tree

The difficulty is utilizing the segment tree to compute the total after the segment tree has been built.

int Sum1(node, a, b)   
{  
   if (the range of the node is within a and b)  
{
                  return value of the node  


}
   else if (the range of the node is outside of a and b)  
        return 0  
   else  
    return Sum1(node left child, a, b) +   
           getSum1(node right child, a, b)  
}  

Updation of Value

Recursive updating is used in segment tree operations. Assume that the value of the j-th index is val and that it has to be changed. Add the value val to all the nodes in the segment tree whose range includes the specified range, starting at the root. One should avoid making any modifications to a node if the range of that node does not contain the specified index.

Program for Segment tree using Java

SegmentTree.java

public class SegmentTree
{  
int stArray[];
SegmentTree(int var[], int s1)  
{  
int height = (int) (Math.ceil(Math.log(s1) / Math.log(2)));  
int maximum_size = 2 * (int) Math.pow(2, height) - 1;  
stArray = new int[maximum_size]; constructST(var, 0, s1 - 1, 0);  
}  
int getMidIndex(int f1, int l1)   
{  
return f1 + (l1 - f1) / 2;  
}  
int getSumUtil(int start, int end, int i_r, int j_r, int segment_index)  
{  
if (i_r <= start && j_r >= end)  
{  
return stArray[segment_index];  
}  
if (end < i_r || start > j_r)  
{  
return 0;  
}  
int mid_value = getMidIndex(start, end);  
return getSumUtil(start, mid_value, i_r, j_r, 2 * segment_index + 1) +  
    getSumUtil(mid_value + 1, end, i_r, j_r, 2 * segment_index + 2);  
}  
void updateValUtil(int start, int end, int j_u, int value, int segment_index)  
{  
if (j_u < start || j_u > end)  
{  
return;  
}
stArray[segment_index] = stArray[segment_index] + value;  
if (end != start)   
{  
int mid_value = getMidIndex(start, end);  
updateValUtil(start, mid_value, j_u, value, 2 * segment_index + 1);  
updateValUtil(mid_value + 1, end, j_u, value, 2 * segment_index + 2);  
}  
}   
void updateVal(int N[], int s1, int j_r, int new_value)  
{  
if (j_r < 0 || j_r > s1 - 1)   
{  
System.out.println("Invalid Input");  
return;  
} 
int different_value = new_value - N[j_r];  
stArray[j_r] = new_value; 
updateValUtil(0, s1 - 1, j_r, different_value, 0);  
}  
int getSum(int sum, int start, int end)  
{  
if (start < 0 || end > sum - 1 || start > end)   
{  
System.out.println("Invalid Input");  
return -1;  
}  
return getSumUtil(0, sum - 1, start, end, 0);  
}  
int constructST(int N[], int start, int end, int segment_index)  
{  
if (start == end)   
{  
stArray [segment_index ] = N[start];  
return N[start ] ;  
}   
int middle = getMidIndex ( start , end ) ;  
stArray [ segment_index ] = constructST ( N , start, middle, segment_index * 2 + 1) +  
    constructST ( N , middle + 1 , end , segment_index * 2 + 2 ) ;  
return stArray [ segment_index ] ;  
}  
public static void main(String argvs[])  
{  
int N[] = {22, 44, 77, 110, 2, 1};  
int size_array = N.length;
SegmentTree t = new SegmentTree(N, size_array);  
System.out.println ( " Sum of values within the specified range 1 to 4  = " + t . getSum ( size_array, 1, 4 ) ) ;  
 t. updateVal ( N , size_array , 3 , 11 ) ;  
System . out . println ( " Current sum of the values in the specified range = " + t.getSum ( size_array, 1, 4 ) ) ;  
}  
}  

Output :

Segment Tree in Java

Related Topics

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 minutes read.

Cyclic Barrier in Java

Programmers often find it challenging to run multiple threads simultaneously. Java introduces the idea of concurrency, which enables us to run many threads concurrently, making this work simpler. Concurrent programming...

6 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

C# vs Java

Difference Between C# and Java C# and Java both languagesare popularly used programming languages. They both are derived from C/C++ programming and follow Object Oriented Programming approach. Even so, both these...

4 minutes read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.