// Process Encode and Decode commands static void doCommands(BufferedReader d, HuffTree<Character> tree) throws IOException { HuffBaseNode<Character> temp; String line; int i; while ((line = d.readLine()) != null) { if (line.substring(0, 6).equals("decode")) { for (i = 0; line.charAt(i) != '"'; i++) ; System.out.print("Decode " + line.charAt(i++)); temp = tree.root(); for (; line.charAt(i) != '"'; i++) { // Traverse the tree if (line.charAt(i) == '0') temp = ((HuffInternalNode) temp).left(); else if (line.charAt(i) == '1') temp = ((HuffInternalNode) temp).right(); else assert false : "Bad input: " + line; if (temp.isLeaf()) { System.out.print(((HuffLeafNode) temp).element()); temp = tree.root(); // reset at root } } System.out.println(line.charAt(i)); } else if (line.substring(0, 6).equals("encode")) { for (i = 0; line.charAt(i) != '"'; i++) ; System.out.print("Encode " + line.charAt(i++)); for (; line.charAt(i) != '"'; i++) { // Assume codes are characters. Should generalize this. int index = getindex(line.charAt(i)); assert index < codeTable.size() : "Tried to find code of bad character|" + line.charAt(i) + "|"; System.out.print(codeTable.elementAt(index).code()); } System.out.println(line.charAt(i)); } else assert false : "Bad command line: " + line; } // while }
/** Start creating codes and putting them inside each node */ private void codings(HuffBaseNode rt) { if (rt instanceof HuffInternalNode) { if (((HuffInternalNode) rt).left() != null) { ((HuffInternalNode) rt).left().setCode(rt.getCode() + "0"); codings(((HuffInternalNode) rt).left()); } if (((HuffInternalNode) rt).right() != null) { ((HuffInternalNode) rt).right().setCode(rt.getCode() + "1"); codings(((HuffInternalNode) rt).right()); } } }
// 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"); } }
/** * el getter * * @return el -1 if that's an internal node or element of leaf node */ public int getElement() { if (root.isLeaf()) { return element; } else { return -1; } }
/** * Helper method to get all the leaf node from the root * * @param rt root */ private void realTree(HuffBaseNode rt) { if (rt.isLeaf()) { outSize += 10; newHuffArr[index++] = rt; } else { outSize += 1; if (((HuffInternalNode) rt).left() != null) { realTree(((HuffInternalNode) rt).left()); } if (((HuffInternalNode) rt).right() != null) { realTree(((HuffInternalNode) rt).right()); } } }
/** * Set coding for this HuffTree object * * @param newCode code to set */ public void setCode(String newCode) { root.setCode(newCode); }
/** * Get string of codings of this HuffTree object * * @return codings of root */ public String getCode() { return root.getCode(); }
/** * 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(); }