// Print out the codes; insert these codes into CodeTable static void outputTree(HuffBaseNode<Character> node, String prefix) { assert node != null : "Bad input tree"; // This is a full binary tree so must not be null subtree if (node.isLeaf()) { System.out.println(((HuffLeafNode<Character>) node).element() + "\t" + prefix); char temp = ((HuffLeafNode<Character>) node).element(); codeTable.addElement(new Code(temp, prefix)); total += prefix.length() * node.weight(); } else { outputTree(((HuffInternalNode) node).left(), prefix + "0"); outputTree(((HuffInternalNode) node).right(), prefix + "1"); } }
/** * implement compareTo method from canvas * * @param t object to compare with * @return 0 if equals, -1 if less, 1 if more */ public int compareTo(Object t) { HuffTree that = (HuffTree) t; if (root.weight() < that.weight()) return -1; else if (root.weight() == that.weight()) return 0; else return 1; }
/** * weight getter * * @return weight of HuffTree object */ public int weight() { return root.weight(); }