Improved files and folders name conventions and moved lost files to Misc folder
This commit is contained in:
parent
2128c7a15d
commit
467b917416
@ -1,141 +1,141 @@
|
|||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class HashMap<K,V> {
|
public class HashMap<K,V> {
|
||||||
public class hmnodes{ //HashMap nodes
|
public class hmnodes{ //HashMap nodes
|
||||||
K key;
|
K key;
|
||||||
V value;
|
V value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int size=0; //size of hashmap
|
private int size=0; //size of hashmap
|
||||||
private LinkedList<hmnodes> buckets[]; //array of addresses of list
|
private LinkedList<hmnodes> buckets[]; //array of addresses of list
|
||||||
|
|
||||||
public HashMap(){
|
public HashMap(){
|
||||||
buckets=new LinkedList[4]; //initially create bucket of any size
|
buckets=new LinkedList[4]; //initially create bucket of any size
|
||||||
for(int i=0;i<4;i++)
|
for(int i=0;i<4;i++)
|
||||||
buckets[i]=new LinkedList<>();
|
buckets[i]=new LinkedList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(K key,V value) throws Exception{
|
public void put(K key,V value) throws Exception{
|
||||||
int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index
|
int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index
|
||||||
int fountAt=find(bi,key); //check if key already exists or not
|
int fountAt=find(bi,key); //check if key already exists or not
|
||||||
if(fountAt==-1){
|
if(fountAt==-1){
|
||||||
hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert
|
hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert
|
||||||
temp.key=key;
|
temp.key=key;
|
||||||
temp.value=value;
|
temp.value=value;
|
||||||
buckets[bi].addLast(temp);
|
buckets[bi].addLast(temp);
|
||||||
this.size++;
|
this.size++;
|
||||||
}else{
|
}else{
|
||||||
buckets[bi].get(fountAt).value=value;//if already exist modify the value
|
buckets[bi].get(fountAt).value=value;//if already exist modify the value
|
||||||
}
|
}
|
||||||
|
|
||||||
double lambda = (this.size*1.0)/this.buckets.length;
|
double lambda = (this.size*1.0)/this.buckets.length;
|
||||||
if(lambda>2.0){
|
if(lambda>2.0){
|
||||||
rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0
|
rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public V get(K key) throws Exception{
|
public V get(K key) throws Exception{
|
||||||
int bi=bucketIndex(key);
|
int bi=bucketIndex(key);
|
||||||
int fountAt=find(bi,key);
|
int fountAt=find(bi,key);
|
||||||
if(fountAt==-1){
|
if(fountAt==-1){
|
||||||
return null;
|
return null;
|
||||||
}else{
|
}else{
|
||||||
return buckets[bi].get(fountAt).value;
|
return buckets[bi].get(fountAt).value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public V remove(K key) throws Exception{
|
public V remove(K key) throws Exception{
|
||||||
int bi=bucketIndex(key);
|
int bi=bucketIndex(key);
|
||||||
int fountAt=find(bi,key);
|
int fountAt=find(bi,key);
|
||||||
if(fountAt==-1){
|
if(fountAt==-1){
|
||||||
return null;
|
return null;
|
||||||
}else{
|
}else{
|
||||||
this.size--;
|
this.size--;
|
||||||
return buckets[bi].remove(fountAt).value;
|
return buckets[bi].remove(fountAt).value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containskey(K key) throws Exception{
|
public boolean containskey(K key) throws Exception{
|
||||||
int bi=bucketIndex(key);
|
int bi=bucketIndex(key);
|
||||||
int fountAt=find(bi,key);
|
int fountAt=find(bi,key);
|
||||||
if(fountAt==-1){
|
if(fountAt==-1){
|
||||||
return false;
|
return false;
|
||||||
}else{
|
}else{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size(){
|
public int size(){
|
||||||
return this.size;
|
return this.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isempty(){
|
public boolean isempty(){
|
||||||
return this.size==0;
|
return this.size==0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<K> keyset() throws Exception{
|
public ArrayList<K> keyset() throws Exception{
|
||||||
ArrayList<K> arr=new ArrayList<>();
|
ArrayList<K> arr=new ArrayList<>();
|
||||||
for(int i=0;i<buckets.length;i++){
|
for(int i=0;i<buckets.length;i++){
|
||||||
for(int j=0;j<buckets[i].size();j++){
|
for(int j=0;j<buckets[i].size();j++){
|
||||||
arr.add(buckets[i].get(j).key);
|
arr.add(buckets[i].get(j).key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<V> valueset() throws Exception{
|
public ArrayList<V> valueset() throws Exception{
|
||||||
ArrayList<V> arr=new ArrayList<>();
|
ArrayList<V> arr=new ArrayList<>();
|
||||||
for(int i=0;i<buckets.length;i++){
|
for(int i=0;i<buckets.length;i++){
|
||||||
for(int j=0;j<buckets[i].size();j++){
|
for(int j=0;j<buckets[i].size();j++){
|
||||||
arr.add(buckets[i].get(j).value);
|
arr.add(buckets[i].get(j).value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void display() throws Exception{
|
public void display() throws Exception{
|
||||||
for(int i=0;i<buckets.length;i++){
|
for(int i=0;i<buckets.length;i++){
|
||||||
System.out.print("Bucket: "+i+" ");
|
System.out.print("Bucket: "+i+" ");
|
||||||
for(int j=0;j<buckets[i].size();j++){
|
for(int j=0;j<buckets[i].size();j++){
|
||||||
hmnodes temp=buckets[i].get(j);
|
hmnodes temp=buckets[i].get(j);
|
||||||
System.out.print("["+temp.key+"->"+temp.value+"]");
|
System.out.print("["+temp.key+"->"+temp.value+"]");
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int find(int bi,K key) throws Exception{
|
public int find(int bi,K key) throws Exception{
|
||||||
for(int i=0;i<buckets[bi].size();i++){
|
for(int i=0;i<buckets[bi].size();i++){
|
||||||
if(key.equals(buckets[bi].get(i).key))
|
if(key.equals(buckets[bi].get(i).key))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int bucketIndex(K key) throws Exception{
|
public int bucketIndex(K key) throws Exception{
|
||||||
int bi=key.hashCode();
|
int bi=key.hashCode();
|
||||||
return Math.abs(bi%buckets.length);
|
return Math.abs(bi%buckets.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rehash() throws Exception{
|
private void rehash() throws Exception{
|
||||||
LinkedList<hmnodes> ob[]= buckets;
|
LinkedList<hmnodes> ob[]= buckets;
|
||||||
buckets=new LinkedList[ob.length*2];
|
buckets=new LinkedList[ob.length*2];
|
||||||
for(int i=0;i<ob.length*2;i++)
|
for(int i=0;i<ob.length*2;i++)
|
||||||
buckets[i]=new LinkedList<>();
|
buckets[i]=new LinkedList<>();
|
||||||
|
|
||||||
size = 0;
|
size = 0;
|
||||||
for(int i=0;i<ob.length;i++){
|
for(int i=0;i<ob.length;i++){
|
||||||
for(int j=0;j<ob[i].size();j++){
|
for(int j=0;j<ob[i].size();j++){
|
||||||
put(ob[i].get(j).key,ob[i].get(j).value);
|
put(ob[i].get(j).key,ob[i].get(j).value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,226 +1,226 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class treeclass {
|
public class treeclass {
|
||||||
private class Node {
|
private class Node {
|
||||||
int data;
|
int data;
|
||||||
ArrayList<Node> child = new ArrayList<>();
|
ArrayList<Node> child = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node root;
|
private Node root;
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
A generic tree is a tree which can have as many children as it can be
|
A generic tree is a tree which can have as many children as it can be
|
||||||
It might be possible that every node present is directly connected to
|
It might be possible that every node present is directly connected to
|
||||||
root node.
|
root node.
|
||||||
|
|
||||||
In this code
|
In this code
|
||||||
Every function has two copies: one function is helper function which can be called from
|
Every function has two copies: one function is helper function which can be called from
|
||||||
main and from that function a private function is called which will do the actual work.
|
main and from that function a private function is called which will do the actual work.
|
||||||
I have done this, while calling from main one have to give minimum parameters.
|
I have done this, while calling from main one have to give minimum parameters.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public treeclass() { //Constructor
|
public treeclass() { //Constructor
|
||||||
Scanner scn = new Scanner(System.in);
|
Scanner scn = new Scanner(System.in);
|
||||||
root = create_treeG(null, 0, scn);
|
root = create_treeG(null, 0, scn);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node create_treeG(Node node, int childindx, Scanner scn) {
|
private Node create_treeG(Node node, int childindx, Scanner scn) {
|
||||||
// display
|
// display
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
System.out.println("Enter root's data");
|
System.out.println("Enter root's data");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Enter data of parent of index " + node.data + " " + childindx);
|
System.out.println("Enter data of parent of index " + node.data + " " + childindx);
|
||||||
}
|
}
|
||||||
// input
|
// input
|
||||||
node = new Node();
|
node = new Node();
|
||||||
node.data = scn.nextInt();
|
node.data = scn.nextInt();
|
||||||
System.out.println("number of children");
|
System.out.println("number of children");
|
||||||
int number = scn.nextInt();
|
int number = scn.nextInt();
|
||||||
for (int i = 0; i < number; i++) {
|
for (int i = 0; i < number; i++) {
|
||||||
Node childd = create_treeG(node, i, scn);
|
Node childd = create_treeG(node, i, scn);
|
||||||
size++;
|
size++;
|
||||||
node.child.add(childd);
|
node.child.add(childd);
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to display the generic tree
|
Function to display the generic tree
|
||||||
*/
|
*/
|
||||||
public void display() { //Helper function
|
public void display() { //Helper function
|
||||||
display_1(root);
|
display_1(root);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void display_1(Node parent) {
|
private void display_1(Node parent) {
|
||||||
System.out.print(parent.data + "=>");
|
System.out.print(parent.data + "=>");
|
||||||
for (int i = 0; i < parent.child.size(); i++) {
|
for (int i = 0; i < parent.child.size(); i++) {
|
||||||
System.out.print(parent.child.get(i).data + " ");
|
System.out.print(parent.child.get(i).data + " ");
|
||||||
}
|
}
|
||||||
System.out.println(".");
|
System.out.println(".");
|
||||||
for (int i = 0; i < parent.child.size(); i++) {
|
for (int i = 0; i < parent.child.size(); i++) {
|
||||||
display_1(parent.child.get(i));
|
display_1(parent.child.get(i));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
One call store the size directly but if you are asked compute size this function to calcuate
|
One call store the size directly but if you are asked compute size this function to calcuate
|
||||||
size goes as follows
|
size goes as follows
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public int size2call() {
|
public int size2call() {
|
||||||
return size2(root);
|
return size2(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size2(Node roott) {
|
public int size2(Node roott) {
|
||||||
int sz = 0;
|
int sz = 0;
|
||||||
for (int i = 0; i < roott.child.size(); i++) {
|
for (int i = 0; i < roott.child.size(); i++) {
|
||||||
sz += size2(roott.child.get(i));
|
sz += size2(roott.child.get(i));
|
||||||
}
|
}
|
||||||
return sz + 1;
|
return sz + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to compute maximum value in the generic tree
|
Function to compute maximum value in the generic tree
|
||||||
*/
|
*/
|
||||||
public int maxcall() {
|
public int maxcall() {
|
||||||
int maxi = root.data;
|
int maxi = root.data;
|
||||||
return max(root, maxi);
|
return max(root, maxi);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int max(Node roott, int maxi) {
|
private int max(Node roott, int maxi) {
|
||||||
if (maxi < roott.data)
|
if (maxi < roott.data)
|
||||||
maxi = roott.data;
|
maxi = roott.data;
|
||||||
for (int i = 0; i < roott.child.size(); i++) {
|
for (int i = 0; i < roott.child.size(); i++) {
|
||||||
maxi = max(roott.child.get(i), maxi);
|
maxi = max(roott.child.get(i), maxi);
|
||||||
}
|
}
|
||||||
|
|
||||||
return maxi;
|
return maxi;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to compute HEIGHT of the generic tree
|
Function to compute HEIGHT of the generic tree
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public int heightcall() {
|
public int heightcall() {
|
||||||
return height(root) - 1;
|
return height(root) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int height(Node node) {
|
private int height(Node node) {
|
||||||
int h = 0;
|
int h = 0;
|
||||||
for (int i = 0; i < node.child.size(); i++) {
|
for (int i = 0; i < node.child.size(); i++) {
|
||||||
int k = height(node.child.get(i));
|
int k = height(node.child.get(i));
|
||||||
if (k > h)
|
if (k > h)
|
||||||
h = k;
|
h = k;
|
||||||
}
|
}
|
||||||
return h + 1;
|
return h + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to find whether a number is present in the generic tree or not
|
Function to find whether a number is present in the generic tree or not
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public boolean findcall(int info) {
|
public boolean findcall(int info) {
|
||||||
return find(root, info);
|
return find(root, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean find(Node node, int info) {
|
private boolean find(Node node, int info) {
|
||||||
if (node.data == info)
|
if (node.data == info)
|
||||||
return true;
|
return true;
|
||||||
for (int i = 0; i < node.child.size(); i++) {
|
for (int i = 0; i < node.child.size(); i++) {
|
||||||
if (find(node.child.get(i), info))
|
if (find(node.child.get(i), info))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to calculate depth of generic tree
|
Function to calculate depth of generic tree
|
||||||
*/
|
*/
|
||||||
public void depthcaller(int dep) {
|
public void depthcaller(int dep) {
|
||||||
depth(root, dep);
|
depth(root, dep);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void depth(Node node, int dep) {
|
public void depth(Node node, int dep) {
|
||||||
if (dep == 0) {
|
if (dep == 0) {
|
||||||
System.out.println(node.data);
|
System.out.println(node.data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < node.child.size(); i++)
|
for (int i = 0; i < node.child.size(); i++)
|
||||||
depth(node.child.get(i), dep - 1);
|
depth(node.child.get(i), dep - 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to print generic tree in pre-order
|
Function to print generic tree in pre-order
|
||||||
*/
|
*/
|
||||||
public void preordercall() {
|
public void preordercall() {
|
||||||
preorder(root);
|
preorder(root);
|
||||||
System.out.println(".");
|
System.out.println(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void preorder(Node node) {
|
private void preorder(Node node) {
|
||||||
System.out.print(node.data + " ");
|
System.out.print(node.data + " ");
|
||||||
for (int i = 0; i < node.child.size(); i++)
|
for (int i = 0; i < node.child.size(); i++)
|
||||||
preorder(node.child.get(i));
|
preorder(node.child.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to print generic tree in post-order
|
Function to print generic tree in post-order
|
||||||
*/
|
*/
|
||||||
public void postordercall() {
|
public void postordercall() {
|
||||||
postorder(root);
|
postorder(root);
|
||||||
System.out.println(".");
|
System.out.println(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postorder(Node node) {
|
private void postorder(Node node) {
|
||||||
for (int i = 0; i < node.child.size(); i++)
|
for (int i = 0; i < node.child.size(); i++)
|
||||||
postorder(node.child.get(i));
|
postorder(node.child.get(i));
|
||||||
System.out.print(node.data + " ");
|
System.out.print(node.data + " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to print generic tree in level-order
|
Function to print generic tree in level-order
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void levelorder() {
|
public void levelorder() {
|
||||||
LinkedList<Node> q = new LinkedList<>();
|
LinkedList<Node> q = new LinkedList<>();
|
||||||
q.addLast(root);
|
q.addLast(root);
|
||||||
while (!q.isEmpty()) {
|
while (!q.isEmpty()) {
|
||||||
int k = q.getFirst().data;
|
int k = q.getFirst().data;
|
||||||
System.out.print(k + " ");
|
System.out.print(k + " ");
|
||||||
|
|
||||||
for (int i = 0; i < q.getFirst().child.size(); i++) {
|
for (int i = 0; i < q.getFirst().child.size(); i++) {
|
||||||
q.addLast(q.getFirst().child.get(i));
|
q.addLast(q.getFirst().child.get(i));
|
||||||
}
|
}
|
||||||
q.removeFirst();
|
q.removeFirst();
|
||||||
}
|
}
|
||||||
System.out.println(".");
|
System.out.println(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function to remove all leaves of generic tree
|
Function to remove all leaves of generic tree
|
||||||
*/
|
*/
|
||||||
public void removeleavescall() {
|
public void removeleavescall() {
|
||||||
removeleaves(root);
|
removeleaves(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeleaves(Node node) {
|
private void removeleaves(Node node) {
|
||||||
ArrayList<Integer> arr = new ArrayList<>();
|
ArrayList<Integer> arr = new ArrayList<>();
|
||||||
for (int i = 0; i < node.child.size(); i++) {
|
for (int i = 0; i < node.child.size(); i++) {
|
||||||
if (node.child.get(i).child.size() == 0) {
|
if (node.child.get(i).child.size() == 0) {
|
||||||
arr.add(i);
|
arr.add(i);
|
||||||
// node.child.remove(i);
|
// node.child.remove(i);
|
||||||
// i--;
|
// i--;
|
||||||
} else
|
} else
|
||||||
removeleaves(node.child.get(i));
|
removeleaves(node.child.get(i));
|
||||||
}
|
}
|
||||||
for (int i = arr.size() - 1; i >= 0; i--) {
|
for (int i = arr.size() - 1; i >= 0; i--) {
|
||||||
node.child.remove(arr.get(i) + 0);
|
node.child.remove(arr.get(i) + 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,158 +1,158 @@
|
|||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Mayank Kumar (mk9440)
|
* @author Mayank Kumar (mk9440)
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
Output :
|
Output :
|
||||||
|
|
||||||
Enter number of distinct letters
|
Enter number of distinct letters
|
||||||
6
|
6
|
||||||
Enter letters with its frequncy to encode
|
Enter letters with its frequncy to encode
|
||||||
Enter letter : a
|
Enter letter : a
|
||||||
Enter frequncy : 45
|
Enter frequncy : 45
|
||||||
|
|
||||||
Enter letter : b
|
Enter letter : b
|
||||||
Enter frequncy : 13
|
Enter frequncy : 13
|
||||||
|
|
||||||
Enter letter : c
|
Enter letter : c
|
||||||
Enter frequncy : 12
|
Enter frequncy : 12
|
||||||
|
|
||||||
Enter letter : d
|
Enter letter : d
|
||||||
Enter frequncy : 16
|
Enter frequncy : 16
|
||||||
|
|
||||||
Enter letter : e
|
Enter letter : e
|
||||||
Enter frequncy : 9
|
Enter frequncy : 9
|
||||||
|
|
||||||
Enter letter : f
|
Enter letter : f
|
||||||
Enter frequncy : 5
|
Enter frequncy : 5
|
||||||
|
|
||||||
Letter Encoded Form
|
Letter Encoded Form
|
||||||
a 0
|
a 0
|
||||||
b 1 0 1
|
b 1 0 1
|
||||||
c 1 0 0
|
c 1 0 0
|
||||||
d 1 1 1
|
d 1 1 1
|
||||||
e 1 1 0 1
|
e 1 1 0 1
|
||||||
f 1 1 0 0
|
f 1 1 0 0
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Node{
|
class Node{
|
||||||
String letr="";
|
String letr="";
|
||||||
int freq=0,data=0;
|
int freq=0,data=0;
|
||||||
Node left=null,right=null;
|
Node left=null,right=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//A comparator class to sort list on the basis of their frequency
|
//A comparator class to sort list on the basis of their frequency
|
||||||
class comp implements Comparator<Node>{
|
class comp implements Comparator<Node>{
|
||||||
@Override
|
@Override
|
||||||
public int compare(Node o1, Node o2) {
|
public int compare(Node o1, Node o2) {
|
||||||
if(o1.freq>o2.freq){return 1;}
|
if(o1.freq>o2.freq){return 1;}
|
||||||
else if(o1.freq<o2.freq){return -1;}
|
else if(o1.freq<o2.freq){return -1;}
|
||||||
else{return 0;}
|
else{return 0;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class Huffman {
|
public class Huffman {
|
||||||
|
|
||||||
// A simple function to print a given list
|
// A simple function to print a given list
|
||||||
//I just made it for debugging
|
//I just made it for debugging
|
||||||
public static void print_list(List li){
|
public static void print_list(List li){
|
||||||
Iterator<Node> it=li.iterator();
|
Iterator<Node> it=li.iterator();
|
||||||
while(it.hasNext()){Node n=it.next();System.out.print(n.freq+" ");}System.out.println();
|
while(it.hasNext()){Node n=it.next();System.out.print(n.freq+" ");}System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Function for making tree (Huffman Tree)
|
//Function for making tree (Huffman Tree)
|
||||||
public static Node make_huffmann_tree(List li){
|
public static Node make_huffmann_tree(List li){
|
||||||
//Sorting list in increasing order of its letter frequency
|
//Sorting list in increasing order of its letter frequency
|
||||||
li.sort(new comp());
|
li.sort(new comp());
|
||||||
Node temp=null;
|
Node temp=null;
|
||||||
Iterator it=li.iterator();
|
Iterator it=li.iterator();
|
||||||
//System.out.println(li.size());
|
//System.out.println(li.size());
|
||||||
//Loop for making huffman tree till only single node remains in list
|
//Loop for making huffman tree till only single node remains in list
|
||||||
while(true){
|
while(true){
|
||||||
temp=new Node();
|
temp=new Node();
|
||||||
//a and b are Node which are to be combine to make its parent
|
//a and b are Node which are to be combine to make its parent
|
||||||
Node a=new Node(),b=new Node();
|
Node a=new Node(),b=new Node();
|
||||||
a=null;b=null;
|
a=null;b=null;
|
||||||
//checking if list is eligible for combining or not
|
//checking if list is eligible for combining or not
|
||||||
//here first assignment of it.next in a will always be true as list till end will
|
//here first assignment of it.next in a will always be true as list till end will
|
||||||
//must have atleast one node
|
//must have atleast one node
|
||||||
a=(Node)it.next();
|
a=(Node)it.next();
|
||||||
//Below condition is to check either list has 2nd node or not to combine
|
//Below condition is to check either list has 2nd node or not to combine
|
||||||
//If this condition will be false, then it means construction of huffman tree is completed
|
//If this condition will be false, then it means construction of huffman tree is completed
|
||||||
if(it.hasNext()){b=(Node)it.next();}
|
if(it.hasNext()){b=(Node)it.next();}
|
||||||
//Combining first two smallest nodes in list to make its parent whose frequncy
|
//Combining first two smallest nodes in list to make its parent whose frequncy
|
||||||
//will be equals to sum of frequency of these two nodes
|
//will be equals to sum of frequency of these two nodes
|
||||||
if(b!=null){
|
if(b!=null){
|
||||||
temp.freq=a.freq+b.freq;a.data=0;b.data=1;//assigining 0 and 1 to left and right nodes
|
temp.freq=a.freq+b.freq;a.data=0;b.data=1;//assigining 0 and 1 to left and right nodes
|
||||||
temp.left=a;temp.right=b;
|
temp.left=a;temp.right=b;
|
||||||
//after combing, removing first two nodes in list which are already combined
|
//after combing, removing first two nodes in list which are already combined
|
||||||
li.remove(0);//removes first element which is now combined -step1
|
li.remove(0);//removes first element which is now combined -step1
|
||||||
li.remove(0);//removes 2nd element which comes on 1st position after deleting first in step1
|
li.remove(0);//removes 2nd element which comes on 1st position after deleting first in step1
|
||||||
li.add(temp);//adding new combined node to list
|
li.add(temp);//adding new combined node to list
|
||||||
//print_list(li); //For visualizing each combination step
|
//print_list(li); //For visualizing each combination step
|
||||||
}
|
}
|
||||||
//Sorting after combining to again repeat above on sorted frequency list
|
//Sorting after combining to again repeat above on sorted frequency list
|
||||||
li.sort(new comp());
|
li.sort(new comp());
|
||||||
it=li.iterator();//resetting list pointer to first node (head/root of tree)
|
it=li.iterator();//resetting list pointer to first node (head/root of tree)
|
||||||
if(li.size()==1){return (Node)it.next();} //base condition ,returning root of huffman tree
|
if(li.size()==1){return (Node)it.next();} //base condition ,returning root of huffman tree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Function for finding path between root and given letter ch
|
//Function for finding path between root and given letter ch
|
||||||
public static void dfs(Node n,String ch){
|
public static void dfs(Node n,String ch){
|
||||||
Stack<Node> st=new Stack(); // stack for storing path
|
Stack<Node> st=new Stack(); // stack for storing path
|
||||||
int freq=n.freq; // recording root freq to avoid it adding in path encoding
|
int freq=n.freq; // recording root freq to avoid it adding in path encoding
|
||||||
find_path_and_encode(st,n,ch,freq);
|
find_path_and_encode(st,n,ch,freq);
|
||||||
}
|
}
|
||||||
|
|
||||||
//A simple utility function to print stack (Used for printing path)
|
//A simple utility function to print stack (Used for printing path)
|
||||||
public static void print_path(Stack<Node> st){
|
public static void print_path(Stack<Node> st){
|
||||||
for(int i=0;i<st.size();i++){
|
for(int i=0;i<st.size();i++){
|
||||||
System.out.print(st.elementAt(i).data+" ");
|
System.out.print(st.elementAt(i).data+" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void find_path_and_encode(Stack<Node> st,Node root,String s,int f){
|
public static void find_path_and_encode(Stack<Node> st,Node root,String s,int f){
|
||||||
//Base condition
|
//Base condition
|
||||||
if(root!= null){
|
if(root!= null){
|
||||||
if(root.freq!=f){st.push(root);} // avoiding root to add in path/encoding bits
|
if(root.freq!=f){st.push(root);} // avoiding root to add in path/encoding bits
|
||||||
if(root.letr.equals(s)){print_path(st);return;} // Recursion stopping condition when path gets founded
|
if(root.letr.equals(s)){print_path(st);return;} // Recursion stopping condition when path gets founded
|
||||||
find_path_and_encode(st,root.left,s,f);
|
find_path_and_encode(st,root.left,s,f);
|
||||||
find_path_and_encode(st,root.right,s,f);
|
find_path_and_encode(st,root.right,s,f);
|
||||||
//Popping if path not found in right or left of this node,because we previously
|
//Popping if path not found in right or left of this node,because we previously
|
||||||
//pushed this node in taking a mindset that it might be in path
|
//pushed this node in taking a mindset that it might be in path
|
||||||
st.pop();
|
st.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
List <Node> li=new LinkedList<>();
|
List <Node> li=new LinkedList<>();
|
||||||
Scanner in=new Scanner(System.in);
|
Scanner in=new Scanner(System.in);
|
||||||
System.out.println("Enter number of distinct letters ");
|
System.out.println("Enter number of distinct letters ");
|
||||||
int n=in.nextInt();
|
int n=in.nextInt();
|
||||||
String s[]=new String[n];
|
String s[]=new String[n];
|
||||||
System.out.print("Enter letters with its frequncy to encode\n");
|
System.out.print("Enter letters with its frequncy to encode\n");
|
||||||
for(int i=0;i<n;i++){
|
for(int i=0;i<n;i++){
|
||||||
Node a=new Node();
|
Node a=new Node();
|
||||||
System.out.print("Enter letter : ");
|
System.out.print("Enter letter : ");
|
||||||
a.letr=in.next();s[i]=a.letr;
|
a.letr=in.next();s[i]=a.letr;
|
||||||
System.out.print("Enter frequncy : ");
|
System.out.print("Enter frequncy : ");
|
||||||
a.freq=in.nextInt();System.out.println();
|
a.freq=in.nextInt();System.out.println();
|
||||||
li.add(a);
|
li.add(a);
|
||||||
}
|
}
|
||||||
Node root=new Node();
|
Node root=new Node();
|
||||||
root=make_huffmann_tree(li);
|
root=make_huffmann_tree(li);
|
||||||
System.out.println("Letter\t\tEncoded Form");
|
System.out.println("Letter\t\tEncoded Form");
|
||||||
for(int i=0;i<n;i++){
|
for(int i=0;i<n;i++){
|
||||||
System.out.print(s[i]+"\t\t");dfs(root,s[i]);System.out.println();}
|
System.out.print(s[i]+"\t\t");dfs(root,s[i]);System.out.println();}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user