Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Data Structures/BST/TreeSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

// Java code to illustrate StringBuffer
// class does not implements
// Comparable interface.
import java.util.*;
class TreeSetDemo {

public static void main(String[] args)
{
TreeSet<StringBuffer> ts = new TreeSet<StringBuffer>();

// Elements are added using add() method
ts.add(new StringBuffer("A"));
ts.add(new StringBuffer("Z"));
ts.add(new StringBuffer("L"));
ts.add(new StringBuffer("B"));
ts.add(new StringBuffer("O"));

// We will get RunTimeException :ClassCastException
// As StringBuffer does not implements Comparable interface
System.out.println(ts);
}
}
38 changes: 38 additions & 0 deletions Data Structures/Hash Map/HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;

class Main
{
// This function prints frequencies of all elements
static void printFreq(int arr[])
{
// Creates an empty HashMap
HashMap<Integer, Integer> hmap =
new HashMap<Integer, Integer>();

// Traverse through the given array
for (int i = 0; i < arr.length; i++)
{
Integer c = hmap.get(arr[i]);

// If this is first occurrence of element
if (hmap.get(arr[i]) == null)
hmap.put(arr[i], 1);

// If elements already exists in hash map
else
hmap.put(arr[i], ++c);
}

// Print result
for (Map.Entry m:hmap.entrySet())
System.out.println("Frequency of " + m.getKey() +
" is " + m.getValue());
}

// Driver method to test above method
public static void main (String[] args)
{
int arr[] = {10, 34, 5, 10, 3, 5, 10};
printFreq(arr);
}
}
30 changes: 30 additions & 0 deletions Data Structures/Hash Map/HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.*;

