notes54 - CS251 Notes - Feb 13 Collision Handling Techniques

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS251 Notes - Feb 13 Collision Handling Techniques Chaining (appending to bucket) Linear Probing Double Hashing Linear Probing Store <key, data> pair in bucket itself Only maxSize-1 items are allowed in the table Advantage  Requires no dynamic memory allocation linear_probing.insert ( k ) { if ( table is full ) return error; int probe = hash ( k ); while ( table [ probe ] is in use ) probe = ( probe + 1 ) % maxSize; table [ probe ]._key = k; table [ probe ]._data = d; } Example hash ( k ) = k % 7 Empty insert (18), 18 % 7 = 4 insert (41), 41 % 7 = 6 insert (22), 22 % 7 = 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
insert (43), 43 % 7 = 1,  collision , increase prob to 2. Double Hashing Is like linear probing, but instead of incrementing by 1 on collision, the  probe is incremented by the value obtained from a second hash  function. double_hash.insert ( k, d ) { if ( table is full ) return error; probe = hash1 ( k ); offset = hash2 ( k ); while ( table [ probe ] is in use ) probe = (probe + offset) % maxSize; table [ probe ]._key = k; table [ probe ]._data = d; } Advantage:  Double hashing distributes keys better across the hash  table. Example h1 ( k ) = k % 13; h2 ( k ) = 8 - k % 8 Empty insert (18), 18 % 13 = 5
Background image of page 2
insert (41), 41 % 13 = 2 insert (22), 22 % 13 = 9 insert (44), 44 % 13 = 5,  collision offset = h2 ( 44 ) = 8 - 44 % 8 = 4 probe = 5 + 4 = 9,  another collision probe = 9 + 4 = 13. Hash tables preform well, Insert, Remove and Find all run in O(1) provided n  << N 1. When n is the number of items and N is the max size. 2. And the has function uniformly distributes the items evenly Otherwise it can take O(n) in the worst case. Example implementation //HashTable.h struct HashTableNode { char * _key; void * _data; HashTableNode * _next; } class HashTable { enum { maxSize = 1037; } // 1037 is prime, primes reduce HashTableNode * _table [ maxSize ]; public: HashTable ( ); bool add ( char * key, void * data ); void * find ( char * key ); bool remove ( char * key ); private: int Hash ( char * key ); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
//HashTable.cc #include "HashTable.h"
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 12

notes54 - CS251 Notes - Feb 13 Collision Handling Techniques

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online