-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman.java
122 lines (112 loc) · 2.86 KB
/
Huffman.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.Scanner;
import java.util.ArrayList;
class Node {
Node parentId = null;
char ch = ' ';
int freq = 0;
String code = "";
public Node(char ch,int n) {
this.ch = ch;
this.freq = n;
}
public String getCode() {
if(parentId==null) return "";
return parentId.getCode()+code;
}
}
class Huffman {
public static void sort(Node[] node) {
for(int i=0;i<node.length-1;i++)
for(int j=i+1;j<node.length;j++)
if(node[j].freq > node[i].freq ) {
Node temp = node[i];
node[i] = node[j];
node[j] = temp;
}
}
public static void insert(Node[] node,Node nod) {
int i=0;
for(i=0;i<node.length-1;i++)
if(node[i].freq<nod.freq) break;
for(int j=node.length-1;j>i;j--)
{node[j]=node[j-1];}
node[i] = nod;
}
public static int nos(Node[] node) {
int count = 0;
for(int i=0;i<node.length;i++)
if(node[i].parentId==null) count++;
return count;
}
public static void println(String str) {
System.out.println(""+str);
}
public static void print(String str) {
System.out.print(""+str);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
print("Enter a string:");
String str = sc.next();
char[] chr = new char[str.length()];
int[] nchar = new int[str.length()];
int chf = 0;
for(int i=0;i<str.length();i++) {
int nch = 0;
boolean dist = true;
for(int k=0;k<i;k++)
if(str.charAt(i)==chr[k]) dist = false ;
if(dist)
for(int j=i;j<str.length();j++) {
if(str.charAt(i)==str.charAt(j)) nch++;
}
else
continue;
chr[chf] = str.charAt(i);
nchar[chf++] = nch;
}
Node[] node = new Node[chf];
for(int i=0;i<chf;i++){
node[i] = new Node(chr[i],nchar[i]);
}
Node[] org = new Node[chf];
sort(node);
for(int i=0;i<chf;i++){
org[i] = node[i];
}
for(int i=0;i<chf;i++)
println(node[i].ch+" = "+node[i].freq);
int k=chf-1,j=chf-2;
while (k!=0) {
if(k==0) break;
println(j+" "+k);
if(node[j].freq < node[k].freq) {
node[j].code+="0";
node[k].code+="1";
}
else {
node[j].code+="1";
node[k].code+="0";
}
Node nod = new Node('\0',node[j].freq+node[k].freq);
node[j].parentId = nod;
node[k].parentId = nod;
if(node[j].ch!='\0'){
for(int i=0;i<org.length;i++) if(org[i].ch==node[j].ch) org[i]=node[j];
}
if(node[k].ch!='\0'){
for(int i=0;i<org.length;i++) if(org[i].ch==node[k].ch) org[i]=node[k];
}
for(int i=0;i<node.length;i++)
println(node[i].ch+" = "+node[i].freq);
println("\n");
insert(node,nod);
for(int i=0;i<node.length;i++)
println(node[i].ch+" = "+node[i].freq);
j--;k--;
}
for(int i=0;i<org.length;i++) {
println("Charecter "+org[i].ch+" : "+org[i].getCode());
}
}
}