class Test
{
public static void main(String[]args)
{
HashSet<String> h = new HashSet<String>();

// Adding elements into HashSet usind add()
h.add("India");
h.add("Australia");
h.add("South Africa");
h.add("India");// adding duplicate elements

// Displaying the HashSet
System.out.println(h);
System.out.println("List contains India or not:" +
h.contains("India"));

// Removing items from HashSet using remove()
h.remove("Australia");
System.out.println("List after removing Australia:"+h);

// Iterating over hash set items
System.out.println("Iterating over list:");
Iterator<String> i = h.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
30 changes: 30 additions & 0 deletions Data Structures/Hash Map/LinkedHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.*;

public class BasicLinkedHashMap
{
public static void main(String a[])
{
LinkedHashMap<String, String> lhm =
new LinkedHashMap<String, String>();
lhm.put("one", "practice.geeksforgeeks.org");
lhm.put("two", "code.geeksforgeeks.org");
lhm.put("four", "quiz.geeksforgeeks.org");

// It prints the elements in same order
// as they were inserted
System.out.println(lhm);

System.out.println("Getting value for key 'one': "
+ lhm.get("one"));
System.out.println("Size of the map: " + lhm.size());
System.out.println("Is map empty? " + lhm.isEmpty());
System.out.println("Contains key 'two'? "+
lhm.containsKey("two"));
System.out.println("Contains value 'practice.geeks"
+"forgeeks.org'? "+ lhm.containsValue("practice"+
".geeksforgeeks.org"));
System.out.println("delete element 'one': " +
lhm.remove("one"));
System.out.println(lhm);
}
}
30 changes: 30 additions & 0 deletions Data Structures/Hash Map/LinkedHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.LinkedHashSet;
public class Demo
{
public static void main(String[] args)
{
LinkedHashSet<String> linkedset =
new LinkedHashSet<String>();

// Adding element to LinkedHashSet
linkedset.add("A");
linkedset.add("B");
linkedset.add("C");
linkedset.add("D");

// This will not add new element as A already exists
linkedset.add("A");
linkedset.add("E");

System.out.println("Size of LinkedHashSet = " +
linkedset.size());
System.out.println("Original LinkedHashSet:" + linkedset);
System.out.println("Removing D from LinkedHashSet: " +
linkedset.remove("D"));
System.out.println("Trying to Remove Z which is not "+
"present: " + linkedset.remove("Z"));
System.out.println("Checking if A is present=" +
linkedset.contains("A"));
System.out.println("Updated LinkedHashSet: " + linkedset);
}
}
186 changes: 186 additions & 0 deletions Data Structures/Segment tree/C++/TwoDimensionalSegmentTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@

/* C++ program to implement 2D Binary Indexed Tree

2D BIT is basically a BIT where each element is another BIT.
Updating by adding v on (x, y) means it's effect will be found
throughout the rectangle [(x, y), (max_x, max_y)],
and query for (x, y) gives you the result of the rectangle
[(0, 0), (x, y)], assuming the total rectangle is
[(0, 0), (max_x, max_y)]. So when you query and update on
this BIT,you have to be careful about how many times you are
subtracting a rectangle and adding it. Simple set union formula
works here.

So if you want to get the result of a specific rectangle
[(x1, y1), (x2, y2)], the following steps are necessary:

Query(x1,y1,x2,y2) = getSum(x2, y2)-getSum(x2, y1-1) -
getSum(x1-1, y2)+getSum(x1-1, y1-1)

Here 'Query(x1,y1,x2,y2)' means the sum of elements enclosed
in the rectangle with bottom-left corner's co-ordinates
(x1, y1) and top-right corner's co-ordinates - (x2, y2)

Constraints -> x1<=x2 and y1<=y2

/\
y |
| --------(x2,y2)
| | |
| | |
| | |
| ---------
| (x1,y1)
|
|___________________________
(0, 0) x-->

In this progrm we have assumed a square matrix. The
program can be easily extended to a rectangular one. */

#include<bits/stdc++.h>
using namespace std;

#define N 4 // N-->max_x and max_y

// A structure to hold the queries
struct Query
{
int x1, y1; // x and y co-ordinates of bottom left
int x2, y2; // x and y co-ordinates of top right
};

// A function to update the 2D BIT
void updateBIT(int BIT[][N+1], int x, int y, int val)
{
for (; x <= N; x += (x & -x))
{
// This loop update all the 1D BIT inside the
// array of 1D BIT = BIT[x]
for (; y <= N; y += (y & -y))
BIT[x][y] += val;
}
return;
}

// A function to get sum from (0, 0) to (x, y)
int getSum(int BIT[][N+1], int x, int y)
{
int sum = 0;

for(; x > 0; x -= x&-x)
{
// This loop sum through all the 1D BIT
// inside the array of 1D BIT = BIT[x]
for(; y > 0; y -= y&-y)
{
sum += BIT[x][y];
}
}
return sum;
}

// A function to create an auxiliary matrix
// from the given input matrix
void constructAux(int mat[][N], int aux[][N+1])
{
// Initialise Auxiliary array to 0
for (int i=0; i<=N; i++)
for (int j=0; j<=N; j++)
aux[i][j] = 0;

// Construct the Auxiliary Matrix
for (int j=1; j<=N; j++)
for (int i=1; i<=N; i++)
aux[i][j] = mat[N-j][i-1];

return;
}

// A function to construct a 2D BIT
void construct2DBIT(int mat[][N], int BIT[][N+1])
{
// Create an auxiliary matrix
int aux[N+1][N+1];
constructAux(mat, aux);

// Initialise the BIT to 0
for (int i=1; i<=N; i++)
for (int j=1; j<=N; j++)
BIT[i][j] = 0;

for (int j=1; j<=N; j++)
{
for (int i=1; i<=N; i++)
{
// Creating a 2D-BIT using update function
// everytime we/ encounter a value in the
// input 2D-array
int v1 = getSum(BIT, i, j);
int v2 = getSum(BIT, i, j-1);
int v3 = getSum(BIT, i-1, j-1);
int v4 = getSum(BIT, i-1, j);

// Assigning a value to a particular element
// of 2D BIT
updateBIT(BIT, i, j, aux[i][j]-(v1-v2-v4+v3));
}
}

return;
}

// A function to answer the queries
void answerQueries(Query q[], int m, int BIT[][N+1])
{
for (int i=0; i<m; i++)
{
int x1 = q[i].x1 + 1;
int y1 = q[i].y1 + 1;
int x2 = q[i].x2 + 1;
int y2 = q[i].y2 + 1;

int ans = getSum(BIT, x2, y2)-getSum(BIT, x2, y1-1)-
getSum(BIT, x1-1, y2)+getSum(BIT, x1-1, y1-1);

printf ("Query(%d, %d, %d, %d) = %d\n",
q[i].x1, q[i].y1, q[i].x2, q[i].y2, ans);
}
return;
}

// Driver program
int main()
{
int mat[N][N] = {{1, 2, 3, 4},
{5, 3, 8, 1},
{4, 6, 7, 5},
{2, 4, 8, 9}};

// Create a 2D Binary Indexed Tree
int BIT[N+1][N+1];
construct2DBIT(mat, BIT);

/* Queries of the form - x1, y1, x2, y2
For example the query- {1, 1, 3, 2} means the sub-matrix-
y
/\
3 | 1 2 3 4 Sub-matrix
2 | 5 3 8 1 {1,1,3,2} ---> 3 8 1
1 | 4 6 7 5 6 7 5
0 | 2 4 8 9
|
--|------ 0 1 2 3 ----> x
|

Hence sum of the sub-matrix = 3+8+1+6+7+5 = 30

*/

Query q[] = {{1, 1, 3, 2}, {2, 3, 3, 3}, {1, 1, 1, 1}};
int m = sizeof(q)/sizeof(q[0]);

answerQueries(q, m, BIT);

return(0);
}