Skip to content

Commit

Permalink
Use src/huffman.js in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Syeoryn committed Oct 22, 2015
1 parent 08feb3e commit fa4f1ac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 89 deletions.
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>
5 changes: 3 additions & 2 deletions src/huffman.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// // 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 "";
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, huffmanTree) {
return "";
return "Not implemented yet!";
};

// Given a corpus of text, return a Huffman tree that represents the
Expand All @@ -22,6 +22,7 @@ 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();
};

0 comments on commit fa4f1ac

Please sign in to comment.