SlideShare a Scribd company logo
Hash Table
Hash table
• Hash table is a data structure in which keys are
mapped to array positions by a hash function.
• Hash table, an element with key k is stored at
index h(k) and not k.
• It means a hash function h is used to calculate the
index at which the element with key k will be
stored.
• The process of mapping the keys to appropriate
locations in a hash table is called hashing.
• Hash table in which each key from the set k is
mapped to locations generated by using a
hash function.
• When two or more keys map to same memory
location is known as collision.
Hash Function
• A hash function is simply a mathematical formula
which when applied to a key, produces an integer
which can be used as an index for the key in the
hash table.
• Hash functions are used to reduce the number of
collisions.
• In Practical, there is no hash function that
eliminates collision completely.
• A good hash function can only minimize the
number of collisions.
Different Hash Functions
• Division Method
• It is the most simple method of hashing an
integer k.
• The method divides k by M and then uses the
remainder thus obtained.
h(k)= k mod M
• Where k is key and M is the maximum prime
number of the hash table size.
Example
• Calculate the hash value of key 1234 and
Given a hash table of size 100.
h(1234) = 1234 % 97 = 70
• 97 is the maximum prime number in the hash
table
Multiplication Method
1. Choose a constant A such that 0 < A <1.
2. Multiply the key k by A.
3. Extract the fractional part of kA.
4. Multiply the result of step 3 by m and take
the floor.
h(k) = |m (kA mod 1) |
Example
• Given a hash table of size 1000, map the key
12345 to an appropriate location in the hash
table.
• Suppose, We will use A = 0.618033, m = 1000 and
k=12345
h(12345) = | 1000 (12345 x 0.618033 mod 1) |
h(12345) = | 1000 (7629.617385 mod 1) |
h(12345) = | 1000 (0.617385) |
h(12345) = | 617.385 |
h(12345) = 617
Mid-Square Method
1. Square the value of the key. i.e. k2.
2. Extract the middle r bits of the result
obtained in step 1.
h(k) = k2
Example
• Calculate the hash value for key 5642 using
the mid-square method. The hash table has
100 memory location.
• We have k = 5642 and hash table has 100
memory location, it means that only two
digits are needed to map the key to a location
in the hash table, so r=2.
h(5642) = 5642 x 5642 = 31832164
h(5642) = 32.
Folding method
1. Divide the key value into a number of parts.
That is, divide k into parts k1, k2 ……. Kn,
where each part has the same number of
digits except the last part which may have
lesser digits than the other parts.
2. Add the individual parts. That is, obtain the
sum of k1+k2+……..Kn.
3. The hash value is produced by ignoring the
last carry, if any
Example
• Given a hash table of 100 locations, calculate
the hash value using folding method for key
34567.
• Key parts = 34, 56 and 7
• Sum of key parts = 34 + 56 + 7
• Hash value = 97
Collisions
• Collisions occur when the hash function maps two
different keys to the same location.
• Two records cannot be stored in the same location.
• A method used to solve the problem of collision, also
called collision resolution technique.
• The two most popular methods of resolving a
collision are :
1. Open addressing and
2. Chaining
Collision Resolution by Open
addressing
• Once a collision takes place, open addressing
computes new positions using a probe sequence
and the next record is stored in that position.
• The hash table contains two types of values :
sentinel value (-1) and data values.
• The presence of a sentinel value indicates that
the location contains no data value at present
but can be used to hold a value.
• Open addressing technique can be implemented
using linear probing, quadratic probing and
double hashing.
Linear Probing
• The simplest approach to resolve a collision is
linear probing.
• Hash function is used to resolve the collision :
h (k,i) = [h’(k) + i] mod m
Where m is the size of the hash table,
h’(k) = (k mod m) and
i is the probe number and varies
from 0 to m-1
Example• Consider a hash table of size 10. Using linear probing,
insert the keys 72, 24, 63, 81 and 92.
• Initially, the hash table can be given as :
• Step 1 : Key = 72
h(72,0) = (72 mod 10 + 0 ) mod 10
h(72,0) = (2) mod 10
h(72,0) = 2
Since T[2] is vacant, insert key 72 at this location.
0 1 2 3 4 5 6 7 8 9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
0 1 2 3 4 5 6 7 8 9
-1 -1 72 -1 -1 -1 -1 -1 -1 -1
Example
• Step 2 : Key = 24
h(24,0) = (24 mod 10 + 0 ) mod 10
h(24,0) = (4) mod 10
h(24,0) = 4
Since T[4] is vacant, insert key 24 at this location.
0 1 2 3 4 5 6 7 8 9
-1 -1 72 -1 24 -1 -1 -1 -1 -1
Example
• Step 3 : Key = 63
h(63,0) = (63 mod 10 + 0 ) mod 10
h(63,0) = (3) mod 10
h(63,0) = 3
Since T[3] is vacant, insert key 63 at this location.
0 1 2 3 4 5 6 7 8 9
-1 -1 72 63 24 -1 -1 -1 -1 -1
Example
• Step 4 : Key = 81
h(81,0) = (81 mod 10 + 0 ) mod 10
h(81,0) = (1) mod 10
h(81,0) = 1
Since T[1] is vacant, insert key 81 at this location.
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 -1 -1 -1 -1 -1
Example
• Step 5 : Key = 92
h(92,0) = (92 mod 10 + 0 ) mod 10
h(92,0) = (2) mod 10
h(92,0) = 2
Since T[2] is occupied, so we cannot store the key
92 in T[2]. Therefore, try again for the next location.
Thus probe i=1
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 -1 -1 -1 -1 -1
Example
• Step 5 : Key = 92
h(92,1) = (92 mod 10 + 1 ) mod 10
h(92,1) = (2+1) mod 10
h(92,1) = 3
Since T[3] is occupied, so we cannot store the key
92 in T[3]. Therefore, try again for the next location.
Thus probe i=2
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 -1 -1 -1 -1 -1
Example
• Step 5 : Key = 92
h(92,2) = (92 mod 10 + 2 ) mod 10
h(92,2) = (2+2) mod 10
h(92,2) = 4
Since T[4] is occupied, so we cannot store the key
92 in T[4]. Therefore, try again for the next location.
Thus probe i=3
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 -1 -1 -1 -1 -1
Example
• Step 5 : Key = 92
h(92,3) = (92 mod 10 + 3 ) mod 10
h(92,3) = (2+3) mod 10
h(92,3) = 5
Since T[5] is vacant, insert key 92 at this location
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 92 -1 -1 -1 -1
Merits & Demerits
• Linear probing enables good memory caching.
• More the number of collisions, higher the
probes that are required to find a free location
and Lesser is the Performance, this process is
known as Primary clustering.
Quadratic Probing
• Hash function is used to resolve the collision :
h (k,i) = [h’(k) + c1 i + c2 i2] mod m
Where m is the size of the hash table
h’(k) = (k mod m)
i is the probe number that varies
from 0 to m-1
and c1 and c2 are constants such
that c1 and c2 != 0
Merits & Demerits
• Quadratic probing eliminates the primary
clustering and provides good memory caching.
• If there is a collision between two keys, then
the same probe sequence will be followed for
both, this process is called secondary
clustering.
Double Hashing
• Double hashing, we use two hash functions rather
than a single function.
• The hash function in the case of double hashing
can be given as :
h(k,i) = [h1(k) + i h2(k)] mod m
Where, m is the size of the hash table,
h1(k) and h2(k) are two hash functions given as
h1(k) = k mod m and h2(k) = k mod m’
i is the probe number that varies from 0 to m-1
and m’ is chosen to be less than m i.e. m’=m-1
or m-2
Merits & demerits
• In double hashing, we use two hash functions
rather a single function.
• The performance of double hashing is very
close to the performance of the ideal scheme
of uniform hashing.
• It minimizes repeated collisions.
Collision Resolution by Chaining
• In chaining, each location in the hash table
stores a pointer to a linked list that contains all
the key values that were hashed to the same
location.
8. Hash table
Example
• Load the keys 23, 13, 21, 14, 7, 8, and 15 , in this order,
in a hash table of size 7 using chaining with the hash
function: h(key) = key % 7
h(23) = 23 % 7 = 2
h(13) = 13 % 7 = 6
h(21) = 21 % 7 = 0
h(14) = 14 % 7 = 0 collision
h(7) = 7 % 7 = 0 collision
h(8) = 8 % 7 = 1
h(15) = 15 % 7 = 1 collision
8. Hash table
Merits
• The hash table size is unlimited. In this case
you don’t need to expand the table and
recreate a hash function.
• Collision handling is simple: just insert
colliding records into a list.
Demerits
• Using pointers slows the algorithm: time
required is to allocate new nodes.
• The main cost of chaining is the extra space
required for the linked lists.
• Overhead of multiple linked lists.

