Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class main {
  4.     public static void main(String[] args) {
  5.         // THE PRINTED VALUES WILL BE IN THE SAME ORDER AS THE VALUES BELOW, MEANING IF 'X' IS THE FIRST VALUE IN THE
  6.         // ARRAY, THEN THE FIRST CODE PRINTED WILL CORRESPOND TO 'X'
  7.  
  8.         int[] array = new int[256];
  9.         String[] arrayS = new String[256];
  10.         array[0] = 1000; //
  11.         array[1] = 800; //
  12.         array[2] = 600; //
  13.         array[3] = 400; //
  14.         array[4] = 300; //
  15.         array[5] = 200; //
  16.  
  17.         Element e = Encode.Huffman( array );
  18.         Node root = e.data;
  19.         int i = 0;
  20.         String[] codeArray = getCodes(root,arrayS,"");
  21.         for ( String s : codeArray ) {
  22.             if ( s != null ) {
  23.                 System.out.println( array[i] + ": " + s );
  24.                 i++;
  25.             }
  26.         }
  27.  
  28.     }
  29.  
  30.     public static void inorder( Node node ) {
  31.         if ( node == null ) return;
  32.         inorder(node.left);
  33.         System.out.println( node.key );
  34.         inorder(node.right);
  35.  
  36.     }
  37.  
  38.     public static String[] getCodes(Node node, String[] codeArray, String code) {
  39.         // Array for the codes
  40.         //System.out.println( node.key );
  41.         if (node.left == null) {
  42.             codeArray[node.key] = code;
  43.         } else {
  44.             getCodes(node.left, codeArray, code + "0");
  45.             getCodes(node.right, codeArray, code + "1");
  46.         }
  47.         // When a null node is reached, save the code in the array
  48.         return codeArray;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement