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