0% found this document useful (0 votes)
2 views27 pages

Lecture 8 - Hash Tables

Hash tables are data structures that store records with keys, where the location of each record is determined by the hash value of its key. In open address hashing, if a collision occurs, the next available location is used for insertion, and searching for a key is typically efficient. Deleted records must be marked in a special way to avoid confusion during searches.

Uploaded by

markkifunye159
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)
2 views27 pages

Lecture 8 - Hash Tables

Hash tables are data structures that store records with keys, where the location of each record is determined by the hash value of its key. In open address hashing, if a collision occurs, the next available location is used for insertion, and searching for a key is typically efficient. Deleted records must be marked in a special way to avoid confusion during searches.

Uploaded by

markkifunye159
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/ 27

Hash Tables

Hash Tables
• What is a hash table ?
• The simplest kind of hash table is an array of records.
• This example has 701 records.
[0] [1] [2] [3] [4] [5] [ 700]

...
What is a Hash Table ?

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


• In this example, the key is a long integer field called Number.
[4]
Number 506643548

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


...
What is a Hash Table ?

• The number might be a person's identification number, and the


rest of the record has information about the person.
[4]
Number 506643548

[ 700]
[0] [1] [2] [3] [4] [5]
...
What is a Hash Table ?
• When a hash table is in use, some spots contain valid records, and
other spots are "empty".

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

Number 281942902 Number 233667136


Number 506643548 Number 155778322

...
Open Address Hashing
• 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. Number 580625685

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


Number 281942902 Number 233667136

...
Number 506643548 Number 155778322
Inserting a New Record
• Typical way create a hash value:
Number 580625685
(Number mod 701)
• What is (580625685 % 701) ?

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


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...
Inserting a New Record
• Typical way to create a hash value: Number 580625685
• (Number mod 701)
• What is (580625685 % 701) ?
3

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


Number 281942902 Number 233667136 Number 506643548 Number 155778322
Inserting a New Record

• The hash value is used for the location of the new record.
Number 580625685

[3]

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


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...
Inserting a New Record

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

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


Number 281942902 Number 233667136

...
Number 580625685 Number 506643548 Number 155778322
Collisions
• Here is another new record to insert, with a hash value of 2.
Number 701466868

My hash
value is [2].

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


...
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322
Collisions
• 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.
Number 701466868

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


Number 281942902 Number 233667136 Number 580625685

...
Number 506643548 Number 155778322
Collisions
• 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.
Number 701466868

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


...
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322
Collisions
• 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.

Number 701466868

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


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322

...
Collisions
• The new record goes in the empty spot.

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


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Searching for a Key

• The data that's attached to a key can be found fairly quickly.


Number 701466868

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


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Searching for a Key
• Calculate the hash value.
• Check the location of the array for the key.
Number 701466868
• If location 2 has a different key than the one
you are looking for, then move forward...
Not me. My hash
value is [2].
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Searching for a Key
• Keep moving forward until you find the key, or you reach an empty spot.
Number 701466868

My hash
Not me.
value is [2].

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


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Searching for a Key
• Keep moving forward until you find the key, or you reach an empty spot.
Number 701466868

My hash
Not me. value is [2].

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


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Searching for a Key
• Keep moving forward until you find the key, or you reach an empty spot.
Number 701466868

My hash
value is [2].

Yes!

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


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Searching for a Key
• When the item is found, the information can be copied to the necessary
location or to provide the result of the search function..
Number 701466868

My hash
value is [2].
Yes!
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Deleting a Record
• Records may also be deleted from a hash table.

Please
delete me.

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


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
Deleting a Record
• Records may also be deleted from a hash table.
• But the location must not be left as an ordinary "empty spot" since that
could interfere with searches.

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


Number 281942902 Number 233667136 Number 580625685 Number 701466868 Number 155778322

...
Deleting a Record
• The location must be marked in some special way so that a search can tell
that the spot used to have something in it.

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


Number 281942902 Number 233667136 Number 580625685 Number 701466868 Number 155778322

...
Summary: Hashing
• Hash tables store a collection of records with keys.
• The location of a record depends on the hash value of the
record's key.
• Open address hashing:
• When a collision occurs, the next available location is used.
• Searching for a particular key is generally quick.
• When an item is deleted, the location must be marked in a special way,
so that the searches know that the spot used to be used.
Reading Assignment
• Clustering
• Double Hashing
• Chained Hashing

You might also like