More Related Content

PPTX
Hamming code system
PPT
S-DES.ppt
PDF
I. Alpha-Beta Pruning in ai
PPTX
Distance Vector Routing Protocols
PPTX
PPTX
Top down parsering and bottom up parsering.pptx
PDF
I. Hill climbing algorithm II. Steepest hill climbing algorithm
PPT
Hamming codes
Hamming code system
S-DES.ppt
I. Alpha-Beta Pruning in ai
Distance Vector Routing Protocols
Top down parsering and bottom up parsering.pptx
I. Hill climbing algorithm II. Steepest hill climbing algorithm
Hamming codes

What's hot (20)

PPTX
Point to-point protocol (ppp)
PPTX
Graph theory
PPTX
Introduction to Dynamic Programming, Principle of Optimality
PPT
stop and wait
PPTX
Lecture 06 production system
PPTX
Single source Shortest path algorithm with example
PPTX
Transport Layer Security (TLS)
PPT
Strongly Connected Components
PPTX
Hill climbing algorithm
PDF
Artificial Intelligence - Hill climbing.
PPTX
N queen problem
PPTX
0 1 knapsack using branch and bound
PPTX
Decision tree, softmax regression and ensemble methods in machine learning
PPTX
Back tracking and branch and bound class 20
PDF
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
PPTX
Bayesian Belief Network and its Applications.pptx
PDF
Theta notation
PPT
Gsm radio-interface
PPT
Framming data link layer
PPTX
Knowledge representation and Predicate logic
Point to-point protocol (ppp)
Graph theory
Introduction to Dynamic Programming, Principle of Optimality
stop and wait
Lecture 06 production system
Single source Shortest path algorithm with example
Transport Layer Security (TLS)
Strongly Connected Components
Hill climbing algorithm
Artificial Intelligence - Hill climbing.
N queen problem
0 1 knapsack using branch and bound
Decision tree, softmax regression and ensemble methods in machine learning
Back tracking and branch and bound class 20
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
Bayesian Belief Network and its Applications.pptx
Theta notation
Gsm radio-interface
Framming data link layer
Knowledge representation and Predicate logic
Ad

