Download presentation
Presentation is loading. Please wait.
Published byYulia Rachman Modified over 5 years ago
1
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED
2
Hashing Goal Topics Collision handling Rehashing
Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining Open addressing: linear probing, quadratic probing, double hashing Rehashing Load factor
3
Tree Structures Binary Search Trees AVL Trees Splay Trees B-Trees
4
Tree Structures insert / delete / find worst average
Binary Search Trees N log N AVL Trees log N Splay Trees amortized log N* B-Trees amortized log N* *Individual operations may require more than O(log N) time, but a sequence of M operations will average O(M log N) time.
5
Goal Develop a structure that will allow user to insert/delete/find records in constant average time structure will be a table (relatively small) table completely contained in memory implemented by an array capitalizes on ability to access any element of the array in constant time
6
Hash Tables When K is much smaller than U, a hash table requires much less space than a direct-address table Can reduce storage requirements to |K| Can still get O(1) search time, but on the average case, not the worst case
7
Hash Tables Idea: Use a function h to compute the slot for each key Store the element in slot h(k) A hash function h transforms a key into an index in a hash table T[0…m-1]: h : U → {0, 1, , m - 1} We say that k hashes to slot h(k) Advantages: Reduce the range of array indices handled: m instead of |U| Storage is also reduced
8
Example: HASH TABLES U (universe of keys) h(k1) h(k4) k1 K (actual
U (universe of keys) h(k1) h(k4) k1 K (actual keys) k4 k2 h(k2) = h(k5) k5 k3 h(k3) m - 1
9
Revisit Example 2
10
Do you see any problems with this approach?
U (universe of keys) h(k1) h(k4) k1 K (actual keys) k4 k2 h(k2) = h(k5) Collisions! k5 k3 h(k3) m - 1
11
Hash Function Thus, since f(x) = x % 15, if x = 25 129 35 2501 47 36
Storing the keys in the array is not a problem. Array: _ _ _ _ _ _ _ _ _
12
Hash Function What happens when you try to insert: x = 65 ? x = 65
f(x) = 5 Array: _ _ _ _ _ _ _ _ _ 65(?)
13
Hash Function What happens when you try to insert: x = 65 ? x 65
f(x) 5 Array: 65(?) This is called a collision.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.