Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.

Similar presentations


Presentation on theme: "Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records."— Presentation transcript:

1 Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records Sometimes the key is the whole record Searching a table Given a key k and a table T = (k 1,i 1 ),…, (k n,i n ), find the pair (k j,i j ) in T such that k=k j (if it exists) Possible approaches: –Sequential search: simple, but only effective for small tables –Binary search: fast, but table must be sorted –Hashing 1COSC 2P03 Week 11

2 Hash Tables Idea: use a function f such that for every possible key k, f(k) = index of record with key k: O(1) time Hash Function: maps keys to addresses Build the table using the hash function: if f(k)=1 then put record (k,i) in cell 1 of table (etc) Hash functions should ideally be: 1.Easy to compute, and 2.Ensure different keys are always mapped to different cells Perfect hash functions are not always possible COSC 2P03 Week 112

3 Hashing and Collisions Collision: the effect of more than one key being mapped to the same cell –Given k x ≠k y, we have f(k x ) = f(k y ) Approaches to dealing with collisions: 1.Allow >1 record to be stored in each table index –Separate chaining: each index has a linked list of records –Buckets: each index is a fixed-size bucket of records 2.Open addressing: allow only 1 record at each index –When a collision occurs, use a collision resolution policy to find a new index for the item, e.g. linear probing etc. COSC 2P03 Week 113

4 Hash table findPos – Linear Probing If a record is hashed to index j, which is already occupied, then look in index j+1, j+2, … and put the record in the next available index (each attempt is called a probe) int findPos(int k) // search for index that should store // record with key k { current = hash(k, tableSize); while(array[current] != null && array[currentPos].record.key != k) { current = (current+1) % tableSize; } return current; } 4COSC 2P03 Week 11

5 Hash Table search int find(int k) // search for record with key k { current = findPos(k); if(isActive(current)) return current; else return -1; // not found } 5COSC 2P03 Week 11

6 Hash Table – insertion void insert(record R) // insert R if not already in table { current = findPos(R.key); if(isActive(current)) // already in hash table return; else { array[current].record = R; array[current].isActive = true; } 6COSC 2P03 Week 11

7 Hash table – deletion void remove(int k) // delete record with key k { current = findPos(R.key); if(isActive(currentPos)) array[current].isActive = false; } 7COSC 2P03 Week 11

8 Open addressing – collision resolution policies Linear probing: If a record is hashed to index j, which is already occupied, then look in index j+1, j+2, … and put the record in the next available index Quadratic probing: If a record is hashed to index j, which is already occupied, then look in index j+1, j+4, j+9, …, and put the record in the next available index. Double hashing: If h 1 (key) hashes a record to index j, which is already occupied, then use h 2 (key) as a step size COSC 2P03 Week 118


Download ppt "Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records."

Similar presentations


Ads by Google