Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
solution
89 changes: 2 additions & 87 deletions Demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<script src="lib/jquery.js"></script>
<script src="src/priorityQueue.js"></script>
<script src="src/misc.js"></script>
<script src="src/huffman.js"></script>
</head>
<body>
<div class='column c1'>
Expand Down Expand Up @@ -107,92 +108,6 @@ <h2>Efficiency:</h2>
}
</style>
<script>
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;
};

var makeHuffmanTree = function(input) {
var charCounts = countChars(input);
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 encodeChar = function(c, huffman) {
var output = "";
while (huffman.val.length > 1) {
if (huffman.left.val.indexOf(c) !== -1) {
huffman = huffman.left;
output += "0";
} else if (huffman.right.val.indexOf(c) !== -1) {
huffman = huffman.right;
output += "1";
} else {
throw new Error("Character " + c + " is not in this Huffman tree.");
}
}
return output;
};

var encodeString = function(input, huffman) {
var output = "";
for (var i = 0; i < input.length; i++) {
output += encodeChar(input[i], huffman);
}
return output;
};

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;
} 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;
};

var getDictionaryFromTree = function(tree) {
var result = {};

Expand Down Expand Up @@ -315,4 +230,4 @@ <h2>Efficiency:</h2>

</script>
</body>
</html>
</html>
75 changes: 75 additions & 0 deletions solution/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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;
};

var makeHuffmanTree = function(input) {
var charCounts = countChars(input);
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 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;
};

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;
};
81 changes: 72 additions & 9 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 "";
// 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 "";
// 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,6 +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) {
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;
};