Similar to 8. Hash table (20)

PDF
Hashing components and its laws 2 types
PPTX
Lecture14_15_Hashing.pptx
PDF
Tojo Sir Hash Tables.pdfsfdasdasv fdsfdfsdv
PPT
Hashing in Data Structure and analysis of Algorithms
PDF
hashtableeeeeeeeeeeeeeeeeeeeeeeeeeee.pdf
PPTX
Data Structures-Topic-Hashing, Collision
PPTX
HASHING IS NOT YASH IT IS HASH.pptx
PDF
LECT 10, 11-DSALGO(Hashing).pdf
PPTX
Hashing .pptx
PPTX
Hashing.pptx
PPT
PDF
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
PPTX
HASHING.ppt.pptx
PPTX
Hashing in Data Structure and Algorithm PPT
PPTX
Hashing using a different methods of technic
PPTX
Hashing techniques, Hashing function,Collision detection techniques
PPTX
Hashing And Hashing Tables
PPT
Advance algorithm hashing lec II
PDF
Hashing CollisionDetection in Data Structures
Hashing components and its laws 2 types
Lecture14_15_Hashing.pptx
Tojo Sir Hash Tables.pdfsfdasdasv fdsfdfsdv
Hashing in Data Structure and analysis of Algorithms
hashtableeeeeeeeeeeeeeeeeeeeeeeeeeee.pdf
Data Structures-Topic-Hashing, Collision
HASHING IS NOT YASH IT IS HASH.pptx
LECT 10, 11-DSALGO(Hashing).pdf
Hashing .pptx
Hashing.pptx
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
HASHING.ppt.pptx
Hashing in Data Structure and Algorithm PPT
Hashing using a different methods of technic
Hashing techniques, Hashing function,Collision detection techniques
Hashing And Hashing Tables
Advance algorithm hashing lec II
Hashing CollisionDetection in Data Structures
Ad

