Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Heaps,Hashing & Tries #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions 18_Heaps,PriorityQueues/Connect_N_Ropes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.PriorityQueue;

public class Connect_N_Ropes {
/* Given are N ropes of diffrent length,the task is to connect these ropes into one rope with
* minimum cost , such that the cost to connect two ropes is equal to the sum of their lengths.
*
* ropes = {4,3,2,6}
* answer - 29
* connect 2 & 3 = 5 ---|
* connect 5 & 4 = 9 ---+= 29 = cost
* connect 9 & 6 = 15 --|
*/
public static void main(String args[]){
int ropes[] = {2,3,3,4,6}; // answer - 41

PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 0; i < ropes.length; i++) {
pq.add(ropes[i]);
}

int cost = 0;
while (pq.size() > 1) {
int min = pq.remove();
int min2 = pq.remove();
cost += min +min2;
pq.add(min+min2);
}
System.out.println("Cost of connecting n ropes = " + cost);
}
}
26 changes: 26 additions & 0 deletions 18_Heaps,PriorityQueues/Heap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Heap {
/*
* 10 1
* / \ / \
* 4 5 2 4
* / \ / \
* 1 2 5 10
* -MaxHeap -MinHeap
*
* Heap --- Visualize --> BinaryTree
* |______Implement --> Array/ArrayList
*
* BinaryTree
* -at most 2 children
*
* Complete Binary Tree
* CBT is a BT in which all the levels are completely filled except possibly the last one ,
* which is filled from the left to right.
*
* Heap Order Property
* Children >= Parent (MinHeap)
* Children <= parent (MaxHeap)
*
* Heap is not implemented as class
*/
}
46 changes: 46 additions & 0 deletions 18_Heaps,PriorityQueues/HeapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public class HeapSort {
public static void heapify(int arr[],int i,int size){
int left = 2*i+1;
int right = 2*i+2;
int maxIdx = i;

if(left < size && arr[maxIdx] < arr[left]){
maxIdx = left;
}
if(right < size && arr[maxIdx] < arr[right]){
maxIdx = right;
}
if(maxIdx != i){
// swap
int temp = arr[i];
arr[i] = arr[maxIdx];
arr[maxIdx] = temp;

heapify(arr,maxIdx,size);
}
}
public static void heapSort(int arr[]){//O(n*logn)
//step 1 - build maxHeap
int n = arr.length;
for (int i = n/2; i >= 0; i--) {
heapify(arr,i,n);
}

//step 2 - push largest at end
for (int i = n-1; i>0 ; i--) {
// swap (largest(first) with last)
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

heapify(arr, 0, i);
}
}
public static void main(String[] args) {
int arr[] = {1,7,4,5};
heapSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
95 changes: 95 additions & 0 deletions 18_Heaps,PriorityQueues/MaxHeap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.util.ArrayList;

public class MaxHeap {
static class Heap{
ArrayList<Integer> arr = new ArrayList<>();

//Insert In Heap
public void add(int data){
// add at last idx
arr.add(data);

int x = arr.size()-1; // x is child index
int par = (x-1)/2; // parent index

while (arr.get(x) > arr.get(par)) { //O(logn)
//swap
int temp = arr.get(x);
arr.set(x, arr.get(par));
arr.set(par, temp);

x = par;
par = (x-1)/2;
}
}

public int peek(){
return arr.get(0);
}

// heapify to fix heap after element deletion
private void heapify(int i){
int left = 2*i+1;
int right = 2*i+2;
int maxIdx = i;

if(left < arr.size() && arr.get(maxIdx) < arr.get(left)){
maxIdx = left;
}
if(right < arr.size() && arr.get(maxIdx) < arr.get(right)){
maxIdx = right;
}
if(maxIdx != i){
// swap
int temp = arr.get(i);
arr.set(i, arr.get(maxIdx));
arr.set(maxIdx, temp);

heapify(maxIdx);
}
}
//Delete in Heap
/* 1) 1st & last node swap
* 2) remove last idx (arr.remove (arr.size()-1))
* 3) fix my Heap --> heapify()
*/
public int remove(){
int data = arr.get(0);

//step 1 - swap first & last
int temp = arr.get(0);
arr.set(0, arr.get(arr.size()-1));
arr.set(arr.size()-1, temp);

//step 2 - delete last
arr.remove(arr.size()-1);

//step 3 - heapify
heapify(0);
return data;
}
public boolean isEmpty(){
return arr.size() == 0;
}
// // To print our heap
// public void print(){
// while(!arr.isEmpty()){
// System.out.println(arr.get(0)/* Or use --> peek()*/);
// arr.remove(0);
// }
// }
// Get Min in Heap

}
public static void main(String[] args) {
Heap pq = new Heap();
pq.add(5);
pq.add(3);
pq.add(7);

while (!pq.isEmpty()) {
System.out.println(pq.peek());
pq.remove();
}
}
}
39 changes: 39 additions & 0 deletions 18_Heaps,PriorityQueues/NearbyCars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.PriorityQueue;

public class NearbyCars {
/* We are given N points in a 2D plane which are locations of N cars. if we are at the
* origin , print the nearest K cars.
* --> C0 (3,3) 3^2 + 3^2 = 18
* --> C1 (5,-1) if k = 2 ans = C0 & C2 5^2 + (-1)^2 = 26
* --> C2 (-2,4) -2^2 + 4^2 = 20
*/
static class Point implements Comparable<Point>{
int x,y,distSq,idx; // Actually here is no need of x & y.
public Point(int x,int y,int distSq,int idx){
this.x = x;
this.y = y;
this.distSq = distSq;
this.idx = idx;
}
@Override
public int compareTo(Point p2){
return this.distSq - p2.distSq;
}
}

public static void main(String[] args) {
int pts[][] = {{3,3},{5,-1},{-2,4}};
int k = 2;

PriorityQueue<Point> pq = new PriorityQueue<>();
for (int i = 0; i < pts.length; i++) {
int distSq = pts[i][0]*pts[i][0] + pts[i][1]*pts[i][1];
pq.add(new Point(pts[i][0], pts[i][1], distSq, i));
}

//nearest k cars
for (int i = 0; i < k; i++) {
System.out.println("C" + pq.remove().idx);
}
}
}
95 changes: 95 additions & 0 deletions 18_Heaps,PriorityQueues/OperationsInHeap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.util.ArrayList;

public class OperationsInHeap {
static class Heap{
ArrayList<Integer> arr = new ArrayList<>();

//Insert In Heap
public void add(int data){
// add at last idx
arr.add(data);

int x = arr.size()-1; // x is child index
int par = (x-1)/2; // parent index

while (arr.get(x) < arr.get(par)) { //O(logn)
//swap
int temp = arr.get(x);
arr.set(x, arr.get(par));
arr.set(par, temp);

x = par;
par = (x-1)/2;
}
}

public int peek(){
return arr.get(0);
}

// heapify to fix heap after element deletion
private void heapify(int i){
int left = 2*i+1;
int right = 2*i+2;
int minIdx = i;

if(left < arr.size() && arr.get(minIdx) > arr.get(left)){
minIdx = left;
}
if(right < arr.size() && arr.get(minIdx) > arr.get(right)){
minIdx = right;
}
if(minIdx != i){
// swap
int temp = arr.get(i);
arr.set(i, arr.get(minIdx));
arr.set(minIdx, temp);

heapify(minIdx);
}
}
//Delete in Heap
/* 1) 1st & last node swap
* 2) remove last idx (arr.remove (arr.size()-1))
* 3) fix my Heap --> heapify()
*/
public int remove(){
int data = arr.get(0);

//step 1 - swap first & last
int temp = arr.get(0);
arr.set(0, arr.get(arr.size()-1));
arr.set(arr.size()-1, temp);

//step 2 - delete last
arr.remove(arr.size()-1);

//step 3 - heapify
heapify(0);
return data;
}
public boolean isEmpty(){
return arr.size() == 0;
}
// // To print our heap
// public void print(){
// while(!arr.isEmpty()){
// System.out.println(arr.get(0)/* Or use --> peek()*/);
// arr.remove(0);
// }
// }
// Get Min in Heap

}
public static void main(String[] args) {
Heap pq = new Heap();
pq.add(5);
pq.add(3);
pq.add(7);

while (!pq.isEmpty()) {
System.out.println(pq.peek());
pq.remove();
}
}
}
29 changes: 29 additions & 0 deletions 18_Heaps,PriorityQueues/PQ_For_Objects.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.PriorityQueue;
public class PQ_For_Objects {
static class Student implements Comparable<Student>{
//Overriding
String name;
int rank;

public Student(String name,int rank){
this.name = name;
this.rank = rank;
}
@Override
public int compareTo(Student s2){
return this.rank - s2.rank; // make it reverse to get decressing order
}
}
public static void main(String[] args) {
PriorityQueue<Student> pq = new PriorityQueue<>();
pq.add(new Student("A",4));
pq.add(new Student("B",5));
pq.add(new Student("C",2));
pq.add(new Student("D",12));

while(!pq.isEmpty()){
System.out.println(pq.peek().name + " --> " + pq.peek().rank);
pq.remove();
}
}
}
4 changes: 4 additions & 0 deletions 18_Heaps,PriorityQueues/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public class Pair {

}
Loading