Skip to content

Commit

Permalink
Move solution to src/huffman.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Syeoryn committed Oct 29, 2015
1 parent 603aeca commit 5e9ca1d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
2 changes: 1 addition & 1 deletion SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<!-- Load tests and helpers here -->
<script src="src/priorityQueue.js"></script>
<script src="src/misc.js"></script>
<script src="solution/solution.js"></script>
<script src="src/huffman.js"></script>
<script src="spec/spec.js"></script>

<script>
Expand Down
82 changes: 72 additions & 10 deletions src/huffman.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
// // Given a Huffman tree and a string, encode that string into a new string
// // consisting only of 1s and 0s, using the code given by the tree.
var encodeString = function(input, huffmanTree) {
return "Not implemented yet!";
// Given a Huffman tree and a string, encode that string into a new string
// consisting only of 1s and 0s, using the code given by the tree.
var encodeString = function(input, huffman) {
var output = "";
for (var i = 0; i < input.length; i++) {
var currentNode = huffman;
var nextCharacter = input[i];
while (currentNode.val.length > 1) {
if (currentNode.left.val.indexOf(nextCharacter) !== -1) {
currentNode = currentNode.left;
output += "0";
} else if (currentNode.right.val.indexOf(nextCharacter) !== -1) {
currentNode = currentNode.right;
output += "1";
} else {
throw new Error("Character " + nextCharacter + " is not in this Huffman tree.");
}
}
}
return output;
};

// // Given a Huffman tree and a string of 1s and 0s, decode that string into
// // a new, human-readable string, using the code given by the tree.
var decodeString = function(input, huffmanTree) {
return "Not implemented yet!";
// Given a Huffman tree and a string of 1s and 0s, decode that string into
// a new, human-readable string, using the code given by the tree.
var decodeString = function(input, huffman) {
var output = "";
var currNode = huffman;
for (var currIdx = 0; currIdx < input.length; currIdx++) {
var currBit = input[currIdx];
if (currBit === "0") {
currNode = currNode.left;
} else if (currBit === "1") {
currNode = currNode.right;
}
if (currNode.val.length === 1) {
output += currNode.val[0];
currNode = huffman;
}
}
return output;
};

// Given a corpus of text, return a Huffman tree that represents the
Expand All @@ -22,7 +52,39 @@ var decodeString = function(input, huffmanTree) {
// You may also use the `Tree` class that is provided in the file `misc.js`
// Some corpuses are included as the variables `lorumIpsum` and `declaration`.
var makeHuffmanTree = function(corpus) {
alert("You must implement makeHuffmanTree first!")
return new Tree();
var charCounts = countChars(corpus);
var pq = new PriorityQueue();
for (var c in charCounts) {
var n = charCounts[c];
var tree = new Tree([c]);
pq.insert(n, tree);
}

while (pq.size() > 1) {
var first = pq.extract();
var second = pq.extract();

var tree1 = first.val;
var tree2 = second.val;

var key1 = first.key;
var key2 = second.key;

var newTree = new Tree(tree1.val.concat(tree2.val));
newTree.left = tree1;
newTree.right = tree2;
pq.insert(key1+key2, newTree);
}

return pq.extract().val;
};

var countChars = function(input) {
var counts = {};
for (var i = 0; i < input.length; i++) {
var c = input[i];
counts[c] = counts[c] || 0;
counts[c]++;
}
return counts;
};

0 comments on commit 5e9ca1d

Please sign in to comment.