1- // // Given a Huffman tree and a string, encode that string into a new string
2- // // consisting only of 1s and 0s, using the code given by the tree.
3- var encodeString = function ( input , huffmanTree ) {
4- return "Not implemented yet!" ;
1+ // Given a Huffman tree and a string, encode that string into a new string
2+ // consisting only of 1s and 0s, using the code given by the tree.
3+ var encodeString = function ( input , huffman ) {
4+ var output = "" ;
5+ for ( var i = 0 ; i < input . length ; i ++ ) {
6+ var currentNode = huffman ;
7+ var nextCharacter = input [ i ] ;
8+ while ( currentNode . val . length > 1 ) {
9+ if ( currentNode . left . val . indexOf ( nextCharacter ) !== - 1 ) {
10+ currentNode = currentNode . left ;
11+ output += "0" ;
12+ } else if ( currentNode . right . val . indexOf ( nextCharacter ) !== - 1 ) {
13+ currentNode = currentNode . right ;
14+ output += "1" ;
15+ } else {
16+ throw new Error ( "Character " + nextCharacter + " is not in this Huffman tree." ) ;
17+ }
18+ }
19+ }
20+ return output ;
521} ;
622
7- // // Given a Huffman tree and a string of 1s and 0s, decode that string into
8- // // a new, human-readable string, using the code given by the tree.
9- var decodeString = function ( input , huffmanTree ) {
10- return "Not implemented yet!" ;
23+ // Given a Huffman tree and a string of 1s and 0s, decode that string into
24+ // a new, human-readable string, using the code given by the tree.
25+ var decodeString = function ( input , huffman ) {
26+ var output = "" ;
27+ var currNode = huffman ;
28+ for ( var currIdx = 0 ; currIdx < input . length ; currIdx ++ ) {
29+ var currBit = input [ currIdx ] ;
30+ if ( currBit === "0" ) {
31+ currNode = currNode . left ;
32+ } else if ( currBit === "1" ) {
33+ currNode = currNode . right ;
34+ }
35+ if ( currNode . val . length === 1 ) {
36+ output += currNode . val [ 0 ] ;
37+ currNode = huffman ;
38+ }
39+ }
40+ return output ;
1141} ;
1242
1343// Given a corpus of text, return a Huffman tree that represents the
@@ -22,7 +52,39 @@ var decodeString = function(input, huffmanTree) {
2252// You may also use the `Tree` class that is provided in the file `misc.js`
2353// Some corpuses are included as the variables `lorumIpsum` and `declaration`.
2454var makeHuffmanTree = function ( corpus ) {
25- alert ( "You must implement makeHuffmanTree first!" )
26- return new Tree ( ) ;
55+ var charCounts = countChars ( corpus ) ;
56+ var pq = new PriorityQueue ( ) ;
57+ for ( var c in charCounts ) {
58+ var n = charCounts [ c ] ;
59+ var tree = new Tree ( [ c ] ) ;
60+ pq . insert ( n , tree ) ;
61+ }
62+
63+ while ( pq . size ( ) > 1 ) {
64+ var first = pq . extract ( ) ;
65+ var second = pq . extract ( ) ;
66+
67+ var tree1 = first . val ;
68+ var tree2 = second . val ;
69+
70+ var key1 = first . key ;
71+ var key2 = second . key ;
72+
73+ var newTree = new Tree ( tree1 . val . concat ( tree2 . val ) ) ;
74+ newTree . left = tree1 ;
75+ newTree . right = tree2 ;
76+ pq . insert ( key1 + key2 , newTree ) ;
77+ }
78+
79+ return pq . extract ( ) . val ;
2780} ;
2881
82+ var countChars = function ( input ) {
83+ var counts = { } ;
84+ for ( var i = 0 ; i < input . length ; i ++ ) {
85+ var c = input [ i ] ;
86+ counts [ c ] = counts [ c ] || 0 ;
87+ counts [ c ] ++ ;
88+ }
89+ return counts ;
90+ } ;
0 commit comments