More from Mandeep Singh (11)

PPTX
9.Sorting & Searching
PPTX
7. Spanning trees
PPTX
6. Graphs
PPTX
5.Linked list
PPTX
4. Queues in Data Structure
PPTX
Stacks in DATA STRUCTURE
PPTX
2. Array in Data Structure
PPT
1. Data structures introduction
PPT
Standard Template Library (STL) in Object Oriented Programming
PPT
Ip6 tables in linux
PPT
Iptables in linux
9.Sorting & Searching
7. Spanning trees
6. Graphs
5.Linked list
4. Queues in Data Structure
Stacks in DATA STRUCTURE
2. Array in Data Structure
1. Data structures introduction
Standard Template Library (STL) in Object Oriented Programming
Ip6 tables in linux
Iptables in linux

Recently uploaded (20)

PDF
Digital Logic Computer Design lecture notes
PDF
composite construction of structures.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Project quality management in manufacturing
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
web development for engineering and engineering
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Digital Logic Computer Design lecture notes
composite construction of structures.pdf
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Foundation to blockchain - A guide to Blockchain Tech
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Model Code of Practice - Construction Work - 21102022 .pdf
CH1 Production IntroductoryConcepts.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Sustainable Sites - Green Building Construction
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Project quality management in manufacturing
Operating System & Kernel Study Guide-1 - converted.pdf
additive manufacturing of ss316l using mig welding
web development for engineering and engineering
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

