-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman1.java
More file actions
144 lines (112 loc) · 3.49 KB
/
Copy pathhuffman1.java
File metadata and controls
144 lines (112 loc) · 3.49 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package MyPractice;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
public class huffman1 {
static PriorityQueue<Alphabet> pq = new PriorityQueue<>(1000,new HuffmanComparator());
static ArrayList<Integer> codeLengths = new ArrayList<>();
public static void main(String [] args)
{
readFromFile();
Alphabet root = executeHuffman();
traverse(root,0);
//
//min = 9
System.out.println(Collections.max(codeLengths));
// Alphabet a = new Alphabet(1,1);
// Alphabet b = new Alphabet(2,2);
// Alphabet c = new Alphabet(3,3);
// a.right=b;
// a.left=c;
// c.left=new Alphabet(4,4);
// traverse(a,0);
// System.out.println(Collections.max(codeLengths));
// System.out.println(pq.poll().weight);
}
public static class Alphabet {
public long index;
public long weight;
public Alphabet left;
public Alphabet right;
public Alphabet(long index, long weight) {
this.index = index;
this.weight = weight;
left=null;
right=null;
}
}
public static void traverse(Alphabet root,int size){
if(root == null){
codeLengths.add(size);
return;
}
if(root.left!=null || root.right!=null)
{
size=size+1;
}
traverse(root.left,size);
traverse(root.right,size);
}
public static class HuffmanComparator implements Comparator<Alphabet>
{
@Override
public int compare(Alphabet a, Alphabet b)
{
if(a.weight>b.weight)
return 1;
else if(a.weight==b.weight)
return 0;
else
return -1;
}
}
public static Alphabet union(Alphabet a,Alphabet b)
{
// if(a==null || b==null)
// return null;
// System.out.println("Combinining with weight: " + a.weight + " , " + b.weight);
Alphabet union = new Alphabet(a.index+b.index,a.weight+b.weight);
union.left=a;
union.right=b;
pq.add(union);
return union;
}
public static Alphabet executeHuffman()
{
//Alphabet node = null;
while (pq.size()>1)
{
Alphabet toCombine1 = pq.poll();
Alphabet toCombine2 = pq.poll();
// if(toCombine.weight<=0)
// continue;
System.out.println("Combinining with weight: " + toCombine1.weight + " , " + toCombine2.weight);
union(toCombine1,toCombine2);
}
return pq.poll();
}
public static void readFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\gupta\\IdeaProjects\\untitled\\src\\MyPractice\\huffman.txt"));
String line = reader.readLine();
int index=0;
while (line != null) {
if (index++ == 0) {
line = reader.readLine();
continue;
}
int val = Integer.parseInt(line);
Alphabet a = new Alphabet(index++,val);
pq.add(a);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}