File tree Expand file tree Collapse file tree 1 file changed +23
-8
lines changed
Interview Prep/section18_Tries Expand file tree Collapse file tree 1 file changed +23
-8
lines changed Original file line number Diff line number Diff line change 11package section18_Tries ;
22
3+ import java .util .BitSet ;
4+
35public class HuffmanEncoderClient {
46
57 public static void main (String [] args ) throws Exception {
68
79 String str = "abbcdda" ;
810 System .out .println ("original string: " + str );
11+ System .out .println ("size of original string is: " + str .length () * 2 + " bytes" );
12+
913 HuffmanEncoder huffman = new HuffmanEncoder (str );
1014
1115 String encoded = huffman .encode (str );
@@ -15,15 +19,26 @@ public static void main(String[] args) throws Exception {
1519 System .out .println ("decoded string: " + decoded );
1620
1721 System .out .println (str .equals (decoded ));
18- }
1922
20- }
23+ BitSet bitset = new BitSet (encoded .length ());
24+ int bitcounter = 0 ;
2125
26+ for (Character ch : encoded .toCharArray ()) {
27+ if (ch == '1' ) {
28+ bitset .set (bitcounter );
29+ }
30+ bitcounter ++;
31+ }
2232
23- /* output:
33+ byte [] arr = bitset .toByteArray ();
34+ System .out .println ("size of encoded string is: " + arr .length + " bytes" );
35+ }
36+
37+ }
2438
25- original string: abbcdda
26- encoded string: 00001011001001000
27- decoded string: abbcdda
28- true
29- */
39+ /*
40+ * output:
41+ *
42+ * original string: abbcdda encoded string: 00001011001001000 decoded string:
43+ * abbcdda true
44+ */
You canβt perform that action at this time.
0 commit comments