8. Hash table

  • 2. Hash table • Hash table is a data structure in which keys are mapped to array positions by a hash function. • Hash table, an element with key k is stored at index h(k) and not k. • It means a hash function h is used to calculate the index at which the element with key k will be stored. • The process of mapping the keys to appropriate locations in a hash table is called hashing.
  • 3. • Hash table in which each key from the set k is mapped to locations generated by using a hash function. • When two or more keys map to same memory location is known as collision.
  • 4. Hash Function • A hash function is simply a mathematical formula which when applied to a key, produces an integer which can be used as an index for the key in the hash table. • Hash functions are used to reduce the number of collisions. • In Practical, there is no hash function that eliminates collision completely. • A good hash function can only minimize the number of collisions.
  • 5. Different Hash Functions • Division Method • It is the most simple method of hashing an integer k. • The method divides k by M and then uses the remainder thus obtained. h(k)= k mod M • Where k is key and M is the maximum prime number of the hash table size.
  • 6. Example • Calculate the hash value of key 1234 and Given a hash table of size 100. h(1234) = 1234 % 97 = 70 • 97 is the maximum prime number in the hash table
  • 7. Multiplication Method 1. Choose a constant A such that 0 < A <1. 2. Multiply the key k by A. 3. Extract the fractional part of kA. 4. Multiply the result of step 3 by m and take the floor. h(k) = |m (kA mod 1) |
  • 8. Example • Given a hash table of size 1000, map the key 12345 to an appropriate location in the hash table. • Suppose, We will use A = 0.618033, m = 1000 and k=12345 h(12345) = | 1000 (12345 x 0.618033 mod 1) | h(12345) = | 1000 (7629.617385 mod 1) | h(12345) = | 1000 (0.617385) | h(12345) = | 617.385 | h(12345) = 617
  • 9. Mid-Square Method 1. Square the value of the key. i.e. k2. 2. Extract the middle r bits of the result obtained in step 1. h(k) = k2
  • 10. Example • Calculate the hash value for key 5642 using the mid-square method. The hash table has 100 memory location. • We have k = 5642 and hash table has 100 memory location, it means that only two digits are needed to map the key to a location in the hash table, so r=2. h(5642) = 5642 x 5642 = 31832164 h(5642) = 32.
  • 11. Folding method 1. Divide the key value into a number of parts. That is, divide k into parts k1, k2 ……. Kn, where each part has the same number of digits except the last part which may have lesser digits than the other parts. 2. Add the individual parts. That is, obtain the sum of k1+k2+……..Kn. 3. The hash value is produced by ignoring the last carry, if any
  • 12. Example • Given a hash table of 100 locations, calculate the hash value using folding method for key 34567. • Key parts = 34, 56 and 7 • Sum of key parts = 34 + 56 + 7 • Hash value = 97
  • 13. Collisions • Collisions occur when the hash function maps two different keys to the same location. • Two records cannot be stored in the same location. • A method used to solve the problem of collision, also called collision resolution technique. • The two most popular methods of resolving a collision are : 1. Open addressing and 2. Chaining
  • 14. Collision Resolution by Open addressing • Once a collision takes place, open addressing computes new positions using a probe sequence and the next record is stored in that position. • The hash table contains two types of values : sentinel value (-1) and data values. • The presence of a sentinel value indicates that the location contains no data value at present but can be used to hold a value. • Open addressing technique can be implemented using linear probing, quadratic probing and double hashing.
  • 15. Linear Probing • The simplest approach to resolve a collision is linear probing. • Hash function is used to resolve the collision : h (k,i) = [h’(k) + i] mod m Where m is the size of the hash table, h’(k) = (k mod m) and i is the probe number and varies from 0 to m-1
  • 16. Example• Consider a hash table of size 10. Using linear probing, insert the keys 72, 24, 63, 81 and 92. • Initially, the hash table can be given as : • Step 1 : Key = 72 h(72,0) = (72 mod 10 + 0 ) mod 10 h(72,0) = (2) mod 10 h(72,0) = 2 Since T[2] is vacant, insert key 72 at this location. 0 1 2 3 4 5 6 7 8 9 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 1 2 3 4 5 6 7 8 9 -1 -1 72 -1 -1 -1 -1 -1 -1 -1
  • 17. Example • Step 2 : Key = 24 h(24,0) = (24 mod 10 + 0 ) mod 10 h(24,0) = (4) mod 10 h(24,0) = 4 Since T[4] is vacant, insert key 24 at this location. 0 1 2 3 4 5 6 7 8 9 -1 -1 72 -1 24 -1 -1 -1 -1 -1
  • 18. Example • Step 3 : Key = 63 h(63,0) = (63 mod 10 + 0 ) mod 10 h(63,0) = (3) mod 10 h(63,0) = 3 Since T[3] is vacant, insert key 63 at this location. 0 1 2 3 4 5 6 7 8 9 -1 -1 72 63 24 -1 -1 -1 -1 -1
  • 19. Example • Step 4 : Key = 81 h(81,0) = (81 mod 10 + 0 ) mod 10 h(81,0) = (1) mod 10 h(81,0) = 1 Since T[1] is vacant, insert key 81 at this location. 0 1 2 3 4 5 6 7 8 9 -1 81 72 63 24 -1 -1 -1 -1 -1
  • 20. Example • Step 5 : Key = 92 h(92,0) = (92 mod 10 + 0 ) mod 10 h(92,0) = (2) mod 10 h(92,0) = 2 Since T[2] is occupied, so we cannot store the key 92 in T[2]. Therefore, try again for the next location. Thus probe i=1 0 1 2 3 4 5 6 7 8 9 -1 81 72 63 24 -1 -1 -1 -1 -1
  • 21. Example • Step 5 : Key = 92 h(92,1) = (92 mod 10 + 1 ) mod 10 h(92,1) = (2+1) mod 10 h(92,1) = 3 Since T[3] is occupied, so we cannot store the key 92 in T[3]. Therefore, try again for the next location. Thus probe i=2 0 1 2 3 4 5 6 7 8 9 -1 81 72 63 24 -1 -1 -1 -1 -1
  • 22. Example • Step 5 : Key = 92 h(92,2) = (92 mod 10 + 2 ) mod 10 h(92,2) = (2+2) mod 10 h(92,2) = 4 Since T[4] is occupied, so we cannot store the key 92 in T[4]. Therefore, try again for the next location. Thus probe i=3 0 1 2 3 4 5 6 7 8 9 -1 81 72 63 24 -1 -1 -1 -1 -1
  • 23. Example • Step 5 : Key = 92 h(92,3) = (92 mod 10 + 3 ) mod 10 h(92,3) = (2+3) mod 10 h(92,3) = 5 Since T[5] is vacant, insert key 92 at this location 0 1 2 3 4 5 6 7 8 9 -1 81 72 63 24 92 -1 -1 -1 -1
  • 24. Merits & Demerits • Linear probing enables good memory caching. • More the number of collisions, higher the probes that are required to find a free location and Lesser is the Performance, this process is known as Primary clustering.
  • 25. Quadratic Probing • Hash function is used to resolve the collision : h (k,i) = [h’(k) + c1 i + c2 i2] mod m Where m is the size of the hash table h’(k) = (k mod m) i is the probe number that varies from 0 to m-1 and c1 and c2 are constants such that c1 and c2 != 0
  • 26. Merits & Demerits • Quadratic probing eliminates the primary clustering and provides good memory caching. • If there is a collision between two keys, then the same probe sequence will be followed for both, this process is called secondary clustering.
  • 27. Double Hashing • Double hashing, we use two hash functions rather than a single function. • The hash function in the case of double hashing can be given as : h(k,i) = [h1(k) + i h2(k)] mod m Where, m is the size of the hash table, h1(k) and h2(k) are two hash functions given as h1(k) = k mod m and h2(k) = k mod m’ i is the probe number that varies from 0 to m-1 and m’ is chosen to be less than m i.e. m’=m-1 or m-2
  • 28. Merits & demerits • In double hashing, we use two hash functions rather a single function. • The performance of double hashing is very close to the performance of the ideal scheme of uniform hashing. • It minimizes repeated collisions.
  • 29. Collision Resolution by Chaining • In chaining, each location in the hash table stores a pointer to a linked list that contains all the key values that were hashed to the same location.
  • 31. Example • Load the keys 23, 13, 21, 14, 7, 8, and 15 , in this order, in a hash table of size 7 using chaining with the hash function: h(key) = key % 7 h(23) = 23 % 7 = 2 h(13) = 13 % 7 = 6 h(21) = 21 % 7 = 0 h(14) = 14 % 7 = 0 collision h(7) = 7 % 7 = 0 collision h(8) = 8 % 7 = 1 h(15) = 15 % 7 = 1 collision
  • 33. Merits • The hash table size is unlimited. In this case you don’t need to expand the table and recreate a hash function. • Collision handling is simple: just insert colliding records into a list.
  • 34. Demerits • Using pointers slows the algorithm: time required is to allocate new nodes. • The main cost of chaining is the extra space required for the linked lists. • Overhead of multiple linked lists.