Hashing: Data Structures and Algorithms in Java
Hashing: Data Structures and Algorithms in Java
Hashing
h Sub set 0
x y
Sub set 1
Domain of h Codomain of h
( Integers ) Sub set 2
position
…..
Sub set i
Hash
item function ….
Sub set n-1
O(1)
Data Structures and Algorithms in Java 3
Contents
K1, val1 0
1
K2, val2 2
Hash function
K3, val3 h: Key index 3
Search: O(1) 4
… 5
Kn, valn 6
7
There are mn functions …
What function should we select? …
m: number of needed blocks for storing items. …
A key can be any value in the set of keys …
There is a situation in which some keys are m-1
mapped to one index? How to resolve this
collision?
Insert B9
Collision
Insert B5 probe: i=1
Collision h(9+1)=h(10)=0 OK
probe: i=1 Insert C2
h(5+1)=h(6)=6 Collision
OK probe: i=1
Insert B2 h(2+1)=h(3)=3 No OK
Collision h(2-1)=h(1)=1 No OK
probe: i=1 probe: i=2, i2=4
h(2+1)=h(3)=3 h(2+4)= 6 No OK
No OK h(2-4), -2<0 No OK
h(2-1)=h(1)=1 probe: i=3, i2=9
OK h(2+9)= 1 No OK
h(2-9)= 2-9<0 No OK
probe: i=4, i2=16
h(2+16)= 8 OK
Chaining Method
• Keys do not have to stored in table itself, each
position of the table is associated with a linked
list or chain of structures whose info fields
store keys or references to keys
• This method is called separate chaining, and a
table of references (pointers) is called a scatter
table (bảng phân phối)
h(K) index of a
linked list of
elements having
the same value of
hash function.
K h(K) index
traverse the
appropriate list to
find the element
having this key.
Coalesced chaining
• A version of chaining called coalesced hashing-
băm theo nhóm sử dụng array- (or coalesced chaining)
combines linear probing with chaining
• An overflow area (vùng tràn, hầm chứa) known
as a cellar can be allocated to store keys for
which there is no room in the table
Index of
next
element
in the
7
same
group
Main
area
When cellar
is full,
inserted
element will
be put to
the main
Cellar: overflow area
region
Mechansm: bottom-up
Insert C2
Collision
Use linear probing
Bucket 3 containing
a space
Insert C2 to bucket
3
bucket
Reference to separate
overflow area
Collision
resolution with
buckets and
overflow area
h(k)
index
Search
and Group k
delete k
End
Delete A4
Linear search in the situation
where both insertion and deletion of keys are permitted
One-to-
one and
onto
mapping
k i
h
Ideal
situation
h(k1) != h(k2), if k1 !=k2
h=?
• File=table.
• Expandable hashing, dynamic hashing, and extendible hashing
methods distribute keys among buckets in a similar fashion
• Data h(Data) index buckets[index]
• The main difference is the structure of the index (directory)
• In expandable hashing and dynamic hashing, a binary tree is used
as an index of buckets
• In extendible hashing, a directory of records is kept in a table
Function h generates
patterns of 5 bits
11001 arrives.
Case of splitting
a bucket Case of increase
depth, expand
An example of extendible hashing the directory
Linear Hashing…
Click to go the
HashSet class
Demonstrating
the operation of
the methods in
class HashMap