Skip to content

Commit 26f0608

Browse files
author
dwipam
committed
Compressing
1 parent b9450df commit 26f0608

File tree

10 files changed

+735
-0
lines changed

10 files changed

+735
-0
lines changed

Algorithm/BinarySearchTree.java

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import java.util.Random;
2+
import java.util.Scanner;
3+
4+
public class BinarySearchTree {
5+
public static Node root;
6+
public BinarySearchTree(){
7+
this.root = null;
8+
}
9+
10+
public boolean find(int id){
11+
Node current = root;
12+
while(current!=null){
13+
if(current.data==id){
14+
return true;
15+
}else if(current.data>id){
16+
current = current.left;
17+
}else{
18+
current = current.right;
19+
}
20+
}
21+
return false;
22+
}
23+
public boolean delete(int id){
24+
Node parent = root;
25+
Node current = root;
26+
boolean isLeftChild = false;
27+
while(current.data!=id){
28+
parent = current;
29+
if(current.data>id){
30+
isLeftChild = true;
31+
current = current.left;
32+
}else{
33+
isLeftChild = false;
34+
current = current.right;
35+
}
36+
if(current ==null){
37+
return false;
38+
}
39+
}
40+
//if i am here that means we have found the node
41+
//Case 1: if node to be deleted has no children
42+
if(current.left==null && current.right==null){
43+
if(current==root){
44+
root = null;
45+
}
46+
if(isLeftChild ==true){
47+
parent.left = null;
48+
}else{
49+
parent.right = null;
50+
}
51+
}
52+
53+
else if(current.right==null){
54+
if(current==root){
55+
root = current.left;
56+
}else if(isLeftChild){
57+
parent.left = current.left;
58+
}else{
59+
parent.right = current.left;
60+
}
61+
}
62+
else if(current.left==null){
63+
if(current==root){
64+
root = current.right;
65+
}else if(isLeftChild){
66+
parent.left = current.right;
67+
}else{
68+
parent.right = current.right;
69+
}
70+
}else if(current.left!=null && current.right!=null){
71+
72+
73+
Node successor = getSuccessor(current);
74+
if(current==root){
75+
root = successor;
76+
}else if(isLeftChild){
77+
parent.left = successor;
78+
}else{
79+
parent.right = successor;
80+
}
81+
successor.left = current.left;
82+
}
83+
return true;
84+
}
85+
86+
public Node getSuccessor(Node deleleNode){
87+
Node successsor =null;
88+
Node successsorParent =null;
89+
Node current = deleleNode.right;
90+
while(current!=null){
91+
successsorParent = successsor;
92+
successsor = current;
93+
current = current.left;
94+
}
95+
96+
if(successsor!=deleleNode.right){
97+
successsorParent.left = successsor.right;
98+
successsor.right = deleleNode.right;
99+
}
100+
return successsor;
101+
}
102+
public void insert(int id){
103+
Node newNode = new Node(id);
104+
if(root==null){
105+
root = newNode;
106+
return;
107+
}
108+
Node current = root;
109+
Node parent = null;
110+
while(true){
111+
parent = current;
112+
if(id<current.data){
113+
current = current.left;
114+
if(current==null){
115+
parent.left = newNode;
116+
return;
117+
}
118+
}else{
119+
current = current.right;
120+
if(current==null){
121+
parent.right = newNode;
122+
return;
123+
}
124+
}
125+
}
126+
}
127+
public void disp(Node root){
128+
if(root!=null){
129+
disp(root.left);
130+
System.out.print(" " + root.data);
131+
disp(root.right);
132+
}
133+
}
134+
public static void main(String arg[]){
135+
BinarySearchTree bst = new BinarySearchTree();
136+
Scanner in=new Scanner(System.in);
137+
int[] array=new int[10];
138+
Random randomGenerator = new Random();
139+
for (int i=1;i<10;i++){
140+
array[i]=randomGenerator.nextInt(100);
141+
System.out.print(array[i] +" ");
142+
}
143+
for (int i=1;i<10;i++)
144+
bst.insert(array[i]);
145+
System.out.println("");
146+
System.out.println("Enter the value to search : ");
147+
int fin = in.nextInt();
148+
if(bst.find(fin) == false)
149+
System.out.println("Not Found");
150+
else
151+
System.out.println("Found");
152+
/*System.out.println("Enter the Node to delete : ");
153+
int del = in.nextInt();
154+
bst.delete(del);*/
155+
bst.disp(root);
156+
157+
}
158+
}
159+
160+
class Node{
161+
int data;
162+
Node left;
163+
Node right;
164+
public Node(int data){
165+
this.data = data;
166+
left = null;
167+
right = null;
168+
}
169+
}

