private static Node buildTree( BitVector lexSortedCodeWords[], final int[] symbol, int prefix, int offset, int length ) { if ( DEBUG ) { System.err.println( "****** " + offset + " " + length ); System.err.println( Arrays.toString( lexSortedCodeWords ) ); for( int i = 0; i < length; i++ ) { System.err.print( lexSortedCodeWords[ offset + i ].size() + "\t" ); for( int j = 0; j < lexSortedCodeWords[ offset + i ].size(); j++ ) System.err.print( lexSortedCodeWords[ offset + i ].getBoolean( j ) ? 1 : 0 ); System.err.println(); } } if ( length == 1 ) return new LeafNode( symbol[ offset ] ); for( int i = length - 1; i-- != 0; ) if ( lexSortedCodeWords[ offset + i ].get( prefix ) != lexSortedCodeWords[ offset + i + 1 ].get( prefix ) ) { final Node node = new Node(); node.left = buildTree( lexSortedCodeWords, symbol, prefix + 1, offset, i + 1 ); node.right = buildTree( lexSortedCodeWords, symbol, prefix + 1, offset + i + 1, length - i - 1 ); return node; } throw new IllegalStateException(); }
private static Node buildTree( BitVector lexSortedCodeWords[], final int[] symbol, int prefix, int offset, int length ) { if ( DEBUG ) { System.err.println( "****** " + offset + " " + length ); System.err.println( Arrays.toString( lexSortedCodeWords ) ); for( int i = 0; i < length; i++ ) { System.err.print( lexSortedCodeWords[ offset + i ].size() + "\t" ); for( int j = 0; j < lexSortedCodeWords[ offset + i ].size(); j++ ) System.err.print( lexSortedCodeWords[ offset + i ].getBoolean( j ) ? 1 : 0 ); System.err.println(); } } if ( length == 1 ) return new LeafNode( symbol[ offset ] ); for( int i = length - 1; i-- != 0; ) if ( lexSortedCodeWords[ offset + i ].get( prefix ) != lexSortedCodeWords[ offset + i + 1 ].get( prefix ) ) { final Node node = new Node(); node.left = buildTree( lexSortedCodeWords, symbol, prefix + 1, offset, i + 1 ); node.right = buildTree( lexSortedCodeWords, symbol, prefix + 1, offset + i + 1, length - i - 1 ); return node; } throw new IllegalStateException(); }