I have an open hashing code in Javascript

0

I have a code in js about an "open hashing" that is hanging, I think it's because of the 'Insert_in_bucket (obj, pos)' function and I'm not finding the solution.

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);
}

When executing it with Node.js it hangs showing, with the instruction "console.log (i)" inside the "insert" function, 0 and 1 alternated if I'm not mistaken.

    
asked by Tuti Videla 09.11.2018 в 17:27
source

1 answer

0

Your code looks a little confusing, you declare variables with the same name and this can be the problem (an example see variable i) and this is giving you problems reaching the variables (scope)

my alternative solution with less code just change the methods of 'Insert_in_bucket(obj,pos)' e insert_in_array each of them what it does is insert and update. the hash method simplify it with the includes method as follows :

"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()}')
    }

Intentionally leave the hash code that was constantly repeated so that it is a bit easier to understand, but it may look like this:

"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()}')
}
    
answered by 09.11.2018 / 19:20
source