Skip to content

Commit

Permalink
some bug fixes for demo site and renaming of priority queue stuff and…
Browse files Browse the repository at this point in the history
… addition docs
  • Loading branch information
peterkhayes committed Aug 26, 2014
1 parent 70ede8c commit b1cd240
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
17 changes: 16 additions & 1 deletion HuffmanCoding/Demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,17 @@ <h2>Efficiency:</h2>
currNode = currNode.left;
} else if (currBit === "1") {
currNode = currNode.right;
} else {
throw new Error("String to decode must consist of only 0s and 1s.");
}
if (currNode.val.length === 1) {
output += currNode.val[0];
currNode = huffman;
}
}
if (currNode !== huffman) {
throw new Error("String cannot be decoded - make sure you aren't missing any bits!");
}
return output;
};

Expand Down Expand Up @@ -230,7 +235,7 @@ <h2>Efficiency:</h2>
var pairs = Object
.keys(dictionary)
.sort(function(a, b) {
return dictionary[a].length - dictionary[b].length;
return a < b ? -1 : 1;
})
.forEach(function(key) {
var li = $("<li></li>");
Expand All @@ -250,11 +255,16 @@ <h2>Efficiency:</h2>
};

var triggerEncode = function() {
$("textarea.decode").val('');
if (!huffman) {
$(".error").text("Please enter a corpus and create an encoding scheme first.")
return;
};
var input = $('textarea.encode').val();
if (!input) {
$(".error").text("Please enter some text to encode.")
return;
}
var output;
try {
output = encodeString(input, huffman);
Expand All @@ -274,11 +284,16 @@ <h2>Efficiency:</h2>

var triggerDecode = function() {
$(".efficiency").hide();
$("textarea.encode").val('');
if (!huffman) {
$(".error").text("Please enter a corpus and create an encoding scheme first.")
return;
};
var input = $('textarea.decode').val();
if (!input) {
$(".error").text("Please enter some text to decode.")
return;
}
var output;
try {
output = decodeString(input, huffman);
Expand Down
11 changes: 9 additions & 2 deletions HuffmanCoding/src/huffman.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ var decodeString = function(input, huffmanTree) {

// Given a corpus of text, return a Huffman tree that represents the
// frequencies of characters in the corpus.
// You may use the `Tree` class that is provided in the file `misc.js`
// A sample corpus is also included as the variable `lorumIpsum`.
//
// You should use the `PriorityQueue` class that is provided in the
// file `priorityQueue.js`. The relevant methods are .insert(key, val),
// which inserts a value with the given key into the queue, and
// .extract(), which returns the {key: key, val: val} pair with the lowest
// key priority.
//
// 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) {
return new Tree();
};
Expand Down
1 change: 0 additions & 1 deletion HuffmanCoding/src/priorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ PriorityQueue.prototype.insert = function(key, val) {
var working = parent !== undefined;

while (working && current > 0) {
console.log(this._key(parent), this._key(current));
if (this._key(parent) > this._key(current)) {
this._swap(current, parent);
current = parent;
Expand Down

0 comments on commit b1cd240

Please sign in to comment.