Skip to content

Commit

Permalink
Fix quadractic probing overflowing the array (#21)
Browse files Browse the repository at this point in the history
function HashTable(size) {
    this.size = size;
    this.keys = this.initArray(size);
    this.values = this.initArray(size);
    this.limit = 0;
}

HashTable.prototype.get = function(key) {
    var hashedIndex = this.hash(key);

    while (this.keys[hashedIndex] != key) {
        hashedIndex++;

        hashedIndex = hashedIndex % this.size;

    }
    return this.values[hashedIndex];
}

HashTable.prototype.hash = function(key) {
    // Check if int
    if (!Number.isInteger(key)) throw 'must be int';
    return key % this.size;
}

HashTable.prototype.initArray = function(size) {
    var array = [];
    for (var i = 0; i < size; i++) {
        array.push(null);
    }
    return array;
}

HashTable.prototype.put = function(key, value) {
    if (this.limit >= this.size) throw 'hash table is full'

    var hashedIndex = this.hash(key),
        squareIndex = 1;

    // quadratic probing
    while (this.keys[hashedIndex % this.size] != null) {
        hashedIndex += Math.pow(squareIndex, 2);
        squareIndex++;
    }

    this.keys[hashedIndex % this.size] = key;
    this.values[hashedIndex % this.size] = value;
    this.limit++;
}

HashTable.prototype.get = function(key) {

    var hashedIndex = this.hash(key),
        squareIndex = 1;

    while (this.keys[hashedIndex % this.size] != key) {
        hashedIndex += Math.pow(squareIndex, 2);
        hashedIndex = hashedIndex % this.size;
        squareIndex++;
    }
    return this.values[hashedIndex % this.size];
}

var exampletable = new HashTable(13);
exampletable.put(7, "hi");
exampletable.put(20, "hello");
exampletable.put(33, "sunny");
exampletable.put(46, "weather");
exampletable.put(59, "wow");
exampletable.put(72, "fourty");
exampletable.put(85, "happy");
exampletable.put(98, "sad");
  • Loading branch information
Sammie Bae authored May 26, 2020
1 parent 6c4c0bf commit dd84d10
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
17 changes: 6 additions & 11 deletions Chapter11(HashTables).js
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,34 @@ exampletable.put(85, "happy");
exampletable.put(98, "sad");




HashTable.prototype.put = function(key, value) {
if (this.limit >= this.size) throw 'hash table is full'

var hashedIndex = this.hash(key),
squareIndex = 1;

// quadratic probing
while (this.keys[hashedIndex] != null) {
while (this.keys[hashedIndex % this.size] != null) {
hashedIndex += Math.pow(squareIndex, 2);

hashedIndex
squareIndex++;
}

this.keys[hashedIndex] = key;
this.values[hashedIndex] = value;
this.keys[hashedIndex % this.size] = key;
this.values[hashedIndex % this.size] = value;
this.limit++;
}

HashTable.prototype.get = function(key) {

var hashedIndex = this.hash(key),
squareIndex = 1;

while (this.keys[hashedIndex] != key) {
while (this.keys[hashedIndex % this.size] != key) {
hashedIndex += Math.pow(squareIndex, 2);

hashedIndex = hashedIndex % this.size;
squareIndex++;
}

return this.values[hashedIndex];
return this.values[hashedIndex % this.size];
}


Expand Down
73 changes: 73 additions & 0 deletions errata.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,79 @@ function bubbleSort(array) {
}
```

### Page 158

On **page 158**: Using Quadratic Probing [Summary of error]:


**Before**:
```javascript
HashTable.prototype.get = function(key) {
var hashedIndex = this.hash(key),
squareIndex = 1;

while (this.keys[hashedIndex] != key) {
hashedIndex += Math.pow(squareIndex, 2);

hashedIndex = hashedIndex % this.size;
squareIndex++;
}

return this.values[hashedIndex];
}


HashTable.prototype.put = function(key, value) {
if (this.limit >= this.size) throw 'hash table is full'

var hashedIndex = this.hash(key);

while (this.keys[hashedIndex] != null) {
hashedIndex++;

hashedIndex = hashedIndex % this.size;

}
this.keys[hashedIndex] = key;
this.values[hashedIndex] = value;
this.limit++;
}
```

**After**:
```javascript
HashTable.prototype.put = function(key, value) {
if (this.limit >= this.size) throw 'hash table is full'

var hashedIndex = this.hash(key),
squareIndex = 1;

// quadratic probing
while (this.keys[hashedIndex % this.size] != null) {
hashedIndex += Math.pow(squareIndex, 2);
squareIndex++;
}

this.keys[hashedIndex % this.size] = key;
this.values[hashedIndex % this.size] = value;
this.limit++;
}

HashTable.prototype.get = function(key) {

var hashedIndex = this.hash(key),
squareIndex = 1;

while (this.keys[hashedIndex % this.size] != key) {
hashedIndex += Math.pow(squareIndex, 2);
hashedIndex = hashedIndex % this.size;
squareIndex++;
}
return this.values[hashedIndex % this.size];
}
```


***

### page 183
Expand Down

0 comments on commit dd84d10

Please sign in to comment.