Skip to content

Added Important Algos #1322

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

Merged
merged 16 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
437 changes: 437 additions & 0 deletions DataStructures/Graphs/GraphAlgos

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions DataStructures/HashMap/Hashing/Intersection
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Intersection {

public static ArrayList Main(int arr[],int arr2[]) {
HashMap<Integer,Integer> hmap=new HashMap<>();
HashMap<Integer,Integer> hmap2=new HashMap<>();
for(int i=0;i<arr.length;i++) {
if(hmap.containsKey(arr[i])) {
int val=hmap.get(arr[i]);
hmap.put(arr[i],val+1);
}else
hmap.put(arr[i],1);

}
ArrayList<Integer> res=new ArrayList<>();
for(int i=0;i<arr2.length;i++) {
if(hmap.containsKey(arr2[i])&&hmap.get(arr2[i])>0) {
int val=hmap.get(arr2[i]);
hmap.put(arr2[i],val-1);
res.add(arr2[i]);
}

}
return res;
}
public Intersection() {

}



}
63 changes: 0 additions & 63 deletions DataStructures/HashMap/Hashing/LinkedList.java

This file was deleted.

11 changes: 0 additions & 11 deletions DataStructures/HashMap/Hashing/Node.java

This file was deleted.

100 changes: 100 additions & 0 deletions DataStructures/Lists/ListAddnFun
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.util.*;
import java.lang.*;
import java.io.*;
class LinkedList {
private class Node{
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
private int size=0;

public void addLast(int data) {
Node newNode = new Node(data);

if(this.head == null) {
this.head = newNode;
this.tail = newNode;
this.size++;
}
else {
this.tail.next = newNode;
this.tail = newNode;
this.size++;
}
}


public void display() {
Node current = this.head;
if(this.head == null) {
return;
}
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

public void formLL2(LinkedList LL1) {
Node current=LL1.head;
while(current.next!=null&&current.next.next!=null) {
int sum=current.data+current.next.next.data;
this.addLast(sum);
current=current.next.next;
}
}
public void formLL3(LinkedList LL1) {
Node current=LL1.head.next;
while(current.next!=null&&current.next.next!=null) {
int sum=current.data+current.next.next.data;
this.addLast(sum);
current=current.next.next;
}
}
public Node mid() {
Node slow=this.head;
Node fast=this.head;
while(fast.next!=null && fast.next.next!=null) {
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
public Node midValue() {
int sum=this.head.data+this.tail.data;
Node mid=new Node(sum);
return mid;
}
public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) {
Node LL1mid=LL1.mid();
Node currentLL1=LL1.head;
Node currentLL2=LL2.head;
Node currentLL3=LL3.head;
while(currentLL1!=null) {
this.addLast(currentLL1.data);

if(currentLL2!=null) {
this.addLast(currentLL2.data);
currentLL2=currentLL2.next;
}else if(currentLL1.equals(LL1mid)) {
this.addLast(MID.data);
}
else if(currentLL2==null&&currentLL3!=null) {
this.addLast(currentLL3.data);
currentLL3=currentLL3.next;
}
currentLL1=currentLL1.next;
}
}
public void Size() {
System.out.println(this.size);
}
}
106 changes: 106 additions & 0 deletions DataStructures/Trees/AVLSimple
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
public class AVLTree {
private class Node{
int data;
int height;
Node left;
Node right;
Node(int data){
this.data=data;
this.height=1;
}

}
private Node root;
public void insert(int data) {
this.root=insert(this.root,data);
}
private Node insert(Node node,int item) {
if(node==null) {
Node add=new Node(item);
return add;
}
if(node.data>item) {
node.left=insert(node.left,item);
}
if(node.data<item) {
node.right=insert(node.right,item);
}
node.height=Math.max(height(node.left),height(node.right))+1;
int bf=bf(node);
//LL case
if(bf>1&&item<node.left.data)
return rightRotate(node);
//RR case
if(bf<-1&&item>node.right.data)
return leftRotate(node);
//RL case
if(bf<-1&&item<node.right.data) {
node.right=rightRotate(node.right);
return leftRotate(node);
}
//LR case
if(bf>1&&item>node.left.data) {
node.left=leftRotate(node.left);
return rightRotate(node);
}

return node;
}
public void display() {
this.display(this.root);
System.out.println(this.root.height);
}
private void display (Node node) {
String str="";
if(node.left!=null)
str+=node.left.data+"=>";
else
str+="END=>";
str+=node.data+"";
if(node.right!=null)
str+="<="+node.right.data;
else
str+="<=END";
System.out.println(str);
if(node.left!=null)
display(node.left);
if(node.right!=null)
display(node.right);
}
private int height(Node node) {
if(node==null) {
return 0;
}
return node.height;

}
private int bf(Node node) {
if(node==null)
return 0;
return height(node.left)-height(node.right);
}

private Node rightRotate(Node c) {
Node b=c.left;
Node T3=b.right;

b.right=c;
c.left=T3;
c.height=Math.max(height(c.left),height(c.right))+1;
b.height=Math.max(height(b.left),height(b.right))+1;
return b;

}
private Node leftRotate(Node c) {
Node b=c.right;
Node T3=b.left;

b.left=c;
c.right=T3;
c.height=Math.max(height(c.left),height(c.right))+1;
b.height=Math.max(height(b.left),height(b.right))+1;
return b;

}

}
52 changes: 52 additions & 0 deletions DynamicProgramming/BoardPath
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

public class BoardPath {
public static long startTime;
public static long endTime;
public static void startAlgo() {
startTime=System.currentTimeMillis();
}
public static long endAlgo() {
endTime=System.currentTimeMillis();
return endTime-startTime;
}
public static int bpR(int start,int end){
if(start==end) {
return 1;
}
else if(start>end)
return 0;
int count=0;
for(int dice=1;dice<=6;dice++) {
count+=bpR(start+dice,end);
}
return count;
}
public static int bpRS(int curr,int end,int strg[]){
if(curr==end) {
return 1;
}
else if(curr>end)
return 0;
if(strg[curr]!=0)
return strg[curr];
int count=0;
for(int dice=1;dice<=6;dice++) {
count+=bpRS(curr+dice,end,strg);
}
strg[curr]=count;
return count;
}
public static int bpIS(int curr,int end,int[]strg){
strg[end]=1;
for(int i=end-1;i>=0;i--) {
int count=0;
for(int dice=1;dice<=6&&dice+i<strg.length;dice++) {
count+=strg[i+dice];
}
strg[i]=count;
}
return strg[0];
}


}
Loading