Skip to content
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
Binary file added .DS_Store
Binary file not shown.
Binary file added Java/.DS_Store
Binary file not shown.
Binary file added Java/algorithms/.DS_Store
Binary file not shown.
183 changes: 183 additions & 0 deletions Java/algorithms/Huffman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;

class HuffmanNode {

int data;
char c;

HuffmanNode left;
HuffmanNode right;
}

class MyComparator implements Comparator<HuffmanNode> {

public int compare(HuffmanNode x, HuffmanNode y) {
return x.data - y.data;
}
}

class HuffmanBuild {

private HashMap<Character, Integer> hash_map;
private HashMap<Character, String> new_hash_map;
private PriorityQueue<HuffmanNode> q;
private String filename;

public HuffmanBuild(String filename) {
hash_map = new HashMap<Character, Integer>();
new_hash_map = new HashMap<Character, String>();
this.filename = filename;
}

private void makeFrequencyMap(Character c) {

if (hash_map.containsKey(c)) {
hash_map.put(c, hash_map.get(c) + 1);
} else {
hash_map.put(c, 1);
}
}

private void makePriorityQueue() {
int size = hash_map.size();
q = new PriorityQueue<HuffmanNode>(size, new MyComparator());

for (Character alphabet : hash_map.keySet()) {
HuffmanNode hn = new HuffmanNode();

hn.c = alphabet;
hn.data = hash_map.get(alphabet);

hn.left = null;
hn.right = null;

q.add(hn);
}

generateHuffmanTree();
}

private void generateHuffmanTree() {

HuffmanNode root = null;

while (q.size() > 1) {

HuffmanNode x = q.peek();
q.poll();

HuffmanNode y = q.peek();
q.poll();

HuffmanNode f = new HuffmanNode();

f.data = x.data + y.data;
f.c = '-';

f.left = x;
f.right = y;

root = f;

q.add(f);
}

generateCharCode(root, "");
printMap();
printCode();

}

private void generateCharCode(HuffmanNode root, String s) {

if (root.left == null && root.right == null) {
// System.out.println(root.c + ":" + s);
new_hash_map.put(root.c, s);
return;
}

generateCharCode(root.left, s + "0");
generateCharCode(root.right, s + "1");
}

private void printCode() {
File file;

try {

file = new File(filename);
BufferedReader br = new BufferedReader(new FileReader(file));

String line;
StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {

for(int i=0; i<line.length(); i++) {
char ch = line.charAt(i);
sb.append(new_hash_map.get(ch));
}

sb.append(new_hash_map.get('\n'));
}

String code = sb.toString();
System.out.println(code);

} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}

public void printMap() {
for (Character entry : new_hash_map.keySet()) {
System.out.print(entry + " ");
System.out.println(new_hash_map.get(entry));
}
}

public void readFile() {
File file;

try {

file = new File(filename);
BufferedReader br = new BufferedReader(new FileReader(file));

String line;

while ((line = br.readLine()) != null) {

for(int i=0; i<line.length(); i++) {
makeFrequencyMap(line.charAt(i));
}

makeFrequencyMap('\n');
}

makePriorityQueue();

} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}
}

class Huffman {

public static void main(String[] args) {
HuffmanBuild hf = new HuffmanBuild(args[0]);
hf.readFile();
}
}
Binary file added Java/data-structures/.DS_Store
Binary file not shown.
153 changes: 153 additions & 0 deletions Java/data-structures/DoubleLinklist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
class Node {
int data;
Node fLink;
Node bLink;

Node(int data) {
this.data = data;
this.fLink = this.bLink = null;
}
}

class DoubleLinklist {
private Node start;
private int length;

DoubleLinklist() {
this.start = null;
this.length = 0;
}

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

if(start == null) {
start = newNode;
} else {
start.bLink = newNode;
newNode.fLink = start;
start = newNode;
}

length++;
}

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

if(start == null) {
start = newNode;
} else {
Node n = start;

while(n.fLink != null) {
n = n.fLink;
}

n.fLink = newNode;
newNode.bLink = n;
}

length++;
}

public void insetPos(int data, int pos) {
if(pos == 1) {
insertBeg(data);
} else if(pos > length ) {
insetEnd(data);
} else {

int i=1;
Node n = start;

while( n.fLink != null ) {
i++;
if( i == pos) break;
n = n.fLink;
}

Node newNode = new Node(data);

newNode.bLink = n;
newNode.fLink = n.fLink;
n.fLink.bLink = newNode;
n.fLink = newNode;

length++;

}
}

public void deleteBeg() {
if(start == null) {
System.out.println("list empty");
} else {
start = n.fLink;
start.bLink = null;
}

length--;
}

public void deleteEnd() {
if(start == null) {
System.out.println("list empty");
} else {
Node n = start;

while(n.fLink.fLink != null) {
n = n.fLink;
}

n.fLink.bLink = null;
n.fLink = null;
length--;
}
}


public void displayForward() {
Node n = start;
while(n.fLink != null) {
System.out.print(n.data + " -> ");
n = n.fLink;
}
System.out.print(n.data + " ");
}

public void displayBackward() {
Node n = start;
while(n.fLink != null) {
n = n.fLink;
}

while(n.bLink != null) {
System.out.print(n.data + " <- ");
n = n.bLink;
}
System.out.print(n.data + " ");
}

public static void main(String[] args) {
doubleLinklist dl = new doubleLinklist();

dl.insertBeg(10);
dl.insertBeg(20);
dl.insertBeg(30);
dl.insetEnd(40);

dl.insetPos(2,4);

dl.displayForward(); // 30 -> 20 -> 10 -> 2 -> 40
System.out.println();

dl.deleteEnd();
dl.deleteBeg();

dl.displayForward(); // 20 -> 10 -> 2
System.out.println();

dl.displayBackward(); // 2 <- 10 <- 20
}
}
Loading