0% found this document useful (0 votes)
16 views33 pages

Dsa L11

The document provides an overview of hash tables, explaining their structure as an array of records with keys that are used to generate hash values for record insertion. It discusses the concept of collisions that occur when multiple records are assigned the same hash value and presents methods for resolving these collisions, such as open addressing and chaining. Additionally, it includes examples of inserting and deleting records in a hash table.

Uploaded by

seunadepoju64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views33 pages

Dsa L11

The document provides an overview of hash tables, explaining their structure as an array of records with keys that are used to generate hash values for record insertion. It discusses the concept of collisions that occur when multiple records are assigned the same hash value and presents methods for resolving these collisions, such as open addressing and chaining. Additionally, it includes examples of inserting and deleting records in a hash table.

Uploaded by

seunadepoju64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

1

Dr. Mohammed Al-Hubaishi


Data Structures & Algorithms

Hashing (Hash Table)

Dr. MOHAMMED AL-HUBAISHI


2

Dr. Mohammed Al-Hubaishi


3
Hashing

Dr. Mohammed Al-Hubaishi


What is a Hash Table ? 4

► The simplest kind of hash table is an array of records.


► This example has 701 records.

[0] [1] [2] [3] [4] [5] [ 700]

...

An array of records

Dr. Mohammed Al-Hubaishi


[4] 5
What is a Hash Table ?
Number 506643548

► Each record has a special field, called its key.


► In this example, the key is a long integer field called
Number.
► The number might be a person's identification number,
and the rest of the record has information about the
person.
[0] [1] [2] [3] [4] [5] [ 700]

...

Dr. Mohammed Al-Hubaishi


6
Inserting a New Record Number 580625685

► In order to insert a new record, the key must somehow be


converted to an array index.
► The index is called the hash value of the key.

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...

Dr. Mohammed Al-Hubaishi


7
Hashing problem - Fits to size

Dr. Mohammed Al-Hubaishi


8
Hashing : char

Dr. Mohammed Al-Hubaishi


9
We used mod by size

Dr. Mohammed Al-Hubaishi


10
Inserting a New Record Number 580625685

► Typical way create a hash value:

(Number mod 701)

What is (580625685 mod 701) ?

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...

Dr. Mohammed Al-Hubaishi


11
Inserting a New Record Number 580625685

► Typical way to create a hash value:

(Number mod 701)


3
What is (580625685 mod 701) ?

[0] [1] [2] [3] [4] [5] [ 700]


Number 155778322
281942902
506643548
233667136

..
.
Dr. Mohammed Al-Hubaishi
12
Inserting a New Record Number 580625685

► The hash value is used for the location of


the new record.

[3]

[0] [1] [2] [3] [4] [5] [ 700]


Number 155778322
281942902
506643548
233667136

..
.
Dr. Mohammed Al-Hubaishi
13
Inserting a New Record

► The hash value is used for the location of the new record.

[0] [1] [2] [3] [4] [5] [ 700]


Number 580625685
281942902
155778322
506643548
233667136

..
.
Dr. Mohammed Al-Hubaishi
14
New problem: Collisions Number 701466868

► Here is another new record to insert, with a


hash value of 2.

My hash
value is [2].

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322

...

Dr. Mohammed Al-Hubaishi


15
Ways of solving collisions

Dr. Mohammed Al-Hubaishi


16
Replacement

Dr. Mohammed Al-Hubaishi


17
Replacement

Dr. Mohammed Al-Hubaishi


18
Open Addressing

Dr. Mohammed Al-Hubaishi


19
Collisions
Number 701466868

► This is called a collision, because there is


already another valid record at [2].

When a collision
occurs,
move forward until you
find an empty spot.
[0] [1] [2] [3] [4] [5] [ 700]
Number 580625685
281942902
155778322
506643548
233667136

..
.
Dr. Mohammed Al-Hubaishi
20
Collisions
Number 701466868

► This is called a collision, because there is


already another valid record at [2].

When a collision
occurs,
move forward until you
find an empty spot.
[0] [1] [2] [3] [4] [5] [ 700]
Number 580625685
281942902
155778322
506643548
233667136

..
.
Dr. Mohammed Al-Hubaishi
21
Collisions Number 701466868

► This is called a collision, because there is


already another valid record at [2].

When a collision
occurs,
move forward until you
find an empty spot.
[0] [1] [2] [3] [4] [5] [ 700]
Number 580625685
281942902
155778322
506643548
233667136

..
.
Dr. Mohammed Al-Hubaishi
22
Collisions

► This is called a collision, because there is


already another valid record at [2].

The new record goes


in the empty spot.

[0] [1] [2] [3] [4] [5] [ 700]


Number 701466868
281942902
155778322
580625685
506643548
233667136

..
.
Dr. Mohammed Al-Hubaishi
23
Open Addressing

Dr. Mohammed Al-Hubaishi


24

Dr. Mohammed Al-Hubaishi


25
Open Addressing

Dr. Mohammed Al-Hubaishi


26
Open Addressing

Number of collisions happen

Dr. Mohammed Al-Hubaishi


27
Chaining

Dr. Mohammed Al-Hubaishi


28
Chaining : Insert

In hashing there is a hash function that maps keys to some


values. But these hashing function may lead to collision that
is two or more keys are mapped to same value. Chain
hashing avoids collision. The idea is to make each cell of
hash table point to a linked list of records that have same
hash function value.

Insert: Move to the bucket corresponds to


the above calculated hash index and insert
the new node at the end of the list.

Dr. Mohammed Al-Hubaishi


29
Chaining : Delete

Delete: To delete a node from hash table, calculate the hash


index for the key, move to the bucket corresponds to the
calculated hash index, search the list in the current bucket to
find and remove the node with the given key (if found).

Dr. Mohammed Al-Hubaishi


30
Chaining : C++ code

https://onlinegdb.com/1too_4ogK
Dr. Mohammed Al-Hubaishi
31
Problem of Chaining

► When the size = 2 ?

Dr. Mohammed Al-Hubaishi


32
Book References

Dr. Mohammed Al-Hubaishi


33

Dr. Mohammed Al-Hubaishi

You might also like