diff --git a/huffman_coding/src/huffman_coding/Main.java b/huffman_coding/src/huffman_coding/Main.java index 3144a58..fc1dd87 100644 --- a/huffman_coding/src/huffman_coding/Main.java +++ b/huffman_coding/src/huffman_coding/Main.java @@ -31,8 +31,7 @@ class Examples { void testITreeSort(Tester t) { init(); - Util.sort(trees); - t.checkExpect(trees, treesSorted); + t.checkExpect(Util.sort(trees), treesSorted); } } @@ -106,16 +105,27 @@ 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); + static ArrayList sort(ArrayList trees) { + System.out.println("Sorting."); + // The sorted list starts out as just the first element. + ArrayList sorted = new ArrayList(Arrays.asList(trees.getFirst())); // Why must this be so fucking + // verbose. + + if (trees.size() == 1) + return sorted; + + for (int i = 1; i < trees.size() - 1; i++) { + Util.insert(trees.get(i), sorted, i); } + + return sorted; } // Insert a Tree into an ArrayList of trees already sorted in [0, i], where // 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++) { + System.out.println("Set j = 0. While j < " + (i + 1) + ", j++."); + for (int j = 0; j < (i + 1); j++) { if (trees.get(j).lessThan(tree)) { trees.add(j, tree); return;