Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Devuelve todos los nodos de la tabla hash

Intento devolver todos los nodos de una función hash.

Así que lo tengo así:

 class HasTable { constructor(size) { this.buckets = Array(size); this.numBuckets = this.buckets.length; } hash(key) { let sum = 0; for (let i = 0; i < key.length; i++) { sum += key.charCodeAt(i); } let bucket = sum % this.numBuckets; return bucket; } insert(key, value) { let index = this.hash(key); console.log("INDEX", index); if (!this.buckets[index]) this.buckets[index] = new HashNode(key, value); else if (this.buckets[index].key === key) { this.buckets[index].value = value; } else { let currentNode = this.buckets[index]; while (currentNode.next) { if (currentNode.next.key === key) { currentNode.next.value = value; return; } currentNode = currentNode.next; } currentNode.next = new HashNode(key, value); } } get(key) { let index = this.hash(key); if (!this.buckets[index]) return null; else { let currentNode = this.buckets[index]; while (currentNode) { if (this.buckets[index].key === key) return currentNode.value; currentNode.next; } return null; } } returnAll() { let allNodes = []; for (let i = 0; i < this.numBuckets; i++) { let currentNode = this.buckets[i]; while (currentNode) { allNodes.push({key: currentNode.key, value: currentNode.value}); currentNode = currentNode.next; } } return allNodes; }; } class HashNode { constructor(key, value, next) { this.key = key; this.value = value; this.next = next || null; } } let has = new HasTable(30); has.insert("a", "gmail.com"); has.insert("b", "hotmail.com"); has.insert("b", "hotmailHOTMAIL.com"); has.insert("c", "gstar.com"); has.insert("d", "oke.com"); has.insert("e", "nice.com"); has.insert("e", "nice99.com"); has.insert("e", "nice101.com"); console.log(has.returnAll());

Pero la salida es esta:

 0: {key: 'a', value: 'gmail.com'} 1: {key: 'b', value: 'hotmailHOTMAIL.com'} 2: {key: 'c', value: 'gstar.com'} 3: {key: 'd', value: 'oke.com'} 4: {key: 'e', value: 'nice101.com'}

Lo cual, por supuesto, no es correcto. Porque tengo tres llaves con e

? Mi resultado deseado es muy claro.

Inserto ocho nodos. Pero si llamo al método returnAll, solo devuelve nodos.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En lo que respecta a mi comprensión de la tabla hash, su implementación se está comportando correctamente. Las entradas encadenadas se agregan al depósito solo si las entradas con diferentes claves se asignan al mismo hash. En su caso, tiene entradas con la misma clave insertada una y otra vez, lo que hace que se sobrescriba la entrada ya existente. Esto se hace con esta parte de su código:

 ... else if (this.buckets[index].key === key) { this.buckets[index].value = value; } ...

Se agregarían entradas encadenadas, por ejemplo

 has.insert("e", "nice.com"); has.insert("eZ", "nice99.com");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!