Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class main {
- public static void main(String[] args) {
- // THE PRINTED VALUES WILL BE IN THE SAME ORDER AS THE VALUES BELOW, MEANING IF 'X' IS THE FIRST VALUE IN THE
- // ARRAY, THEN THE FIRST CODE PRINTED WILL CORRESPOND TO 'X'
- int[] array = new int[256];
- String[] arrayS = new String[256];
- array[0] = 1000; //
- array[1] = 800; //
- array[2] = 600; //
- array[3] = 400; //
- array[4] = 300; //
- array[5] = 200; //
- Element e = Encode.Huffman( array );
- Node root = e.data;
- int i = 0;
- String[] codeArray = getCodes(root,arrayS,"");
- for ( String s : codeArray ) {
- if ( s != null ) {
- System.out.println( array[i] + ": " + s );
- i++;
- }
- }
- }
- public static void inorder( Node node ) {
- if ( node == null ) return;
- inorder(node.left);
- System.out.println( node.key );
- inorder(node.right);
- }
- public static String[] getCodes(Node node, String[] codeArray, String code) {
- // Array for the codes
- //System.out.println( node.key );
- if (node.left == null) {
- codeArray[node.key] = code;
- } else {
- getCodes(node.left, codeArray, code + "0");
- getCodes(node.right, codeArray, code + "1");
- }
- // When a null node is reached, save the code in the array
- return codeArray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement