コード例 #1
0
 private void findCode(
     Node localRoot,
     String
         code) { // Recursively traverses tree saving the leaf node locations as a string in their
                 // respective codeTable[] slot
   if (localRoot.dData == '+') {
     findCode(localRoot.leftChild, code + "0");
     findCode(localRoot.rightChild, code + "1");
   } else {
     codeTable[(int) localRoot.dData - 65] = code;
   }
 }
コード例 #2
0
  public void
      code() { // Takes codeTable data and creates a coded string with the binary versions of the
               // substrings within the user input
    findCode(huffTree.root, encoded);
    for (int i = 0; i < codeTable.length; i++) {
      if (freqTable[i] > 0) {
        System.out.println((char) (i + 65) + " " + codeTable[i]);
      }
    }
    System.out.println("Coded message:");
    for (int i = 0; i < input.length(); i++) {
      encoded += codeTable[(int) input.charAt(i) - 65];
    }

    System.out.print("\n" + encoded + "\n");
  }