Docsity

Hash Tables and Dictionaries: Implementations and Optimizations, Slides of Computer Science

Partial preview of the text

Download Hash Tables and Dictionaries: Implementations and Optimizations and more Slides Computer Science in PDF only on Docsity!

Dictionaries

  • Collection of pairs.

 (key, element)  Pairs have different keys.

  • Operations.

 get(theKey)  put(theKey, theElement)  remove(theKey)

Application

  • Collection of student records in this class.  (key, element) = (student name, linear list of assignment and exam scores)  All keys are distinct.
  • Get the element whose key is John Adams.
  • Update the element whose key is Diana Ross.

 put() implemented as update when there is already a pair with the given key.  remove() followed by put().

Represent As A Linear List

  • L = (e 0 , e 1 , e 2 , e 3 , …, e (^) n-1 )
  • Each ei is a pair (key, element).
  • 5-pair dictionary D = (a, b, c, d, e).

 a = (aKey, aElement), b = (bKey, bElement), etc.

  • Array or linked representation.

Array Representation

a b c d e

  • get(theKey)

 O(size) time

  • put(theKey, theElement)

 O(size) time to verify duplicate, O(1) to add at right end.

  • remove(theKey)

 O(size) time.

Unsorted Chain

  • get(theKey)

 O(size) time

  • put(theKey, theElement)

 O(size) time to verify duplicate, O(1) to add at left end.

  • remove(theKey)

 O(size) time.

a b c d e

firstNode null

Sorted Chain

  • Elements are in ascending order of Key.
  • get(theKey)

 O(size) time

  • put(theKey, theElement)

 O(size) time to verify duplicate, O(1) to put at proper place.

A B C D E

firstNode null

Skip Lists

  • Worst-case time for get, put, and remove is

O(size).

  • Expected time is O(log size).
  • We’ll skip skip lists.

Hash Tables

  • Worst-case time for get, put, and remove is

O(size).

  • Expected time is O(1).

[0] [1] [2] [3] [4] [5] [6] [7]

Ideal Hashing Example

  • Pairs are: (22,a), (33,c), (3,d), (73,e), (85,f).
  • Hash table is table[0:7], b = 8.
  • Hash function is key/11.
  • Pairs are stored in table as below:

(3,d) (22,a) (33,c) (73,e) (85,f)

  • get, put, and remove take O(1) time.

What Can Go Wrong?

  • Where does (26,g) go?
  • Keys that have the same home bucket are synonyms.  22 and 26 are synonyms with respect to the hash function that is in use.
  • The home bucket for (26,g) is already occupied.

[0] [1] [2] [3] [4] [5] [6] [7]

(3,d) (22,a) (33,c) (73,e) (85,f)

Hash Table Issues

  • Choice of hash function.
  • Overflow handling method.
  • Size (number of buckets) of hash table.

Hash Functions

  • Two parts:

 Convert key into an integer in case the key is not an integer.

  • Done by the method hashCode().
  • Map an integer into a home bucket.

 f(k) is an integer in the range [0, b-1], where b is the number of buckets in the table.

String To Nonnegative Integer

public static int integer(String s)

{

int length = s.length(); // number of characters in s int answer = 0; if (length % 2 == 1) {// length is odd answer = s.charAt(length - 1); length--; }

String To Nonnegative Integer

// length is now even for (int i = 0; i < length; i += 2) {// do two characters at a time answer += s.charAt(i); answer += ((int) s.charAt(i + 1)) << 16; } return (answer < 0)? -answer : answer;

}