This preview shows pages 1–11. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Hashing and Hash Tables Nyhoff: 12 COMP152 Spring 2010 D.Y. Yeung 1 2 Outline Motivation Hashing Algorithms and Improving the Hash Functions Collision Strategies Open addressing and linear probing Separate chaining 3 Searching BST trees Balanced BST trees (such as an AVL tree) have a search time of O(logN) This is optimal when we consider a comparisonbased searching mechanism Can we make searching even faster? O(1)? Yes, with hashing Has some limitations, but for some applications where insertions and deletions are not frequent it is very suitable C E M O P T U C E T M U O P Balanced BST 3 steps to locate U Lopsided BST 7 steps to locate U 4 Rethinking Keys Again Tree structures discussed so far assume that we can only use the keys for comparison while other operations on the keys are not considered In practice, however, the key can be decomposed into smaller units, for example: 1. Integers consist of digits: can be used as array index 2. Strings consist of letters: we may even perform arithmetic operations on the letters 5 Idea behind Hashing Given a key in the key universe, compute an index into a hash table Index is computed using a hash function Hash Table Index = hash_function( Key ) This is an ultrafast way to search! Time complexity = O(1). What are the things we need to consider? 1. A good hash function for the keys. 2. The table size is limited, so multiple keys may be mapped to the same location! (We call this a collision ) Need to resolve collisions. Hash Tables COMP152 6 Collection of data items x 0 1 2 3 … h(x) x Hash table h a s h f u n c t i o n 7 General Terms Universe U = { u 0 , u 1 , . . . , u n1 } Relatively easy to compute some index given a key Hash support operations: Find() Insert() Delete() Deletion may be unnecessary in some applications 8 Hash Tables vs. Trees Hash tables are only for problems that require fast search Unlike trees No notion of order remember that a tree is sorted if we visit the nodes inorder No way to find the range min and max elements No notion of successor or predecessor Hash tables are generally implemented using an array structure with fixed size 9 Example Applications Compilers use hash tables to keep track of declared variables Online spell checkers We can “hash” an entire dictionary (or the most common words) Allows us to quickly check if words are spelled correctly in constant time 10 Bit Vector Representation entry = 0 if u i is absent; other entry = 1 Find: test entry Insert: set entry to 1 Delete: set entry to 0 Constant time each, independent of the number of keys!...
View
Full
Document
This note was uploaded on 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.
 Spring '10
 D.Y.Yeung
 ObjectOriented Programming, Algorithms, Data Structures

Click to edit the document details