Upload
meljun-cortes-mbampa
View
230
Download
0
Embed Size (px)
Citation preview
8/21/2019 Meljun Cortes Data Structures Hash Table
1/22
Data Structures and Algorithms
Hash Table
another data structure used to handle large
amount of data that offers fast insertion and
searching
fast searching and deleting
based on arrays, and arrays are difficult to
expand one they’ve been created
Hash Table * Property of STI Page 1 of 22
no convenient way to visit the items in a hash
table in any kind of order
8/21/2019 Meljun Cortes Data Structures Hash Table
2/22
Data Structures and Algorithms
Keys are Index Numbers
Hash Table * Property of STI Page 2 of 22
empRecord rec = databaseArray[72];
databaseArray[totalEmployees++] =
newRecord;
8/21/2019 Meljun Cortes Data Structures Hash Table
3/22
Data Structures and Algorithms
Dictionary
English-language dictionary is a typical exampleof database that can be efficiently handled with
a hash table
computer-language compiler - similar widelyused application for hash tables
Hash Table * Property of STI Page 3 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
4/22
Data Structures and Algorithms
Converting Words to Numbers
computers use various schemes forrepresenting individual characters as numbers
ASCII code - runs from 0 to 255 to
accommodate capitals, punctuation, and so on
How do you combine digits from individualletters into a number that represents an entireword?
Sorts of approaches
Hash Table * Property of STI Page 4 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
5/22
Data Structures and Algorithms
Add the Digits
Add them:
3 + 1 + 20 + 19 =43
C = 3A = 1T = 20S = 19
Hash Table * Property of STI Page 5 of 22
a wou e co e as
0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 = 1
zzzzzzzzzz (ten Zs)
26 + 26 + 26 + 26 +26 + 26 +26 + 26 + 26 +
26 = 260
8/21/2019 Meljun Cortes Data Structures Hash Table
6/22
Data Structures and Algorithms
Multiply by Powers
another approach of mapping words to numbers
enerates a uni ue number for ever otential
7,546 really means:7*1000 + 5*100 + 4*10 + 6*1
Or writing the multipliers as powers of 10:7*103 + 5*102 + 4*101 + 6*100
Hash Table * Property of STI Page 6 of 22
word
8/21/2019 Meljun Cortes Data Structures Hash Table
7/22
Data Structures and Algorithms
Hashing
use modulo operator (%), which finds theremainder when one number is divided byanother
smallNumber = largeNumber %
smallRange;
Hash Table * Property of STI Page 7 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
8/22
Data Structures and Algorithms
Hashing
hash function - hashes (converts) a number in alarge range into a number in a smaller range
An array into which data is inserted using a
hash function is called a hash table
hugeNumber = ch0*27
9
+ ch0*278
+ ch0*277
+ch0*276 + ch0*275 ch0*274 + ch0*273 +ch0*272 + ch0*271 + ch0*270
arraySize = numberWords * 2;
Hash Table * Property of STI Page 8 of 22
arrayIndex = hugeNumber %
arraySize;
8/21/2019 Meljun Cortes Data Structures Hash Table
9/22
Data Structures and Algorithms
Collisions
To insert the word melioration into the array, hash theword to obtain its index number, but find that the cellat that number is already occupied by the worddemystify , which happens to hash exact samenumber (for a certain size array).
Hash Table * Property of STI Page 9 of 22
Open addressing - search the array in somesystematic way for an empty cell, and insert the newitem there
linear probing , quadratic probing , and double
hashing Separate chaining - new item is simply inserted in the
list at that index
8/21/2019 Meljun Cortes Data Structures Hash Table
10/22
Data Structures and Algorithms
Linear Probing
sequentially search for vacant cells
public DataItem find(int key)
{
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
Hash Table * Property of STI Page 10 of 22
++hashVal;
hashVal %= arraySize;
}
return null;
}
8/21/2019 Meljun Cortes Data Structures Hash Table
11/22
Data Structures and Algorithms
Linear Probing
public void insert(DataItem item)
{
int key = item.getKey();
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null &&
hashArray[hashVal].getKey() != -1)
{++hashVal;
hashVal %= arraySize;
}
hashArray[hashVal] = item;
Hash Table * Property of STI Page 11 of 22
public DataItem delete(int key)
{
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
{
DataItem temp = hashArray[hashVal];
hashArray[hashVal] = nonItem;
return temp;
}++hashVal;
hashVal %= arraySize;
}
return null;
}
8/21/2019 Meljun Cortes Data Structures Hash Table
12/22
Data Structures and Algorithms
Linear Probing
Sample output
Hash Table * Property of STI Page 12 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
13/22
Data Structures and Algorithms
Quadratic Probing
load factor - ratio of the number of items in atable, to the table’s size
loadFactor = nItems / arraySize;
Quadratic probing is an attempt to keep clusters
from forming
Hash Table * Property of STI Page 13 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
14/22
Data Structures and Algorithms
Quadratic Probing
Step is the Square of the Step Number
Hash Table * Property of STI Page 14 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
15/22
Data Structures and Algorithms
Quadratic Probing
Problem with Quadratic Probes
A quadratic probe eliminates the clusteringproblem seen with the linear probe called as
primary clustering
Secondary clustering - each additional item witha key that hashes to its maximum will require alonger probe
Hash Table * Property of STI Page 15 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
16/22
Data Structures and Algorithms
Double Hashing
Sometimes called as rehashing
The secondary hash function must have certaincharacteristics:
It must not be the same as the primary hashfunction.
It must never output a 0 (otherwise therewould be no step; every probe would land
Hash Table * Property of STI Page 16 of 22
,go into an endless loop.)
following form work well
stepSize = constant – (key %
constant);
stepSize = 5 – (key & 5);
8/21/2019 Meljun Cortes Data Structures Hash Table
17/22
Data Structures and Algorithms
Double Hashing
Hash Table * Property of STI Page 17 of 22
public DataItem find(int key){
int hashVal = hashFunc1(key);
int stepSize = hashFunc2(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
return hashArray[hashVal];
hashVal += stepSize;hashVal %= arraySize;
}
return null;
}
8/21/2019 Meljun Cortes Data Structures Hash Table
18/22
Data Structures and Algorithms
Double Hashing
public void insert(int key, DataItem item)
{
int hashVal = hashFunc1(key);
int stepSize = hashFunc2(key);
while(hashArray[hashVal] != null &&
hashArray[hashVal].getKey() != -1)
{hashVal += stepSize;
hashVal %= arraySize;
}
hashArray[hashVal] = item;
}
Hash Table * Property of STI Page 18 of 22
public DataItem delete(int key)
{
int hashVal = hashFunc1(key);int stepSize = hashFunc2(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
{
DataItem temp = hashArray[hashVal];
hashArray[hashVal] = nonItem;
return temp;
}
hashVal += stepSize;
hashVal %= arraySize;
}
return null;
}
8/21/2019 Meljun Cortes Data Structures Hash Table
19/22
Data Structures and Algorithms
Double Hashing
Hash Table * Property of STI Page 19 of 22
8/21/2019 Meljun Cortes Data Structures Hash Table
20/22
Data Structures and Algorithms
Separate Chaining
to install a linked list at each index in the hashtable
Hash Table * Property of STI Page 20 of 22
Deletion
Table Size
Buckets
8/21/2019 Meljun Cortes Data Structures Hash Table
21/22
Data Structures and Algorithms
Separate Chaining
public void insert(Link theLink)
{
int key = theLink.getKey();
Link previous = null;
Link current = first;
while( current != null && key > current.getKey() )
{
previous = current;
Hash Table * Property of STI Page 21 of 22
.
}
if(previous==null)
first = theLink;else
previous.next = theLink;
theLink.next = current;
}
8/21/2019 Meljun Cortes Data Structures Hash Table
22/22
Data Structures and Algorithms
Separate Chaining
public void delete(int key)
{
Link previous = null;
Link current = first;
while( current != null && key != current.getKey()
)
{
previous = current;
current = current.next;
}
Hash Table * Property of STI
public Link find(int key)
{
Link current = first;
while(current != null && current.getKey()