hash tables
Download
Skip this Video
Download Presentation
Hash Tables

Loading in 2 Seconds...

play fullscreen
1 / 9

Hash Tables - PowerPoint PPT Presentation


  • 128 Views
  • Uploaded on

Hash Tables. What is a Hash Table?. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element should be placed Mapping is called the hash function. Hash Table Operations. size() isEmpty()

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Hash Tables' - prem


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what is a hash table
What is a Hash Table?
  • Container of elements where each element has an associated key
  • Each key is mapped to a value that determines the table cell where element should be placed
  • Mapping is called the hash function
hash table operations
Hash Table Operations
  • size()
  • isEmpty()
  • find(k) – find element with key k
  • insertItem(k, e) – insert element e with key k
  • removeElement(k) – remove element with key k
  • elements() – return Iterator of elements
  • keys() – return Iterator of keys
array implementation
element is name

key is id number

map id number to array index

Array Implementation

[0]

bob 1234

[1]

[2]

jane 1344

[3]

sally 1354

[4]

[5]

[6]

[7]

[8]

[9]

why a hash table
Why a Hash Table?
  • Random access to items
    • Don’t have to perform search to find item
  • Complexity of find/remove?
hash function
Hash Function
  • The hash function has to map the key to a value
    • Bad option: add up ASCII values of characters in key and % by number of table elements
    • Better option:

for(i=0; i < key.length(); i++)

hashVal= 37*hashVal + key[i]

hashVal %= tableSize

collisions
Collisions
  • It is still possible that two keys will map to the same value
  • Separate chaining
    • each array slot is a pointer to a linked list of elements
    • how would find/insert/remove work?
  • Linear probing
    • walk down the list until you find an empty slot
collisions1
Collisions
  • Quadratic probing
    • A[i+f(j) mod N] for j=0, 1, 2, … where f(j) = j2
  • Double Hashing
    • probe at hash2(x), 2hash2(x), etc
rehashing
Rehashing
  • As table gets full, insertions may slow because of more collisions
  • Rehash by creating table twice as large and reinserting all elements into new table
    • requires rehashing all keys (because of new table size)
ad