=========================================================================== CSC 263H Tutorial Outline for Week 7 Winter 2004 =========================================================================== ------- Hashing ------- - Exercise R-2.19 on page 132 of the textbook. 0: 1: -> 20 2: 3: 4: -> 5 -> 16 5: -> 11 -> 88 -> 44 6: -> 39 -> 94 7: -> 23 -> 12 8: 9: -> 13 10: - Write pseudo-code for performing SEARCH, INSERT, and DELETE on a hash table T of size m, where collisions are resolved using double hashing with primary hash function h and secondary hash function h_2. Assume that the two hash functions are well-designed, i.e., that the probe sequence for any key k is a permutation of the indices 0,1,...,m-1. Also, the hash table stores elements x with a key[x] field, following the Dictionary ADT. Hint: For DELETE, you will have to come up with a way of keeping track of the difference between an empty location (where no element has ever been stored) and a deleted location. This was covered in class at the Mississauga and Scarborough campuses, but not on the St. George campus. Assume we have a special element DEL that is guaranteed to be different from any actual element, and such that key[DEL] is guaranteed to be different from all actual keys. During the search phase of the various algorithms, an empty element (denoted by NIL) indicates the end of the search (the element sought was not found). DEL is used to mark the places where elements were removed, so that the various algorithms know there may be other elements further along the probe sequence. DELETE(x): // Examine locations one-by-one following the probe sequence for k // = key[x], until x is found (and replaced by the special element // DEL to indicate a deleted element) or an empty location is found // (meaning x is not in the table and nothing needs to be done). for i := 0 to m-1: b := (h(key[x]) + i * h_2(key[x])) mod m if T[b] = x: T[b] := DEL break if T[b] = NIL: break INSERT(x): // Examine locations one-by-one following the probe sequence for k // = key[x], until an empty or previously deleted location is found // (where x can be stored). for i := 0 to m-1: b := (h(key[x]) + i * h_2(key[x])) mod m if T[b] = NIL or T[b] = DEL: T[b] := x break SEARCH(k): // Examine locations one-by-one following the probe sequence for k, // until k is found (and the element stored with k is returned), an // empty location is found (meaning k is not in the table), or the // entire table has been examined without finding k. for i := 0 to m-1: b := (h(k) + i * h_2(k)) mod m if T[b] = NIL: break if key[T[b]] = k: return T[b] return NIL