From 9f5b75c7821e8fbcb554b0c17da9c2e76b5aacde Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Tue, 17 Dec 2024 11:25:42 -0500 Subject: [PATCH] g --- huffman_coding/src/huffman_coding/Main.java | 46 +++++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/huffman_coding/src/huffman_coding/Main.java b/huffman_coding/src/huffman_coding/Main.java index 6104f38..2ed5af1 100644 --- a/huffman_coding/src/huffman_coding/Main.java +++ b/huffman_coding/src/huffman_coding/Main.java @@ -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 freqs; // Frequencies of characters. Huffman(ArrayList charset, ArrayList 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 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 sortedTrees, int maxIndexPossible) { + // 0 <= i <= trees.size(), such that it is sorted through [0, i]. + static void insert(ITree tree, ArrayList 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 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. } }