Hashing
Hashing
What is hashing?
Hashing is a method of inserting data into a
table.
Tables can be implemented in many ways.
Examples include a fixed array (limiting
number of elements), array of linked lists
(potentially unlimited number of elements)
Hashing is a technique used for performing
insertions, deletions and finds in constant
average time (i.e. O(1))
Why use hashing?
0 1 2 3 4 5 6 7 8 9 10 11
12
Yousuf
Husain
Musab
Radha
Ziyad
Louai
Adel
Hash Tables
• There are two types of Hash Tables: Open-addressed Hash Tables and Separate-
Chained Hash Tables.
· Initialization.
· Insertion.
· Searching
· Deletion.
Types of Hashing
• There are two types of hashing :
1. Static hashing: In static hashing, the hash
function maps search-key values to a fixed set of
locations.
2. Dynamic hashing: In dynamic hashing a
hash table can grow to handle more items. The
associated hash function must change as the table
grows.
• The load factor of a hash table is the ratio of the
number of keys in the table to the size of the hash
table.
Hash Functions
• A hash function, h, is a function which
transforms a key from a set, K, into an index in
a table of size n:
A key can be a number, a string, a record etc.
• The size of the set of keys, |K|, to be
relatively very large.
• It is possible for different keys to hash to the
same array location.
• This situation is called collision and the
colliding keys are called synonyms.
Hash Functions (cont’d)
· Minimize collisions.
· Be easy and quick to compute.
· Distribute key values evenly in the hash
table.
· Use all the information provided in the
key.
Some Applications of Hash Tables
• Database systems: Specifically, those that require efficient random access. Generally,
database systems try to optimize between two types of access methods: sequential and
random. Hash tables are an important part of efficient random access because they
provide a way to locate data in a constant amount of time.
• Symbol tables: The tables used by compilers to maintain information about symbols from
a program. Compilers access information about symbols frequently. Therefore, it is
important that symbol tables be implemented very efficiently.
• Data dictionaries: Data structures that support adding, deleting, and searching for data.
Although the operations of a hash table and a data dictionary are similar, other data
structures may be used to implement data dictionaries. Using a hash table is particularly
efficient.