2

Tengo un codigo en js sobre un "hashing abierto" que se me esta colgando , creo que es por la funcion de 'Insert_in_bucket(obj,pos)' y no estoy encontrando la solucion.

let node=function(){
this.key='';
this.word='';

this.getKey=function(){
    return this.key;
}
this.getWord=function(){
    return this.word;
}
this.setKey=function(key){
    this.key=key;
}
this.setWord=function(word){
    this.word=word;
}

}


function hash(word){
    let key=[],i,j=0;
    key[j]=word[0];
    j++;
    for(i=1;i<word.length && j<4;i++){
        if(word[i]=='b' || word[i]=='p' || word[i]=='f' || word[i]=='v'){
            key[j]='1';
            j++;
        }else{
            if(word[i]=='c' || word[i]=='g' || word[i]=='j' || word[i]=='k' || word[i]=='q' || word[i]=='s' || word[i]=='x' || word[i]=='z'){
                key[j]='2';
                j++;
            }else{
                if(word[i]=='d' || word[i]=='t'){
                    key[j]='3';
                    j++;
                }else{
                    if(word[i]=='l'){
                        key[j]='4';
                        j++;
                    }else{
                        if(word[i]=='m'|| word[i]=='n'){
                            key[j]='5';
                            j++;
                        }else{
                            if(word[i]=='r'){
                                key[j]='6';
                                j++;
                            }
                        }

                    }
                }
            }
        }
    }
    if(key.length<4){
        for(i=key.length;i<4;i++){key[j]='0';}
    }
let ans=key.join('');
return ans;

}

function insert(obj){
    let key=obj.getKey();
    let pos=0;

    if(table.length==0){
        table[0]=new Array(1);
        table[0][0]=new node();
        table[0][0]=obj;
    }else{
        for(i=0;i<table.length;i++){
            if(table[i][0].getKey()==key){
                pos=i;
                break;
            }
        }
        console.log(i)
        pos=i;
        if(pos==table.length){
            insert_in_array(obj);
        }else{
            insert_in_bucket(obj,pos);
        }
    }

}
function insert_in_bucket(obj,pos){
    let l=table[pos].length;
    table[pos][l]=new node();
    table[pos][l]=obj;
}
function insert_in_array(obj){
    let l=table.length;

    table[l]=new Array(1);
    table[l][0]=new node();
    table[l][0]=obj;
}



let table=[];
let Node=new node();
let pal=['Departamento','Almacenero','Deporte'];
for(i=0;i<pal.length;i++){

    Node.setWord(pal[i]);
    Node.setKey(hash(pal[i]));
    insert(Node);
    Node=new node();

}

for(j=0;j<table.length;j++){
    for(k=0;k<table[j].length;k++){
        console.log('Node: ',table[j][k].getWord(),table[j][k].getKey());
    }
    console.log('End of Bucket ',j);
}

Al ejecutarlo con Node.js se cuelga mostrando, con la instruccion "console.log(i)" dentro de la funcion "insert", 0 y 1 alternados si no me equivoco.

JackNavaRow
  • 6,836
  • 5
  • 22
  • 49

1 Answers1

1

Tu codigo luce un poco confuso, declaras variables con el mismo nombre y esto puede ser el problema (un ejemplo ver la variable i) y esto te este dando problemas al alcance de las variables (scope)

mi alternativa de solucion con menos codigo solo cambie los metodos de 'Insert_in_bucket(obj,pos)' e insert_in_array cada uno de ellos lo que hace es insertar y actualizar. el metodo hash lo simplifique con el metodo includes quedo de la siguiente forma:

"use strict"
const Node =function(word , key){
    this.key='';
    this.word='';

    this.getKey=function(){
        return this.key;
    }
    this.getWord=function(){
        return this.word;
    }
    this.setKey=function(key){
        this.key=key;
    }
    this.setWord=function(word){
        this.word=word;
    }

    }


    function insert(obj){
        let key=obj.getKey();
        let update=false;
        for(let i=0;i<arrayNode.length;i++){

          if(arrayNode[i].getKey()==key){
            //update
              update = true;
              arrayNode[i] = obj;
          }
         }
        if (!update){
          arrayNode.push(obj)
        }
        }
    function hash(word){
     const  key=[];
     key[0]=word[0];
     let  j = 1
     const arrayWords = ['' , 'bpfv', 'cgjkqsxz', 'dt', 'l', 'mn', 'r'];
        for(let i=1;i<word.length && j<4;i++){
            if(arrayWords[1].includes(word[i])){
              key[j] = '1';
              j++;
              continue;
            }
            if(arrayWords[2].includes(word[i])){
              key[j] = '2';
              j++;
              continue;
            }
            if(arrayWords[3].includes(word[i])){
              key[j] = '3';
              j++;
              continue;
            }
            if(arrayWords[4].includes(word[i])){
              key[j] = '4';
              j++;
              continue;
            }
            if(arrayWords[5].includes(word[i])){
              key[j] = '5';
              j++;
              continue;
            }
            if(arrayWords[6].includes(word[i])){
              key[j] = '6';
              j++;
              continue;
            }
        }
        return key.join('')
    }
    const arrayNode = [];
    const pal=['Departamento','Almacenero','Deporte'];
    for(let i=0;i<pal.length;i++){

        const node= new Node();
        node.setWord(pal[i]);
        node.setKey(hash(pal[i]));
        insert(node);
      
    }
    for(let i in arrayNode){
      console.info(`la palabra ${arrayNode[i].getWord()} su llave es ${arrayNode[i].getKey()}`)
    }

Intencionalmente deje el codigo de hash que se viera constantemente repetido para que sea un poco mas facil de entender, pero puede quedar de la siguiente manera:

"use strict"
const Node =function(word , key){
 this.key='';
 this.word='';

 this.getKey=function(){
  return this.key;
 }
 this.getWord=function(){
  return this.word;
 }
 this.setKey=function(key){
  this.key=key;
 }
 this.setWord=function(word){
  this.word=word;
 }

 }


function insert(obj){
 let key=obj.getKey();
 let update=false;
 for(let i=0;i<arrayNode.length;i++){

  if(arrayNode[i].getKey()==key){
  //update
   update = true;
   arrayNode[i] = obj;
  }
  }
 if (!update){
  arrayNode.push(obj)
 }
 }
function hash(word){
 const  key=[];
 key[0]=word[0];
 let  j = 1
 const arrayWords = ['' , 'bpfv', 'cgjkqsxz', 'dt', 'l', 'mn', 'r'];
 for(let i=1;i<word.length && j<4;i++){
  for (let k = 1; k < arrayWords.length ; k++){
   if(!arrayWords[k].includes(word[i])){
      continue;
   }
   key[j] = k;
   j++;
  }
  
 }
 return key.join('')
}
const arrayNode = [];
const pal=['Departamento','Almacenero','Deporte'];
for(let i=0;i<pal.length;i++){

 const node= new Node();
 node.setWord(pal[i]);
 node.setKey(hash(pal[i]));
 insert(node);
 
}
for(let i in arrayNode){
 console.info(`la palabra ${arrayNode[i].getWord()} su llave es ${arrayNode[i].getKey()}`)
}
JackNavaRow
  • 6,836
  • 5
  • 22
  • 49