Skip to content

Commit 24eadc6

Browse files
authored
Merge pull request TheAlgorithms#586 from rmakynen/master
Fixed Compiler warnings, closed the scanner and fixed some typos
2 parents 6f7c2bf + 1177b54 commit 24eadc6

2 files changed

Lines changed: 31 additions & 33 deletions

File tree

β€ŽOthers/Huffman.javaβ€Ž

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import java.util.Comparator;
32
import java.util.Iterator;
43
import java.util.LinkedList;
@@ -14,24 +13,24 @@
1413
1514
Enter number of distinct letters
1615
6
17-
Enter letters with its frequncy to encode
16+
Enter letters with its frequency to encode
1817
Enter letter : a
19-
Enter frequncy : 45
18+
Enter frequency : 45
2019
2120
Enter letter : b
22-
Enter frequncy : 13
21+
Enter frequency : 13
2322
2423
Enter letter : c
25-
Enter frequncy : 12
24+
Enter frequency : 12
2625
2726
Enter letter : d
28-
Enter frequncy : 16
27+
Enter frequency : 16
2928
3029
Enter letter : e
31-
Enter frequncy : 9
30+
Enter frequency : 9
3231
3332
Enter letter : f
34-
Enter frequncy : 5
33+
Enter frequency : 5
3534
3635
Letter Encoded Form
3736
a 0
@@ -64,17 +63,17 @@ public class Huffman {
6463

6564
// A simple function to print a given list
6665
//I just made it for debugging
67-
public static void print_list(List li){
66+
public static void print_list(List<Node> li){
6867
Iterator<Node> it=li.iterator();
6968
while(it.hasNext()){Node n=it.next();System.out.print(n.freq+" ");}System.out.println();
7069
}
7170

7271
//Function for making tree (Huffman Tree)
73-
public static Node make_huffmann_tree(List li){
72+
public static Node make_huffmann_tree(List<Node> li){
7473
//Sorting list in increasing order of its letter frequency
7574
li.sort(new comp());
7675
Node temp=null;
77-
Iterator it=li.iterator();
76+
Iterator<Node> it=li.iterator();
7877
//System.out.println(li.size());
7978
//Loop for making huffman tree till only single node remains in list
8079
while(true){
@@ -89,7 +88,7 @@ public static Node make_huffmann_tree(List li){
8988
//Below condition is to check either list has 2nd node or not to combine
9089
//If this condition will be false, then it means construction of huffman tree is completed
9190
if(it.hasNext()){b=(Node)it.next();}
92-
//Combining first two smallest nodes in list to make its parent whose frequncy
91+
//Combining first two smallest nodes in list to make its parent whose frequency
9392
//will be equals to sum of frequency of these two nodes
9493
if(b!=null){
9594
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 static Node make_huffmann_tree(List li){
109108

110109
//Function for finding path between root and given letter ch
111110
public static void dfs(Node n,String ch){
112-
Stack<Node> st=new Stack(); // stack for storing path
111+
Stack<Node> st=new Stack<Node>(); // stack for storing path
113112
int freq=n.freq; // recording root freq to avoid it adding in path encoding
114113
find_path_and_encode(st,n,ch,freq);
115114
}
@@ -140,15 +139,16 @@ public static void main(String args[]){
140139
System.out.println("Enter number of distinct letters ");
141140
int n=in.nextInt();
142141
String s[]=new String[n];
143-
System.out.print("Enter letters with its frequncy to encode\n");
142+
System.out.print("Enter letters with its frequency to encode\n");
144143
for(int i=0;i<n;i++){
145144
Node a=new Node();
146145
System.out.print("Enter letter : ");
147146
a.letr=in.next();s[i]=a.letr;
148-
System.out.print("Enter frequncy : ");
147+
System.out.print("Enter frequency : ");
149148
a.freq=in.nextInt();System.out.println();
150149
li.add(a);
151150
}
151+
in.close();
152152
Node root=new Node();
153153
root=make_huffmann_tree(li);
154154
System.out.println("Letter\t\tEncoded Form");

β€ŽOthers/KMP.javaβ€Ž

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
21
/*
32
Implementation of Knuth–Morris–Pratt algorithm
4-
Usage:
5-
final String T = "AAAAABAAABA";
6-
final String P = "AAAA";
7-
KMPmatcher(T, P);
3+
Usage: see the main function for an example
84
*/
95
public class KMP {
10-
11-
// find the starting index in string T[] that matches the search word P[]
12-
public void KMPmatcher(final String T, final String P) {
13-
final int m = T.length();
14-
final int n = P.length();
15-
final int[] pi = computePrefixFunction(P);
6+
//a working example
7+
public static void main(String[] args) {
8+
final String haystack = "AAAAABAAABA"; //This is the full string
9+
final String needle = "AAAA"; //This is the substring that we want to find
10+
KMPmatcher(haystack, needle);
11+
}
12+
// find the starting index in string haystack[] that matches the search word P[]
13+
public static void KMPmatcher(final String haystack, final String needle) {
14+
final int m = haystack.length();
15+
final int n = needle.length();
16+
final int[] pi = computePrefixFunction(needle);
1617
int q = 0;
1718
for (int i = 0; i < m; i++) {
18-
while (q > 0 && T.charAt(i) != P.charAt(q)) {
19+
while (q > 0 && haystack.charAt(i) != needle.charAt(q)) {
1920
q = pi[q - 1];
2021
}
2122

22-
if (T.charAt(i) == P.charAt(q)) {
23+
if (haystack.charAt(i) == needle.charAt(q)) {
2324
q++;
2425
}
2526

@@ -28,11 +29,9 @@ public void KMPmatcher(final String T, final String P) {
2829
q = pi[q - 1];
2930
}
3031
}
31-
3232
}
33-
3433
// return the prefix function
35-
private int[] computePrefixFunction(final String P) {
34+
private static int[] computePrefixFunction(final String P) {
3635
final int n = P.length();
3736
final int[] pi = new int[n];
3837
pi[0] = 0;
@@ -49,7 +48,6 @@ private int[] computePrefixFunction(final String P) {
4948
pi[i] = q;
5049

5150
}
52-
5351
return pi;
5452
}
55-
}
53+
}

0 commit comments

Comments
 (0)