CS 04
CS 04
• h(k) = | ak + b | mod n
22
Compression Maps
• h(k) = k mod m
b
• Option 1: m = 2
h(k) gives the b least significant bits of k
All keys with the same ending go to the same place.
• Option 2: m is prime
• Ensures uniform distribution
• Avoid using prime numbers to close to a power of 2.
23
Compression Maps
• h(k) = ⌊m(k A mod 1)⌋ 0<A<1
p
• Value of m is not critical. Can be chosen as 2
5−1
A= corresponds to Fibonacci Hashing
2
24
Compression Maps
• h(k) = | ak + b | mod n
25
Universal Hashing
• Create a set of hash functions H, from which h can be randomly
selected.
26
Handling Collision
• A general class of techniques for handling collisions is Open Addressing
27
Open Addressing
• Probing can involve multiple attempts to find an available slot
28
Linear Probing
• If there is a collision, the probe advances to the next slot
29
Linear Probing . . . . . . . . . .
• h(k) = k mod 10 0 1 2 3 4 5 6 7 8 9
Insert 89 . . . . . . . . . 89
0 1 2 3 4 5 6 7 8 9
Insert 18 . . . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 49 49 . . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 58 49 58 . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
30
Linear Probing
• How to perform deletion?
. . . . . . . . . . . .
0 1 2 3 4 5 6 7 8 9 10 11
31
Double Hashing
• Use two hash functions h1(k) and h2(k)
• h1(k) is the position in the table where we first check for key k
• DoubleHashing_Insert(k):
if (table is full) error; probe = h1(k); offset = h2(k)
while (table[probe] is occupied)
probe = (probe + offset) mod m
table[probe] = k
32
Double Hashing Insert 89 . . . . . . . . . 89
h1(k) = k mod 10 0 1 2 3 4 5 6 7 8 9
h2(k) = 7 − (k mod 7) Insert 18 . . . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 49 . . . . . . 49 . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 58 . . . 58 . . 49 . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 69 69 . . 58 . . 49 . 18 89
0 1 2 3 4 5 6 7 8 9
33
Double Hashing Example
h1(k) = k mod 13 . . . . . 18 . . . . . . .
h2(k) = 8 − (k mod 8) 0 1 2 3 4 5 6 7 8 9 10 11 12
. . 41 . . 18 . . . . . . .
0 1 2 3 4 5 6 7 8 9 10 11 12
Insert
18 . . 41 . . 18 . . . 22 . . .
41 0 1 2 3 4 5 6 7 8 9 10 11 12
22
44 . 41 . . 48 . . . 22 . . .
44
59 0 1 2 3 4 5 6 7 8 9 10 11 12
32 44 . 41 . . 48 . 59 . 22 . . .
31
73 0 1 2 3 4 5 6 7 8 9 10 11 12
44 . 41 73 . 48 32 59 31 22 . . .
34
Analysis of Double Hashing
• The load factor λ is less than 1
• We assume that every probe looks at a random location in the table
35
• Average number of probes for a successful search = average number of
probes required to insert all elements
36
m m m m
• No of probes required to insert 2 + 4 + 8 + … + 2i elements
1
= no of probes required to leave i fraction of the table empty = m×i
2
(1 − λ)
1
= m log = − m log(1 − λ)
(n) (λ)
m 1
− log(1 − λ) = − log(1 − λ)
37
Expected number of probes
• Load factor λ < 1 for probing
unsuccessful Successful
Chaining
O(1 + λ) O(1 + λ)
(1 − λ) (λ 1 − λ)
Probing
1 1 1
O O ln
38
Trees
1
Definitions A
• A, B, C, H — internal nodes
• D, E, F, G, I — leaf nodes B C
• The root has level 0
• The depth of a node is its level in D E F G H
the tree
4
Example Tree
5
Binary Tree
• An ordered tree is one in which the children of each node are ordered.
• Binary tree is an ordered tree with all nodes having at most 2 children
A
B C
D F H
6
Recursive Definition of Binary Tree
• A binary tree is either a
• leaf or
A
• An internal node (the
root) and one/two
B C
binary trees (left subtree
and/or right subtree).
D F H