g
This commit is contained in:
@@ -2,10 +2,10 @@ package huffman_coding;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
|
||||
ITree a;
|
||||
ITree b;
|
||||
ITree c;
|
||||
@@ -41,13 +41,13 @@ class Huffman {
|
||||
ArrayList<Integer> freqs; // Frequencies of characters.
|
||||
|
||||
Huffman(ArrayList<String> charset, ArrayList<Integer> freqs) {
|
||||
if (charset.size() != freqs.size())
|
||||
throw new IllegalArgumentException(
|
||||
"Character set must match frequencies.");
|
||||
if (charset.size() != freqs.size()) throw new IllegalArgumentException(
|
||||
"Character set must match frequencies."
|
||||
);
|
||||
|
||||
if (charset.size() < 2)
|
||||
throw new IllegalArgumentException(
|
||||
"Character set too small.");
|
||||
if (charset.size() < 2) throw new IllegalArgumentException(
|
||||
"Character set too small."
|
||||
);
|
||||
|
||||
this.charset = charset;
|
||||
this.freqs = freqs;
|
||||
@@ -57,21 +57,22 @@ class Huffman {
|
||||
// Binary tree.
|
||||
interface ITree {
|
||||
// Compare this and another tree, and return the lesser of the two.
|
||||
ITree lesserOf(ITree n);
|
||||
boolean lessThan(ITree n);
|
||||
|
||||
// Is this tree less than the given frequency?
|
||||
boolean lessThan(int freq);
|
||||
}
|
||||
|
||||
abstract class ATree implements ITree {
|
||||
|
||||
int freq; // The frequency of the tree.
|
||||
|
||||
ATree(int freq) {
|
||||
this.freq = freq;
|
||||
}
|
||||
|
||||
public ITree lesserOf(ITree n) {
|
||||
return n.lessThan(this.freq) ? n : this;
|
||||
public boolean lessThan(ITree n) {
|
||||
return n.lessThan(this.freq);
|
||||
}
|
||||
|
||||
public boolean lessThan(int freq) {
|
||||
@@ -92,6 +93,7 @@ class Node extends ATree {
|
||||
}
|
||||
|
||||
class Leaf extends ATree {
|
||||
|
||||
String ch; // The character.
|
||||
|
||||
Leaf(int freq, String ch) {
|
||||
@@ -101,17 +103,35 @@ class Leaf extends ATree {
|
||||
}
|
||||
|
||||
class Util {
|
||||
|
||||
// Sort a list of trees from least to most frequent.
|
||||
static void sort(ArrayList<ITree> trees) {
|
||||
|
||||
for (int i = 0; i < trees.size() - 1; i++) {
|
||||
Util.insert(trees.get(i), trees, i);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a Tree into an ArrayList of trees already sorted in [0, i], where
|
||||
// 0 <= i <= sortedTrees.size(), such that it is sorted through [0, i].
|
||||
static void insert(ITree tree, ArrayList<ITree> sortedTrees, int maxIndexPossible) {
|
||||
// 0 <= i <= trees.size(), such that it is sorted through [0, i].
|
||||
static void insert(ITree tree, ArrayList<ITree> trees, int i) {
|
||||
for (int j = 0; j < i + 1; j++) {
|
||||
if (trees.get(j).lessThan(tree)) {
|
||||
trees.add(j, tree);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
trees.add(i, tree);
|
||||
}
|
||||
|
||||
// Insert a Tree in an already fully sorted ArrayList of Trees.
|
||||
static void insert(ITree tree, ArrayList<ITree> sortedTrees) {
|
||||
for (int i = 0; i < sortedTrees.size(); i++) {
|
||||
if (sortedTrees.get(i).lessThan(tree)) {
|
||||
sortedTrees.add(i, tree);
|
||||
return;
|
||||
}
|
||||
}
|
||||
sortedTrees.addLast(tree); // This tree is bigger than all the others.
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user