1-
21import java .util .Comparator ;
32import java .util .Iterator ;
43import java .util .LinkedList ;
1413
1514Enter number of distinct letters
16156
17- Enter letters with its frequncy to encode
16+ Enter letters with its frequency to encode
1817Enter letter : a
19- Enter frequncy : 45
18+ Enter frequency : 45
2019
2120Enter letter : b
22- Enter frequncy : 13
21+ Enter frequency : 13
2322
2423Enter letter : c
25- Enter frequncy : 12
24+ Enter frequency : 12
2625
2726Enter letter : d
28- Enter frequncy : 16
27+ Enter frequency : 16
2928
3029Enter letter : e
31- Enter frequncy : 9
30+ Enter frequency : 9
3231
3332Enter letter : f
34- Enter frequncy : 5
33+ Enter frequency : 5
3534
3635Letter Encoded Form
3736a 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 \t Encoded Form" );
0 commit comments