Skip to content

Commit e88334d

Browse files
committedOct 31, 2015
fix huffman && insertionsort
1 parent 9416ee9 commit e88334d

File tree

5 files changed

+110
-108
lines changed

5 files changed

+110
-108
lines changed
 

Diff for: ‎js-algorithm.js

+106-104
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@
329329
}
330330
this.visited[s] = true
331331
}
332-
332+
333333
for (var i = 0; i < this.adj[s].length; i++) {
334334
queue.push(this.adj[s][i].to);
335335
var totalCost = dList[s] + this.adj[s][i].cost;
@@ -351,10 +351,10 @@
351351
}
352352

353353
/**
354-
*
355-
* LINKED LIST
356-
*
357-
*/
354+
*
355+
* LINKED LIST
356+
*
357+
*/
358358

359359
function LinkedList() {
360360
this.baseNode = {
@@ -466,7 +466,7 @@
466466
}
467467

468468
function insertionSort(arr, callback) {
469-
for (var i = 0; i < arr.length; i++) {
469+
for (var i = 1; i < arr.length; i++) {
470470
var temp = arr[i];
471471
var j = i;
472472
while (j > 0 && callback(arr[j - 1], temp)) {
@@ -749,113 +749,115 @@
749749
}
750750

751751
/**
752-
*
753-
* HUFFMAN
754-
*
755-
*/
756-
757-
function Huffman(text){
758-
this.text = text;
759-
this.coded = {};
760-
this.node = {
761-
right: null,
762-
left: null,
763-
freq: 0,
764-
code:""
765-
};
766-
this.encode = encode;
767-
this.createHuffmanTree = createHuffmanTree;
768-
this.sortByFrequency = sortByFrequency;
769-
this.createFrequencyHash = createFrequencyHash;
770-
this.createBitMap = createBitMap;
771-
this.buildHuffmanCode = buildHuffmanCode;
772-
}
773-
774-
function encode(){
775-
this.textArray = this.text.split("");
776-
var sortedHash = sortByFrequency(this.createFrequencyHash());
777-
var tree = this.createHuffmanTree(sortedHash);
778-
779-
780-
this.createBitMap(tree);
781-
return this.buildHuffmanCode();
782-
}
783-
784-
function createHuffmanTree(elements){
785-
if(elements.length == 1)
786-
return elements[0];
787-
788-
var parent = JSON.parse(JSON.stringify(this.node));
789-
790-
parent.left = elements[0];
791-
parent.right = elements[1];
792-
793-
parent.freq = parent.left.freq + parent.right.freq;
794-
795-
elements.splice(0,2);
796-
elements.push(parent);
797-
798-
return this.createHuffmanTree(this.sortByFrequency(elements));
799-
}
800-
801-
function sortByFrequency(hash){
802-
for(var i = 1 ; i<hash.length ; i++){
803-
var temp = hash[i];
804-
var j = i;
805-
while(j>0 && temp.freq <= hash[j-1].freq){
806-
hash[j] = hash[j-1];
807-
j--;
808-
}
809-
hash[j] = temp;
810-
}
811-
812-
return hash;
813-
}
814-
815-
function createFrequencyHash(){
816-
var freq = [];
817-
var found = false;
818-
var index;
819-
820-
for(var i = 0 ;i < this.textArray.length ; i++){
821-
for(var j = 0 ; j < freq.length ; j++){
822-
if(freq[j].value == this.textArray[i]){
823-
found = true;
824-
index = j;
752+
*
753+
* HUFFMAN
754+
*
755+
*/
756+
757+
function Huffman(text) {
758+
this.text = text;
759+
this.coded = {};
760+
this.node = {
761+
right: null,
762+
left: null,
763+
freq: 0,
764+
code: ""
765+
};
766+
this.encode = encode;
767+
this.createHuffmanTree = createHuffmanTree;
768+
this.sortByFrequency = sortByFrequency;
769+
this.createFrequencyHash = createFrequencyHash;
770+
this.createBitMap = createBitMap;
771+
this.buildHuffmanCode = buildHuffmanCode;
772+
}
773+
774+
function encode() {
775+
this.textArray = this.text.split("");
776+
var sortedHash = sortByFrequency(this.createFrequencyHash());
777+
var tree = this.createHuffmanTree(sortedHash);
778+
this.createBitMap(tree);
779+
return this.buildHuffmanCode();
780+
}
781+
782+
function decode(code){
783+
784+
}
785+
786+
function createHuffmanTree(elements) {
787+
if (elements.length == 1)
788+
return elements[0];
789+
790+
var parent = JSON.parse(JSON.stringify(this.node));
791+
792+
parent.left = elements[0];
793+
parent.right = elements[1];
794+
795+
parent.freq = parent.left.freq + parent.right.freq;
796+
797+
elements.splice(0, 2);
798+
elements.push(parent);
799+
800+
return this.createHuffmanTree(this.sortByFrequency(elements));
801+
}
802+
803+
function sortByFrequency(hash) {
804+
for (var i = 1; i < hash.length; i++) {
805+
var temp = hash[i];
806+
var j = i;
807+
while (j > 0 && temp.freq < hash[j - 1].freq) {
808+
hash[j] = hash[j - 1];
809+
j--;
825810
}
811+
hash[j] = temp;
826812
}
827-
if(found){
828-
freq[index].freq += 1;
829-
found = false;
830-
} else{
831-
freq.push({value: this.textArray[i], freq: 1});
832-
}
813+
814+
return hash;
833815
}
834-
835-
return freq;
836-
}
837816

838-
function createBitMap(tree){
839-
if(!(tree == null) && tree){
840-
if(tree.left != null && (typeof tree.left !== 'undefined')){
841-
tree.left.code = tree.code + "0";
842-
this.coded[tree.left.value] = tree.left.code;
817+
function createFrequencyHash() {
818+
var freq = [];
819+
var found = false;
820+
var index;
821+
822+
for (var i = 0; i < this.textArray.length; i++) {
823+
for (var j = 0; j < freq.length; j++) {
824+
if (freq[j].value == this.textArray[i]) {
825+
found = true;
826+
index = j;
827+
}
828+
}
829+
if (found) {
830+
freq[index].freq += 1;
831+
found = false;
832+
} else {
833+
freq.push({value: this.textArray[i], freq: 1});
834+
}
843835
}
844-
if(tree.right != null && (typeof tree.right !== 'undefined')){
845-
tree.right.code = tree.code + "1";
846-
this.coded[tree.right.value] = tree.right.code;
836+
837+
return freq;
838+
}
839+
840+
function createBitMap(tree) {
841+
if (!(tree == null) && (typeof tree !== 'undefined')) {
842+
if (tree.left != null && (typeof tree.left !== 'undefined')) {
843+
tree.left.code = tree.code + "0";
844+
this.coded[tree.left.value] = tree.left.code;
845+
}
846+
if (tree.right != null && (typeof tree.right !== 'undefined')) {
847+
tree.right.code = tree.code + "1";
848+
this.coded[tree.right.value] = tree.right.code;
849+
}
850+
this.createBitMap(tree.left);
851+
this.createBitMap(tree.right);
847852
}
848-
this.createBitMap(tree.left);
849-
this.createBitMap(tree.right);
850853
}
851-
}
852854

853-
function buildHuffmanCode(){
854-
for(var i = 0 ; i< this.textArray.length ; i++){
855-
this.textArray[i] = this.coded[this.textArray[i]];
855+
function buildHuffmanCode() {
856+
for (var i = 0; i < this.textArray.length; i++) {
857+
this.textArray[i] = this.coded[this.textArray[i]];
858+
}
859+
return this.textArray.join("");
856860
}
857-
return this.textArray.join("");
858-
}
859861

860862

861863
var full = {

Diff for: ‎js-algorithm.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎js-algorithm.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"licence": "MIT",
2222
"main": "js-algorithm.js",
23-
"version": "0.0.4",
23+
"version": "0.0.5",
2424
"repository": {
2525
"type": "git",
2626
"url": "https://github.com/omeroot/js-algorithms"

Diff for: ‎test.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
</head>
55
<body>
6-
<script src="js-algorithm.js"></script>
6+
<script src="js-algorithm.min.js"></script>
77
</body>
88
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.