From 19f82ff0f754e363d1be74b97e8e6dfd32a5b719 Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 17 Dec 2024 01:13:07 -0500 Subject: [PATCH] THings. --- huffman_coding/src/huffman_coding/Main.java | 103 +++++++++++++++++--- 1 file changed, 87 insertions(+), 16 deletions(-) diff --git a/huffman_coding/src/huffman_coding/Main.java b/huffman_coding/src/huffman_coding/Main.java index 6b1f2c6..6104f38 100644 --- a/huffman_coding/src/huffman_coding/Main.java +++ b/huffman_coding/src/huffman_coding/Main.java @@ -1,28 +1,53 @@ package huffman_coding; import java.util.ArrayList; +import java.util.Arrays; + import tester.Tester; class Examples { + ITree a; + ITree b; + ITree c; + ITree d; + ITree e; + ITree f; - void testTest(Tester t) { - t.checkExpect(1, 1); + ArrayList trees; + ArrayList treesSorted; + + void init() { + a = new Leaf(12, "a"); + b = new Leaf(45, "b"); + c = new Leaf(5, "c"); + d = new Leaf(13, "d"); + e = new Leaf(9, "e"); + f = new Leaf(16, "f"); + + trees = new ArrayList(Arrays.asList(a, b, c, d, e, f)); + treesSorted = new ArrayList(Arrays.asList(c, e, a, d, f, b)); + } + + void testITreeSort(Tester t) { + init(); + Util.sort(trees); + t.checkExpect(trees, treesSorted); } } class Huffman { ArrayList charset; // Character set. - ArrayList freqs; // Relative frequencies of characters. + ArrayList freqs; // Frequencies of characters. - Huffman(ArrayList charset, ArrayList freqs) { - if (charset.size() != freqs.size()) throw new IllegalArgumentException( - "Character set must match frequencies." - ); + Huffman(ArrayList charset, ArrayList freqs) { + 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; @@ -30,17 +55,63 @@ class Huffman { } // Binary tree. -interface ITree {} +interface ITree { + // Compare this and another tree, and return the lesser of the two. + ITree lesserOf(ITree n); -class Node implements ITree { + // Is this tree less than the given frequency? + boolean lessThan(int freq); +} - ITree l; - ITree r; +abstract class ATree implements ITree { + int freq; // The frequency of the tree. - Node(ITree l, ITree r) { + ATree(int freq) { + this.freq = freq; + } + + public ITree lesserOf(ITree n) { + return n.lessThan(this.freq) ? n : this; + } + + public boolean lessThan(int freq) { + return this.freq < freq; + } +} + +class Node extends ATree { + + // Left and right branches of tree. + ITree l, r; + + Node(int freq, ITree l, ITree r) { + super(freq); this.l = l; this.r = r; } } -class Leaf implements ITree {} +class Leaf extends ATree { + String ch; // The character. + + Leaf(int freq, String ch) { + super(freq); + this.ch = ch; + } +} + +class Util { + // Sort a list of trees from least to most frequent. + static void sort(ArrayList trees) { + + } + + // 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) { + } + + // Insert a Tree in an already fully sorted ArrayList of Trees. + static void insert(ITree tree, ArrayList sortedTrees) { + } +}