mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Merge pull request #586 from rmakynen/master
Fixed Compiler warnings, closed the scanner and fixed some typos
This commit is contained in:
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -14,24 +13,24 @@ Output :
|
|||||||
|
|
||||||
Enter number of distinct letters
|
Enter number of distinct letters
|
||||||
6
|
6
|
||||||
Enter letters with its frequncy to encode
|
Enter letters with its frequency to encode
|
||||||
Enter letter : a
|
Enter letter : a
|
||||||
Enter frequncy : 45
|
Enter frequency : 45
|
||||||
|
|
||||||
Enter letter : b
|
Enter letter : b
|
||||||
Enter frequncy : 13
|
Enter frequency : 13
|
||||||
|
|
||||||
Enter letter : c
|
Enter letter : c
|
||||||
Enter frequncy : 12
|
Enter frequency : 12
|
||||||
|
|
||||||
Enter letter : d
|
Enter letter : d
|
||||||
Enter frequncy : 16
|
Enter frequency : 16
|
||||||
|
|
||||||
Enter letter : e
|
Enter letter : e
|
||||||
Enter frequncy : 9
|
Enter frequency : 9
|
||||||
|
|
||||||
Enter letter : f
|
Enter letter : f
|
||||||
Enter frequncy : 5
|
Enter frequency : 5
|
||||||
|
|
||||||
Letter Encoded Form
|
Letter Encoded Form
|
||||||
a 0
|
a 0
|
||||||
@ -64,17 +63,17 @@ 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<Node> 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<Node> 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<Node> 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){
|
||||||
@ -89,7 +88,7 @@ public class Huffman {
|
|||||||
//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 frequency
|
||||||
//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
|
||||||
@ -109,7 +108,7 @@ public class Huffman {
|
|||||||
|
|
||||||
//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<Node>(); // 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);
|
||||||
}
|
}
|
||||||
@ -140,15 +139,16 @@ public class Huffman {
|
|||||||
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 frequency 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 frequency : ");
|
||||||
a.freq=in.nextInt();System.out.println();
|
a.freq=in.nextInt();System.out.println();
|
||||||
li.add(a);
|
li.add(a);
|
||||||
}
|
}
|
||||||
|
in.close();
|
||||||
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");
|
||||||
|
@ -1,25 +1,26 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Implementation of Knuth–Morris–Pratt algorithm
|
Implementation of Knuth–Morris–Pratt algorithm
|
||||||
Usage:
|
Usage: see the main function for an example
|
||||||
final String T = "AAAAABAAABA";
|
|
||||||
final String P = "AAAA";
|
|
||||||
KMPmatcher(T, P);
|
|
||||||
*/
|
*/
|
||||||
public class KMP {
|
public class KMP {
|
||||||
|
//a working example
|
||||||
// find the starting index in string T[] that matches the search word P[]
|
public static void main(String[] args) {
|
||||||
public void KMPmatcher(final String T, final String P) {
|
final String haystack = "AAAAABAAABA"; //This is the full string
|
||||||
final int m = T.length();
|
final String needle = "AAAA"; //This is the substring that we want to find
|
||||||
final int n = P.length();
|
KMPmatcher(haystack, needle);
|
||||||
final int[] pi = computePrefixFunction(P);
|
}
|
||||||
|
// find the starting index in string haystack[] that matches the search word P[]
|
||||||
|
public static void KMPmatcher(final String haystack, final String needle) {
|
||||||
|
final int m = haystack.length();
|
||||||
|
final int n = needle.length();
|
||||||
|
final int[] pi = computePrefixFunction(needle);
|
||||||
int q = 0;
|
int q = 0;
|
||||||
for (int i = 0; i < m; i++) {
|
for (int i = 0; i < m; i++) {
|
||||||
while (q > 0 && T.charAt(i) != P.charAt(q)) {
|
while (q > 0 && haystack.charAt(i) != needle.charAt(q)) {
|
||||||
q = pi[q - 1];
|
q = pi[q - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (T.charAt(i) == P.charAt(q)) {
|
if (haystack.charAt(i) == needle.charAt(q)) {
|
||||||
q++;
|
q++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,11 +29,9 @@ public class KMP {
|
|||||||
q = pi[q - 1];
|
q = pi[q - 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the prefix function
|
// return the prefix function
|
||||||
private int[] computePrefixFunction(final String P) {
|
private static int[] computePrefixFunction(final String P) {
|
||||||
final int n = P.length();
|
final int n = P.length();
|
||||||
final int[] pi = new int[n];
|
final int[] pi = new int[n];
|
||||||
pi[0] = 0;
|
pi[0] = 0;
|
||||||
@ -49,7 +48,6 @@ public class KMP {
|
|||||||
pi[i] = q;
|
pi[i] = q;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pi;
|
return pi;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user