Socket
Socket
Sign inDemoInstall

javascript-algorithms

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.4 to 0.0.5

188

js-algorithm.js

@@ -332,3 +332,3 @@ /*

}
for (var i = 0; i < this.adj[s].length; i++) {

@@ -355,6 +355,6 @@ queue.push(this.adj[s][i].to);

/**
*
* LINKED LIST
*
*/
*
* LINKED LIST
*
*/

@@ -471,3 +471,3 @@ function LinkedList() {

function insertionSort(arr, callback) {
for (var i = 0; i < arr.length; i++) {
for (var i = 1; i < arr.length; i++) {
var temp = arr[i];

@@ -755,109 +755,111 @@ var j = i;

/**
*
* HUFFMAN
*
*/
*
* HUFFMAN
*
*/
function Huffman(text){
this.text = text;
this.coded = {};
this.node = {
right: null,
left: null,
freq: 0,
code:""
};
this.encode = encode;
this.createHuffmanTree = createHuffmanTree;
this.sortByFrequency = sortByFrequency;
this.createFrequencyHash = createFrequencyHash;
this.createBitMap = createBitMap;
this.buildHuffmanCode = buildHuffmanCode;
}
function Huffman(text) {
this.text = text;
this.coded = {};
this.node = {
right: null,
left: null,
freq: 0,
code: ""
};
this.encode = encode;
this.createHuffmanTree = createHuffmanTree;
this.sortByFrequency = sortByFrequency;
this.createFrequencyHash = createFrequencyHash;
this.createBitMap = createBitMap;
this.buildHuffmanCode = buildHuffmanCode;
}
function encode(){
this.textArray = this.text.split("");
var sortedHash = sortByFrequency(this.createFrequencyHash());
var tree = this.createHuffmanTree(sortedHash);
this.createBitMap(tree);
return this.buildHuffmanCode();
}
function encode() {
this.textArray = this.text.split("");
var sortedHash = sortByFrequency(this.createFrequencyHash());
var tree = this.createHuffmanTree(sortedHash);
this.createBitMap(tree);
return this.buildHuffmanCode();
}
function createHuffmanTree(elements){
if(elements.length == 1)
return elements[0];
function decode(code){
var parent = JSON.parse(JSON.stringify(this.node));
}
parent.left = elements[0];
parent.right = elements[1];
function createHuffmanTree(elements) {
if (elements.length == 1)
return elements[0];
parent.freq = parent.left.freq + parent.right.freq;
var parent = JSON.parse(JSON.stringify(this.node));
elements.splice(0,2);
elements.push(parent);
parent.left = elements[0];
parent.right = elements[1];
return this.createHuffmanTree(this.sortByFrequency(elements));
}
parent.freq = parent.left.freq + parent.right.freq;
function sortByFrequency(hash){
for(var i = 1 ; i<hash.length ; i++){
var temp = hash[i];
var j = i;
while(j>0 && temp.freq <= hash[j-1].freq){
hash[j] = hash[j-1];
j--;
elements.splice(0, 2);
elements.push(parent);
return this.createHuffmanTree(this.sortByFrequency(elements));
}
function sortByFrequency(hash) {
for (var i = 1; i < hash.length; i++) {
var temp = hash[i];
var j = i;
while (j > 0 && temp.freq < hash[j - 1].freq) {
hash[j] = hash[j - 1];
j--;
}
hash[j] = temp;
}
hash[j] = temp;
return hash;
}
return hash;
}
function createFrequencyHash(){
var freq = [];
var found = false;
var index;
for(var i = 0 ;i < this.textArray.length ; i++){
for(var j = 0 ; j < freq.length ; j++){
if(freq[j].value == this.textArray[i]){
found = true;
index = j;
function createFrequencyHash() {
var freq = [];
var found = false;
var index;
for (var i = 0; i < this.textArray.length; i++) {
for (var j = 0; j < freq.length; j++) {
if (freq[j].value == this.textArray[i]) {
found = true;
index = j;
}
}
if (found) {
freq[index].freq += 1;
found = false;
} else {
freq.push({value: this.textArray[i], freq: 1});
}
}
if(found){
freq[index].freq += 1;
found = false;
} else{
freq.push({value: this.textArray[i], freq: 1});
}
return freq;
}
return freq;
}
function createBitMap(tree){
if(!(tree == null) && tree){
if(tree.left != null && (typeof tree.left !== 'undefined')){
tree.left.code = tree.code + "0";
this.coded[tree.left.value] = tree.left.code;
function createBitMap(tree) {
if (!(tree == null) && (typeof tree !== 'undefined')) {
if (tree.left != null && (typeof tree.left !== 'undefined')) {
tree.left.code = tree.code + "0";
this.coded[tree.left.value] = tree.left.code;
}
if (tree.right != null && (typeof tree.right !== 'undefined')) {
tree.right.code = tree.code + "1";
this.coded[tree.right.value] = tree.right.code;
}
this.createBitMap(tree.left);
this.createBitMap(tree.right);
}
if(tree.right != null && (typeof tree.right !== 'undefined')){
tree.right.code = tree.code + "1";
this.coded[tree.right.value] = tree.right.code;
}
this.createBitMap(tree.left);
this.createBitMap(tree.right);
}
}
function buildHuffmanCode(){
for(var i = 0 ; i< this.textArray.length ; i++){
this.textArray[i] = this.coded[this.textArray[i]];
function buildHuffmanCode() {
for (var i = 0; i < this.textArray.length; i++) {
this.textArray[i] = this.coded[this.textArray[i]];
}
return this.textArray.join("");
}
return this.textArray.join("");
}

@@ -864,0 +866,0 @@

@@ -1,2 +0,2 @@

(function(t){function e(){this.baseNode={data:null,right:null,left:null};this.root=null;this.insert=i;this.inOrder=o;this.preOrder=l;this.postOrder=f;this.getMin=r;this.getMax=n;this.find=s;this.remove=a;this.removeNode=h}function i(t,e){var i=JSON.parse(JSON.stringify(this.baseNode));i.data=t;if(this.root==null){this.root=i}else{var r=this.root;while(true){if(e(r.data,i.data)){if(r.left==null){r.left=i;break}else{r=r.left;continue}}else{if(r.right==null){r.right=i;break}else{r=r.right;continue}}}}return i}function r(){var t=this.root;var e=t.data;while(!(t==null)){e=t.data;t=t.left}return e}function n(){var t=this.root;var e;while(!(t==null)){e=t.data;t=t.right}return e}function s(t,e){var i=this.root;var r;while(e(i.data)!=t){r=i;if(e(i.data)>t){i=i.left}else{i=i.right}if(i==null){return null}}return{parent:r,current:i}}function a(t){var e=this.removeNode(this.root,t);return e}function h(t,e){if(t==null){return null}if(t.data==e){if(t.left==null&&t.right==null){return null}if(t.left==null){return t.right}if(t.right==null){return t.left}var i=r(t.right);t.data=i.data;t.right=h(t.right,i.data);return t}else if(e<t.data){t.left=h(t.left,e);return t}else if(e>t.data){h(t.right,e);return t}function r(t){if(t.left==null){return t}else{r(t.left)}}}function o(t){if(!(t==null)){o(t.left);process.stdout.write(t.data+" ");o(t.right)}}function l(t){if(!(t==null)){process.stdout.write(t.data+" ");l(t.left);l(t.right)}}function f(t){if(!(t==null)){f(t.left);f(t.right);process.stdout.write(t.data+" ")}}function u(t){this.adj={};this.visited={};this.edgeTo=[];this.edges=0;this.addEdge=d;this.display=c;this.clearVisitedList=p;this.depthFirstSearch=v;this.breadthFirstSearch=g;this.dijkstra=y}function d(t,e,i){if(!this.adj.hasOwnProperty(t)){this.adj[t]=[];this.visited[t]=false}if(!this.adj.hasOwnProperty(e)){this.adj[e]=[];this.visited[e]=false}this.adj[t].push({to:e,cost:i});this.adj[e].push({to:t,cost:i});this.edges++}function c(){for(var t in this.adj){process.stdout.write(t+"--> ");for(var e=0;e<this.adj[t].length;e++){process.stdout.write(this.adj[t][e].to+" ")}process.stdout.write("\n")}}function v(t){if(this.adj.hasOwnProperty(t)){this.visited[t]=true}for(var e=0;e<this.adj[t].length;e++){if(this.visited[this.adj[t][e].to]==false){this.depthFirstSearch(this.adj[t][e].to)}}}function g(t){var e=[];e.push(t);while(e.length>0){var i=e.shift();if(this.visited[i]==false){this.visited[i]=true}for(var r=0;r<this.adj[i].length;r++){if(!this.visited[this.adj[i][r].to]){e.push(this.adj[i][r].to);this.edgeTo[this.adj[i][r].to]=i}}}}function p(){for(var t in this.visited){this.visited[t]=false}}function y(t,e){var i={};var r={};var n=[];var s={};var a=0;n.push(t);for(var h in this.adj){i[h]=Infinity;s[h]=[]}while(n.length>0){var o=n.shift();if(this.visited[o]==false){a++;if(i[o]==Infinity){i[o]=0;s[o].push(o)}this.visited[o]=true}for(var l=0;l<this.adj[o].length;l++){n.push(this.adj[o][l].to);var f=i[o]+this.adj[o][l].cost;if(f<i[this.adj[o][l].to]){s[this.adj[o][l].to]=s[o].slice();s[this.adj[o][l].to].push(this.adj[o][l].to);i[this.adj[o][l].to]=i[o]+this.adj[o][l].cost;r=i}}if(a%7==0&&r==i){break}}return s[e]}function w(){this.baseNode={element:null,next:null};this.head=new Node("head");this.current=this.head;this.insert=j;this.remove=S;this.find=m;this.display=b()}function j(t){var e=JSON.parse(JSON.stringify(t));this.current.next=e;e.next=null;this.current=e}function m(t){var e=this.head.next;while(t(e.element)){e=e.next}return e}function b(){var t=this.head;do{console.log(t.next.element);t=t.next}while(t.next!=null)}function S(t){var e=this.head.next;var i=e;while(!t(e.element)){i=e;e=e.next}i.next=e.next;return e}function N(){this.CreateRandomArray=O;this.swap=A;this.bubbleSort=q;this.selectionSorting=H;this.shellSort=I;this.insertionSort=M;this.mergeSort=J;this.quickSort=k}function O(t){var e=[];for(var i=0;i<t;i++){e[i]=Math.floor(Math.random()*(t*20+1))}return e}function A(t,e,i){var r=t[e];t[e]=t[i];t[i]=r}function q(t,e){for(var i=t.length;i>=2;--i){for(var r=0;r<i-1;++r){if(e(t[r],t[r+1])){this.swap(t,r,r+1)}}}return t}function H(t,e){for(var i=0;i<t.length-1;i++){for(var r=i+1;r<t.length;r++){if(e(t[i],t[r])){this.swap(t,i,r)}}}return t}function M(t,e){for(var i=0;i<t.length;i++){var r=t[i];var n=i;while(n>0&&e(t[n-1],r)){t[n]=t[n-1];n=n-1}t[n]=r}return t}function I(t,e){var i=parseInt(t.length/2);while(i>0){for(var r=i;r<t.length;r++){var n=t[r];for(var s=r;s>=i&&e(n,t[s-i]);s-=i){t[s]=t[s-i]}t[s]=n}i=parseInt(i/2)}return t}function J(t,e){var i=1;var r,n;while(i<t.length){r=0;n=i;while(n+i<=t.length){s(t,r,r+i,n,n+i,e);r=n+i;n=r+i}if(n<t.length){s(t,r,r+i,n,t.length,e)}i*=2}function s(t,e,i,r,n,s){var a=new Array(i-e+1);var h=new Array(n-r+1);x=e;for(var o=0;o<a.length-1;++o){a[o]=t[x];++x}x=r;for(var o=0;o<h.length-1;++o){h[o]=t[x];++x}a[a.length-1]=Infinity;h[h.length-1]=Infinity;var l=0;var f=0;var u;var d;for(var c=e;c<n;++c){if((a[l]==Infinity?Infinity:s(a[l]))<=(h[f]==Infinity?Infinity:s(h[f]))){t[c]=a[l];l++}else{t[c]=h[f];f++}}}return t}function k(t,e){if(t.length==0){return[]}var i=[];var r=[];var n=t[0];for(var s=1;s<t.length;s++){if(e(t[s],n)){i.push(t[s])}else{r.push(t[s])}}var a=k(i,e).concat(n,k(r,e));return a}function T(){this.baseNode={element:null,next:null,prev:null};this.head=new Node("head");this.current=this.head;this.insert=B;this.remove=C;this.display=L;this.find=F}function B(t){var e=JSON.parse(JSON.stringify(this.baseNode));e.element=t;this.current.next=e;e.next=null;e.prev=this.current;this.current=e}function F(t){var e=this.head.next;while(!t(e.element)){e=e.next}return e}function C(t){var e=this.find(t);e.prev.next=e.next;if(e.next!=null){e.next.prev=e.prev}return e}function L(){var t=this.head.next;while(t!=null){console.log(t.element);t=t.next}}var P;function E(t){P=t.size;this.table=new Array(P);this.simpleHash=G;this.display=z;this.put=V;this.get=_;this.buildChains=D;this.buildChains()}function V(t){var e=this.simpleHash(t);var i=false;if(this.table[e][0]==undefined){this.table[e][0]=t}else{for(var r=1;r<this.table[e].length+1;r++){if(this.table[e][r]==undefined){this.table[e][r]=t;break}}}}function _(t){return this.table[this.simpleHash(t)]}function z(){console.log("Address | Value");console.log("----------- -----------");for(var t=0;t<P;t++){var e=this.table[t];if(e.length!=0){process.stdout.write(t.toString());for(var i=0;i<11-t.toString().length;i++){process.stdout.write(" ")}process.stdout.write("| ");for(var r=0;r<e.length;r++){process.stdout.write(JSON.stringify(e[r])+" ")}process.stdout.write("\n");console.log("----------- -----------")}}}function D(){for(var t=0;t<P;t++){this.table[t]=new Array}}function G(t){if(typeof t==="string"){return K(t)}else if(Object.prototype.toString.apply(t)==="[object Object]"){return K(JSON.stringify(t))}else if(Object.prototype.toString.apply(t)==="[object Array]"){return undefined}else if(typeof t==="number"){return R()}else{throw new TypeError("unexpected data type","hashTable.js",24)}}function R(){var t="";for(var e=1;e<=9;e++){t+=Math.floor(Math.random()*10)}t+=i(50,100);function i(t,e){return Math.floor(Math.random()*(e-t+1))}return t}function K(t){var e=0;const i=37;for(var r=0;r<t.length;r++){e+=e*i+t.charCodeAt(r)}e=e%P;if(e<0){e+=P-1}return parseInt(e)}function Q(t){this.text=t;this.coded={};this.node={right:null,left:null,freq:0,code:""};this.encode=U;this.createHuffmanTree=W;this.sortByFrequency=X;this.createFrequencyHash=Y;this.createBitMap=Z;this.buildHuffmanCode=$}function U(){this.textArray=this.text.split("");var t=X(this.createFrequencyHash());var e=this.createHuffmanTree(t);this.createBitMap(e);return this.buildHuffmanCode()}function W(t){if(t.length==1)return t[0];var e=JSON.parse(JSON.stringify(this.node));e.left=t[0];e.right=t[1];e.freq=e.left.freq+e.right.freq;t.splice(0,2);t.push(e);return this.createHuffmanTree(this.sortByFrequency(t))}function X(t){for(var e=1;e<t.length;e++){var i=t[e];var r=e;while(r>0&&i.freq<=t[r-1].freq){t[r]=t[r-1];r--}t[r]=i}return t}function Y(){var t=[];var e=false;var i;for(var r=0;r<this.textArray.length;r++){for(var n=0;n<t.length;n++){if(t[n].value==this.textArray[r]){e=true;i=n}}if(e){t[i].freq+=1;e=false}else{t.push({value:this.textArray[r],freq:1})}}return t}function Z(t){if(!(t==null)&&t){if(t.left!=null&&typeof t.left!=="undefined"){t.left.code=t.code+"0";this.coded[t.left.value]=t.left.code}if(t.right!=null&&typeof t.right!=="undefined"){t.right.code=t.code+"1";this.coded[t.right.value]=t.right.code}this.createBitMap(t.left);this.createBitMap(t.right)}}function $(){for(var t=0;t<this.textArray.length;t++){this.textArray[t]=this.coded[this.textArray[t]]}return this.textArray.join("")}var tt={BinarySearchTree:e,Graph:u,Sorting:N,Hash:E,DoublyLinkedList:T,LinkedList:w,Huffman:Q};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=tt}exports.js_algorithms=tt}else{t.js_algorithms=tt}if(typeof define==="function"&&define.amd){define("javascript-algorithms",[],function(){return tt})}})(this);
(function(t){function e(){this.baseNode={data:null,right:null,left:null};this.root=null;this.insert=i;this.inOrder=o;this.preOrder=l;this.postOrder=f;this.getMin=r;this.getMax=n;this.find=s;this.remove=a;this.removeNode=h}function i(t,e){var i=JSON.parse(JSON.stringify(this.baseNode));i.data=t;if(this.root==null){this.root=i}else{var r=this.root;while(true){if(e(r.data,i.data)){if(r.left==null){r.left=i;break}else{r=r.left;continue}}else{if(r.right==null){r.right=i;break}else{r=r.right;continue}}}}return i}function r(){var t=this.root;var e=t.data;while(!(t==null)){e=t.data;t=t.left}return e}function n(){var t=this.root;var e;while(!(t==null)){e=t.data;t=t.right}return e}function s(t,e){var i=this.root;var r;while(e(i.data)!=t){r=i;if(e(i.data)>t){i=i.left}else{i=i.right}if(i==null){return null}}return{parent:r,current:i}}function a(t){var e=this.removeNode(this.root,t);return e}function h(t,e){if(t==null){return null}if(t.data==e){if(t.left==null&&t.right==null){return null}if(t.left==null){return t.right}if(t.right==null){return t.left}var i=r(t.right);t.data=i.data;t.right=h(t.right,i.data);return t}else if(e<t.data){t.left=h(t.left,e);return t}else if(e>t.data){h(t.right,e);return t}function r(t){if(t.left==null){return t}else{r(t.left)}}}function o(t){if(!(t==null)){o(t.left);process.stdout.write(t.data+" ");o(t.right)}}function l(t){if(!(t==null)){process.stdout.write(t.data+" ");l(t.left);l(t.right)}}function f(t){if(!(t==null)){f(t.left);f(t.right);process.stdout.write(t.data+" ")}}function u(t){this.adj={};this.visited={};this.edgeTo=[];this.edges=0;this.addEdge=d;this.display=c;this.clearVisitedList=p;this.depthFirstSearch=v;this.breadthFirstSearch=g;this.dijkstra=y}function d(t,e,i){if(!this.adj.hasOwnProperty(t)){this.adj[t]=[];this.visited[t]=false}if(!this.adj.hasOwnProperty(e)){this.adj[e]=[];this.visited[e]=false}this.adj[t].push({to:e,cost:i});this.adj[e].push({to:t,cost:i});this.edges++}function c(){for(var t in this.adj){process.stdout.write(t+"--> ");for(var e=0;e<this.adj[t].length;e++){process.stdout.write(this.adj[t][e].to+" ")}process.stdout.write("\n")}}function v(t){if(this.adj.hasOwnProperty(t)){this.visited[t]=true}for(var e=0;e<this.adj[t].length;e++){if(this.visited[this.adj[t][e].to]==false){this.depthFirstSearch(this.adj[t][e].to)}}}function g(t){var e=[];e.push(t);while(e.length>0){var i=e.shift();if(this.visited[i]==false){this.visited[i]=true}for(var r=0;r<this.adj[i].length;r++){if(!this.visited[this.adj[i][r].to]){e.push(this.adj[i][r].to);this.edgeTo[this.adj[i][r].to]=i}}}}function p(){for(var t in this.visited){this.visited[t]=false}}function y(t,e){var i={};var r={};var n=[];var s={};var a=0;n.push(t);for(var h in this.adj){i[h]=Infinity;s[h]=[]}while(n.length>0){var o=n.shift();if(this.visited[o]==false){a++;if(i[o]==Infinity){i[o]=0;s[o].push(o)}this.visited[o]=true}for(var l=0;l<this.adj[o].length;l++){n.push(this.adj[o][l].to);var f=i[o]+this.adj[o][l].cost;if(f<i[this.adj[o][l].to]){s[this.adj[o][l].to]=s[o].slice();s[this.adj[o][l].to].push(this.adj[o][l].to);i[this.adj[o][l].to]=i[o]+this.adj[o][l].cost;r=i}}if(a%7==0&&r==i){break}}return s[e]}function w(){this.baseNode={element:null,next:null};this.head=new Node("head");this.current=this.head;this.insert=j;this.remove=S;this.find=m;this.display=b()}function j(t){var e=JSON.parse(JSON.stringify(t));this.current.next=e;e.next=null;this.current=e}function m(t){var e=this.head.next;while(t(e.element)){e=e.next}return e}function b(){var t=this.head;do{console.log(t.next.element);t=t.next}while(t.next!=null)}function S(t){var e=this.head.next;var i=e;while(!t(e.element)){i=e;e=e.next}i.next=e.next;return e}function N(){this.CreateRandomArray=O;this.swap=A;this.bubbleSort=q;this.selectionSorting=H;this.shellSort=I;this.insertionSort=M;this.mergeSort=J;this.quickSort=k}function O(t){var e=[];for(var i=0;i<t;i++){e[i]=Math.floor(Math.random()*(t*20+1))}return e}function A(t,e,i){var r=t[e];t[e]=t[i];t[i]=r}function q(t,e){for(var i=t.length;i>=2;--i){for(var r=0;r<i-1;++r){if(e(t[r],t[r+1])){this.swap(t,r,r+1)}}}return t}function H(t,e){for(var i=0;i<t.length-1;i++){for(var r=i+1;r<t.length;r++){if(e(t[i],t[r])){this.swap(t,i,r)}}}return t}function M(t,e){for(var i=1;i<t.length;i++){var r=t[i];var n=i;while(n>0&&e(t[n-1],r)){t[n]=t[n-1];n=n-1}t[n]=r}return t}function I(t,e){var i=parseInt(t.length/2);while(i>0){for(var r=i;r<t.length;r++){var n=t[r];for(var s=r;s>=i&&e(n,t[s-i]);s-=i){t[s]=t[s-i]}t[s]=n}i=parseInt(i/2)}return t}function J(t,e){var i=1;var r,n;while(i<t.length){r=0;n=i;while(n+i<=t.length){s(t,r,r+i,n,n+i,e);r=n+i;n=r+i}if(n<t.length){s(t,r,r+i,n,t.length,e)}i*=2}function s(t,e,i,r,n,s){var a=new Array(i-e+1);var h=new Array(n-r+1);x=e;for(var o=0;o<a.length-1;++o){a[o]=t[x];++x}x=r;for(var o=0;o<h.length-1;++o){h[o]=t[x];++x}a[a.length-1]=Infinity;h[h.length-1]=Infinity;var l=0;var f=0;var u;var d;for(var c=e;c<n;++c){if((a[l]==Infinity?Infinity:s(a[l]))<=(h[f]==Infinity?Infinity:s(h[f]))){t[c]=a[l];l++}else{t[c]=h[f];f++}}}return t}function k(t,e){if(t.length==0){return[]}var i=[];var r=[];var n=t[0];for(var s=1;s<t.length;s++){if(e(t[s],n)){i.push(t[s])}else{r.push(t[s])}}var a=k(i,e).concat(n,k(r,e));return a}function T(){this.baseNode={element:null,next:null,prev:null};this.head=new Node("head");this.current=this.head;this.insert=B;this.remove=C;this.display=L;this.find=F}function B(t){var e=JSON.parse(JSON.stringify(this.baseNode));e.element=t;this.current.next=e;e.next=null;e.prev=this.current;this.current=e}function F(t){var e=this.head.next;while(!t(e.element)){e=e.next}return e}function C(t){var e=this.find(t);e.prev.next=e.next;if(e.next!=null){e.next.prev=e.prev}return e}function L(){var t=this.head.next;while(t!=null){console.log(t.element);t=t.next}}var P;function E(t){P=t.size;this.table=new Array(P);this.simpleHash=G;this.display=z;this.put=V;this.get=_;this.buildChains=D;this.buildChains()}function V(t){var e=this.simpleHash(t);var i=false;if(this.table[e][0]==undefined){this.table[e][0]=t}else{for(var r=1;r<this.table[e].length+1;r++){if(this.table[e][r]==undefined){this.table[e][r]=t;break}}}}function _(t){return this.table[this.simpleHash(t)]}function z(){console.log("Address | Value");console.log("----------- -----------");for(var t=0;t<P;t++){var e=this.table[t];if(e.length!=0){process.stdout.write(t.toString());for(var i=0;i<11-t.toString().length;i++){process.stdout.write(" ")}process.stdout.write("| ");for(var r=0;r<e.length;r++){process.stdout.write(JSON.stringify(e[r])+" ")}process.stdout.write("\n");console.log("----------- -----------")}}}function D(){for(var t=0;t<P;t++){this.table[t]=new Array}}function G(t){if(typeof t==="string"){return K(t)}else if(Object.prototype.toString.apply(t)==="[object Object]"){return K(JSON.stringify(t))}else if(Object.prototype.toString.apply(t)==="[object Array]"){return undefined}else if(typeof t==="number"){return R()}else{throw new TypeError("unexpected data type","hashTable.js",24)}}function R(){var t="";for(var e=1;e<=9;e++){t+=Math.floor(Math.random()*10)}t+=i(50,100);function i(t,e){return Math.floor(Math.random()*(e-t+1))}return t}function K(t){var e=0;const i=37;for(var r=0;r<t.length;r++){e+=e*i+t.charCodeAt(r)}e=e%P;if(e<0){e+=P-1}return parseInt(e)}function Q(t){this.text=t;this.coded={};this.node={right:null,left:null,freq:0,code:""};this.encode=U;this.createHuffmanTree=X;this.sortByFrequency=Y;this.createFrequencyHash=Z;this.createBitMap=$;this.buildHuffmanCode=tt}function U(){this.textArray=this.text.split("");var t=Y(this.createFrequencyHash());var e=this.createHuffmanTree(t);this.createBitMap(e);return this.buildHuffmanCode()}function W(t){}function X(t){if(t.length==1)return t[0];var e=JSON.parse(JSON.stringify(this.node));e.left=t[0];e.right=t[1];e.freq=e.left.freq+e.right.freq;t.splice(0,2);t.push(e);return this.createHuffmanTree(this.sortByFrequency(t))}function Y(t){for(var e=1;e<t.length;e++){var i=t[e];var r=e;while(r>0&&i.freq<t[r-1].freq){t[r]=t[r-1];r--}t[r]=i}return t}function Z(){var t=[];var e=false;var i;for(var r=0;r<this.textArray.length;r++){for(var n=0;n<t.length;n++){if(t[n].value==this.textArray[r]){e=true;i=n}}if(e){t[i].freq+=1;e=false}else{t.push({value:this.textArray[r],freq:1})}}return t}function $(t){if(!(t==null)&&typeof t!=="undefined"){if(t.left!=null&&typeof t.left!=="undefined"){t.left.code=t.code+"0";this.coded[t.left.value]=t.left.code}if(t.right!=null&&typeof t.right!=="undefined"){t.right.code=t.code+"1";this.coded[t.right.value]=t.right.code}this.createBitMap(t.left);this.createBitMap(t.right)}}function tt(){for(var t=0;t<this.textArray.length;t++){this.textArray[t]=this.coded[this.textArray[t]]}return this.textArray.join("")}var et={BinarySearchTree:e,Graph:u,Sorting:N,Hash:E,DoublyLinkedList:T,LinkedList:w,Huffman:Q};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=et}exports.js_algorithms=et}else{t.js_algorithms=et}if(typeof define==="function"&&define.amd){define("javascript-algorithms",[],function(){return et})}})(this);
//# sourceMappingURL=js-algorithm.min.js.map

@@ -23,3 +23,3 @@ {

"main": "js-algorithm.js",
"version": "0.0.4",
"version": "0.0.5",
"repository": {

@@ -26,0 +26,0 @@ "type": "git",

@@ -133,1 +133,7 @@ # js-algorithms

```
##Huffman
```js
var huffman = new js_algorithms.Huffman("omer demircan");
huffman.encode();//returned : 1011101100...
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc