0% found this document useful (0 votes)
4 views

Ch 6 - Hash Tables (3)

The document discusses hash tables, which are efficient data structures for quick data retrieval using hash functions to map keys to indices. It explains the concepts of hash functions, compression functions, and collision resolution methods, including separate chaining and linear probing. The document also compares the advantages and disadvantages of these collision resolution techniques.

Uploaded by

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

Ch 6 - Hash Tables (3)

The document discusses hash tables, which are efficient data structures for quick data retrieval using hash functions to map keys to indices. It explains the concepts of hash functions, compression functions, and collision resolution methods, including separate chaining and linear probing. The document also compares the advantages and disadvantages of these collision resolution techniques.

Uploaded by

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

Faculty of Information Technology - Computer Science Department 1

Data Structures

Faculty of Information Technology - Computer Science Department 2


Chapter 6
Hash Tables

Faculty of Information Technology - Computer Science Department 3


Hash Table
 Before we get into the definition of Hash Tables, it is good to introduce why to use Hash
Tables.
 Hash tables are good for doing a quick search on things.
 For instance, suppose we have an array full of data (say 100 items). If we knew the position
that a specific item is stored in an array, then we could quickly access it.
 For example, if we just happen to know that the item we want is at position 3; then I can
apply:
 With this, we don't have to search through each element in the array, we just access position 3.
 The question is, how do we know that position 3 stores the data that we are interested in?
 This is where hashing comes in handy. Given some key, we can apply a hash function to it to
find an index or position that we want to access.
Faculty of Information Technology - Computer Science Department 4
Hash Table

 In Hash Table, several items are distributed in a list using a hash function that is
applied to a property of the data called (a key).

Key Hash Digest Compression Index


Function Function

Digest: a large integer number, Index: an integer in the range 0 …. n-1, where n: list size

Faculty of Information Technology - Computer Science Department 5


Hash Functions
 A Hash Function is a mapping of data (of any type) to a digest (a large integer).
 Many hash functions are available. For example, a hash function could be the sum of the
ASCII code of all characters of the key; such as:
 If key = ‘ABC’ the digest = 65+66+67 = 198
 But we will have the same digest for key=‘BAC’, and for ‘CBA’
 So, the hash function should produce different digests for different keys.
 We can add more diversity by multiplying each ASCII code by the index of the desired
character:
• For the key ‘ABC’, digest = 65*1 + 66*2 + 67*3 = 398
• For the key ‘BAC’ digest = 66*1 + 65*2 + 67*3 = 397
 They become different.

Faculty of Information Technology - Computer Science Department 6


Compression Functions

 The result of the hash function is a large integer, which is much greater than the size
of the list (digest cannot be used as an index).
 Another function is used to compress the digest to fit in the index space of a list
(to be in [0 … n-1]), where is the list size.
 A simple example of a compression function is the modulo , for example:
 If the digest is: 76543763234, and n = 1000, then
the index = 70043763234 % 1000 = 234.
 But all digests that end by 234 will have the same index!!

Faculty of Information Technology - Computer Science Department 7


Collisions

 Hash functions, and compression functions, are not ideal, so a hash function could
produce the same digest for different keys, and so for compression functions, as
shown previously.
 Having the same index for two different keys (or more) is called a COLLISION.

 A collision could be resolved using one of the following two methods:


 Separate Chaining
 Linear Probing.

Faculty of Information Technology - Computer Science Department 8


Collision Resolution – Separate Chaining

 In this method, each cell of the array that stores the list is a linked list of items.

 If an item has an index (given by the hash function), it will be added to the linked
list of the cell, if another item is given the same index (by the hash function, then it
will be also added to the linked list of the cell.
 In the case of searching an item , the hash function gives it an , then we search for it
in the linked list at the cell .

Faculty of Information Technology - Computer Science Department 9


Separate Chaining Example

 Given that a list having length = 10, and a hash


function: H(x) = x % 10, then to insert(81),
H(81) = 81 % 10 = 1. So store it in the linked
list of .
 If we insert(1), H(1) = 1 % 10 = 1, So we got a
collision, and 1 will be stored in the linked list
of List[1].
 And so on…

Faculty of Information Technology - Computer Science Department 10


Collision Resolution – Linear Probing

 In this method, if item has an index (given by the hash function), it will be
stored in the cell.
 If another item is given the same index (by the hash function), then we find the
first empty cell after and store in that cell.
 If we reach the end of the list we can return back to the start of the list to find an
empty cell.
 In the case of searching an item , if is given the index (by the hash function),
and , we search the next cells until we find , or return back to the cell (in this
case the item is not found).
Faculty of Information Technology - Computer Science Department 11
Linear Probing Example

 Given a list of 8 cells, and the hash


function .
 Consider the given list in the left
column of the next figure.
 If we want to insert 10, 5, and 15, using
the Linear Probing approach, the list
will be as the right column.

Faculty of Information Technology - Computer Science Department 12


Comparison between Separate chaining and Linear probing

Separate Chaining Linear Probing


Harder to implement Easier to implement
Unlimited size, since linked lists are
Has a limited size
dynamic and can be expanded
Could have empty cells Storage is fully utilized

Faculty of Information Technology - Computer Science Department 13

You might also like