Skip to content

Commit

Permalink
Merge pull request #19 from Yoyo2Code/huffman_coding_syntax
Browse files Browse the repository at this point in the history
Huffman Coding syntax
  • Loading branch information
rrgayhart committed Dec 9, 2016
2 parents 08de739 + f06bcf2 commit b4d9adf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 9 additions & 9 deletions huffman_coding/javascript/compression.js
Expand Up @@ -8,11 +8,11 @@ function Leaf(character, count) {

Leaf.prototype.encoderObject = function(parentBits) {
return {[this.character]: parentBits}
}
};

Leaf.prototype.unsetParents = function() {
return this;
}
};

/////////////////////////////
//// (>’.’)> NODE <(‘.'<) ///
Expand All @@ -30,13 +30,13 @@ Node.prototype.encoderObject = function(parentBits = "") {
var leftEncoderObject = this.left.encoderObject(parentBits + "0");
var rightEncoderObject = this.right.encoderObject(parentBits + "1");
return _.extend(leftEncoderObject, rightEncoderObject);
}
};

Node.prototype.unsetParents = function() {
delete this.left.unsetParents().parent;
delete this.right.unsetParents().parent;
return this;
}
};

///////////////////////////////////
//// (⌐■_■) ENCODER (⌐■_■) ////
Expand All @@ -55,13 +55,13 @@ function Encoder(message) {

var nodeQueue = _.map(characterCounts, function(count, character) {
return new Leaf(character, count);
})
});

encoder.leaves = nodeQueue.slice(0);

while(nodeQueue.length > 1) {
nodeQueue = _.sortBy(nodeQueue, 'count');
newNode = new Node(nodeQueue.shift(), nodeQueue.shift())
newNode = new Node(nodeQueue.shift(), nodeQueue.shift());
newNode.left.parent = newNode.right.parent = newNode;
nodeQueue.push(newNode);
}
Expand All @@ -73,13 +73,13 @@ function Encoder(message) {

Encoder.prototype.characterToCode = function(character) {
return this.root.encoderObject()[character];
}
};

Encoder.prototype.decode = function(compressedBitstring) {

// TODO: All the awesome

}
};

///////////////////////////////////
//// ><((((‘> DECODER <`))))>< ////
Expand All @@ -92,4 +92,4 @@ function Decoder(compressedBitstring, rootNode) {
Decoder.prototype.message = function(){
// TODO: It
// YOU CAN DO IT!!
}
};
8 changes: 4 additions & 4 deletions huffman_coding/javascript/test.js
Expand Up @@ -29,7 +29,7 @@ describe('compression', function() {
it('has a count equal to the sum of the counts of its leaves', function() {
var node = new Node(new Leaf("!", 1), new Leaf("@", 2));
assert.equal(node.count, 3);
})
});

it('returns itself as an object', function() {
var leftLeaf = new Leaf("J", 1);
Expand All @@ -42,7 +42,7 @@ describe('compression', function() {

context('rootNode', function() {
it('has a count equal to the message length', function() {
assert.equal(encoder.root.count, message.length)
assert.equal(encoder.root.count, message.length);
});

it('has children that know about their parents', function() {
Expand All @@ -64,7 +64,7 @@ describe('compression', function() {
var numCharacters = _.uniq(message.split("")).length;
assert.equal(numCharacters, encoder.leaves.length);
encoder.leaves.forEach(function(leaf) {
assert.instanceOf(leaf, Leaf)
assert.instanceOf(leaf, Leaf);
});

});
Expand All @@ -81,7 +81,7 @@ describe('compression', function() {
});

it('can tell me the compressed bitstring', function() {
var compressed = "100001000111110000110001110111001011010010010010111010011110011100001100011101111110001101001111101011011011000100011110000111010111010111"
var compressed = "100001000111110000110001110111001011010010010010111010011110011100001100011101111110001101001111101011011011000100011110000111010111010111";
assert.equal(encoder.compressedBitstring, compressed);
});

Expand Down

0 comments on commit b4d9adf

Please sign in to comment.