Algorithm/HeapSort.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.util.Scanner;
2+
3+
public class HeapSort {
4+
private static int N;
5+
6+
public static void sort(int array[]) {
7+
max_heapify(array);
8+
for (int i = N; i > 0; i--) {
9+
swap(array, 0, i);
10+
N = N - 1;
11+
maxheap(array, 0);
12+
}
13+
}
14+
15+
public static void max_heapify(int array[]) {
16+
N = array.length - 1;
17+
for (int i = N / 2; i > 0; i--) {
18+
19+
// if ((2*i)>N)
20+
// i=0;
21+
maxheap(array, i);
22+
}
23+
}
24+
25+
public static void maxheap(int arr[], int i) {
26+
int left = 2 * i;
27+
int right = 2 * i + 1;
28+
int max = i;
29+
if (left <= N && arr[left] > arr[i])
30+
max = left;
31+
if (right <= N && arr[right] > arr[max])
32+
max = right;
33+
34+
if (max != i) {
35+
swap(arr, i, max);
36+
37+
maxheap(arr, max);
38+
/*
39+
* for (int j=0;j<arr.length;j++){ System.out.print(arr[j]); }
40+
* maxheap(arr, max);
41+
*/
42+
}
43+
for (int j = 0; j < arr.length; j++) {
44+
System.out.print(arr[j] + "\n");
45+
}
46+
}
47+
48+
public static void swap(int array[], int i, int j) {
49+
int temp = array[i];
50+
array[i] = array[j];
51+
array[j] = temp;
52+
System.out.print(" Sorted Element: " + array[j] + " " + array[i] + " ");
53+
54+
}
55+
56+
public static void main(String[] args) {
57+
Scanner scan = new Scanner(System.in);
58+
int n, i;
59+
60+
System.out.println("Enter array size");
61+
n = scan.nextInt();
62+
63+
int array[] = new int[n];
64+
65+
System.out.println("\nEnter numbers");
66+
for (i = 0; i < n; i++)
67+
array[i] = scan.nextInt();
68+
69+
sort(array);
70+
71+
System.out.println("\nElements after sorting ");
72+
for (i = 0; i < n; i++) {
73+
System.out.print(array[i] + " ");
74+
}
75+
76+
}
77+
}

Algorithm/Huffman.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Author: Dwipam Katariya
3+
Email: ddkatari@iu.edu
4+
'''
5+
from heapq import heappush, heappop, heapify
6+
from collections import defaultdict
7+
8+
def encode(symb2freq):
9+
heap = [[wt, [sym, ""]] for sym, wt in symb2freq.items()]
10+
heapify(heap)
11+
print(heap)
12+
13+
14+
while len(heap) > 1:
15+
lo = heappop(heap)
16+
hi = heappop(heap)
17+
for pair in lo[1:]:
18+
pair[1] = '0' + pair[1]
19+
#print(pair[1])
20+
for pair in hi[1:]:
21+
pair[1] = '1' + pair[1]
22+
#print(pair[1])
23+
heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
24+
print(heap)
25+
return sorted(heappop(heap)[1:], key=lambda p: (len(p[-1]), p))
26+
27+
txt = "aaaabbbccd"
28+
symb2freq = defaultdict(int)
29+
30+
for ch in txt:
31+
symb2freq[ch] += 1
32+
33+
print(symb2freq)
34+
# symb2freq = collections.Counter(txt)
35+
huff = encode(symb2freq)
36+
print("Symbol\tWeight\tHuffman Code")
37+
for p in huff:
38+
print("%s\t%s\t%s" % (p[0], symb2freq[p[0]], p[1]))

Algorithm/LCS.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.Scanner;
2+
3+
public class LCS {
4+
5+
public static void main(String[] args) {
6+
Scanner in=new Scanner(System.in);
7+
int i,j;
8+
9+
System.out.print("Enter the first String : ");
10+
String X = in.next();
11+
12+
System.out.print("Enter the Second String : ");
13+
String Y = in.next();
14+
int p = X.length();
15+
int q = Y.length();
16+
17+
int[][] cache = new int[p+1][q+1];
18+
int[][] direction= new int[p+1][q+1];
19+
20+
21+
for (i = 0; i <= p; i++) {
22+
cache[i][0] = 0;
23+
}
24+
25+
26+
for (j = 0; j <= q; j++) {
27+
cache[0][j] = 0;
28+
}
29+
30+
31+
for (i = 1; i <= p; i++) {
32+
for (j = 1; j <= q; j++) {
33+
if (X.charAt(i-1) == Y.charAt(j-1)) {
34+
cache[i][j]=cache[i-1][j-1]+1;
35+
direction[i][j]=1;
36+
}
37+
else if (cache[i-1][j]>=cache[i][j-1]) {
38+
cache[i][j]=cache[i-1][j];
39+
direction[i][j] = 2;
40+
}
41+
else {
42+
cache[i][j]=cache[i][j-1];
43+
direction[i][j]=3;
44+
}
45+
}
46+
}
47+
48+
String lcs = new String();
49+
i=p;
50+
j=q;
51+
while (i!=0 && j!=0) {
52+
if (direction[i][j] ==1) {
53+
lcs =X.charAt(i-1) + lcs;
54+
i = i - 1;
55+
j = j - 1;
56+
}
57+
if (direction[i][j] == 2) {
58+
i = i - 1;
59+
}
60+
if (direction[i][j] == 3) {
61+
j = j - 1;
62+
}
63+
}
64+
65+
66+
System.out.println("The LCS is " + lcs);
67+
68+
}
69+
}

0 commit comments

Comments
 (0)