Docsity

data structures week, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download data structures week and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

CMPT 225

Data Structures and Programming

Week 10

Hash Tables

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… add(element)

  1. index = element.hashCode()
  2. if (table[index] == null) 2.1 table[index] = element
  3. else, // try adding at index+1, index+2…

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… add(element)

  1. index = element.hashCode()
  2. while (table[index] != null) 2.1 index = index+1 (mod table.length)
  3. table[index] = element

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… contains(element)

  1. index = element.hashCode()
  2. while (table[index] != null && !(table[index].equals(element)) ) 2.1 index = index+1 (mod table.length)
  3. return (table[index].equals(element))

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… contains(element) A D L Q S R O Contains( R) Contains( M) Contains( S) Contains( C)

Idea: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… When removing mark that location as “deleted” remove(element)

  1. index = element.hashCode()
  2. table[index] = deleted Here deleted is a special object used by our Hash, e.g. private static final Object deleted = new Object(); This will require us to revise our insert and contains() methods.

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… We have a special object deleted private static final Object deleted = new Object(); add(element)

  1. index = element.hashCode()
  2. while (table[index] != null && table[index] != deleted ) 2.1 index = index+1 (mod table.length)
  3. table[index] = element

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… contains(element)

  1. index = element.hashCode()
  2. while (table[index] != null && !element.equals(table[index])) 2.1 index = index+1 (mod table.length)
  3. return (element.equals(table[index]) != null) // if we see a cell that was deleted, we keep searching

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… contains(element) A D L de l S R O de l Contains( R) Contains( M) Contains( S) Contains( C)

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… delete (element) A D L de l S R O de l delete(S) A D L de l de l R O de l

Linear probing: when insertion causes collision, check if the next entry is empty, and put the element there. If not go to the right one more… What about add(element) Fix: before adding element, check that it is not already in the table A D de l de l S R O de l add(S)

Open Addressing

variants

Open Addressing – variants Quadratic probing: add(element)

  1. index = element.hashCode()
  2. i=
  3. while (table[index + i 2 ] != null) 2.1 i++
  4. table[index + i 2 ] = element