Skip to content

Instantly share code, notes, and snippets.

@Abby3017
Created December 29, 2019 06:04
Show Gist options
  • Save Abby3017/8e43567a59039846f5a65efec1ca655d to your computer and use it in GitHub Desktop.
Save Abby3017/8e43567a59039846f5a65efec1ca655d to your computer and use it in GitHub Desktop.
Segment Tree implementation that tell if given part of array is sorted or not
class SegmentTreeOrder {
class Node {
int value;
boolean isSorted;
Node() {};
Node(int v, boolean i) {
this.value = v;
this.isSorted = i;
}
public String toString() {
return "value " + this.value + " " + this.isSorted;
}
}
Node st[];
int size;
SegmentTreeOrder(int arr[], int n) {
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
// Maximum size of segment tree
size = 2 * (int) Math.pow(2, x)-1;
st = new Node[size]; // Memory allocation
initiateArr();
constructSTUtil(arr, 0, n - 1, 0);
}
void initiateArr() {
for (int i = 0; i < size; i++) {
st[i] = new Node();
}
}
// A utility function to get the middle index from corner indexes.
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int maxVal(int x, int y) {
return (x > y) ? x : y;
}
Node maxVal(Node x, Node y) {
return (x.value > y.value) ? x: y;
}
Node sortedNode(Node firstPart, Node secondPart) {
Node n;
if (firstPart.isSorted && secondPart.isSorted) {
if (firstPart.value < secondPart.value) {
n = new Node(secondPart.value, true);
} else {
n = new Node(firstPart.value, false);
}
} else {
n = maxVal(firstPart, secondPart);
n = new Node(n.value, false);
}
return n;
}
Node sortedNodeUtil(Node firstPart, Node secondPart) {
Node n;
System.out.println(firstPart);
System.out.println(secondPart);
n = maxVal(firstPart, secondPart);
if (firstPart.isSorted && secondPart.isSorted) {
n = new Node(n.value, true);
} else {
n = new Node(n.value, false);
}
return n;
}
Node constructSTUtil(int[] arr, int ss, int se, int ci) {
if(ss == se) {
Node n = new Node(arr[ss], true);
st[ci] = n;
return n;
}
int mid = getMid(ss, se);
st[ci] = sortedNode(constructSTUtil(arr, ss, mid, ci*2 + 1), constructSTUtil(arr, mid+1, se, ci*2 + 2));
return st[ci];
}
Node SortedQuery(int n, int qs, int qe) {
// Check for erroneous input values
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return new Node();
}
return SortedQueryUtil(0, n - 1, qs, qe, 0);
}
Node SortedQueryUtil(int ss, int se, int qs, int qe, int idx) {
System.out.println("che : " + ss + " " + se + " ");
System.out.println("che1 : " + qs + " " + qe + " " + idx);
if (qs <= ss && qe >= se) {
System.out.println("idx " + idx + st[idx]);
return st[idx];
}
// If segment of this node is outside the given range
if (se < qs || ss > qe) {
System.out.println("check mne");
return new Node(0, true);
}
// If a part of this segment overlaps with the given range
int mid = getMid(ss, se);
System.out.println("mid " + mid);
return sortedNodeUtil(SortedQueryUtil(ss, mid, qs, qe, 2 * idx + 1),
SortedQueryUtil(mid + 1, se, qs, qe, 2 * idx + 2));
}
public static void main(String[] args) {
int arr[] = { 1, 3, 2, 7, 9, 11 };
int n = arr.length;
SegmentTreeOrder tree = new SegmentTreeOrder(arr, n);
for (int i = 0; i < tree.size; i++) {
System.out.println(" che: " + tree.st[i].value + " " + tree.st[i].isSorted);
}
System.out.println("Minimum of values in range [" + 0 + ", "
+ 2 + "] is = " + tree.SortedQuery(n, 1, 3).toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment