lec18 - Lecture 18 Separate chaining Dictionary data types...

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

View Full Document Right Arrow Icon
Page 1 of 31 CSE 100, UCSD: LEC 18 Lecture 18 Separate chaining Dictionary data types Hashtables vs. balanced search trees A hashtable implementation: java.util.Hashtable Object serialization in Java Reading: Weiss, Ch 5; and JDK source code
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 of 31 CSE 100, UCSD: LEC 18 Final exam Final exam time: Wed Dec 9 11:30am-2:00pm Location: lecture room Closed book, closed notes, no calculators. .. Bring something to write with, and picture ID Practice final is on line Final exam discussion topic on Webboard Exam review sessions will be: last lecture Coverage: All lectures, all assignments, corresponding sections of the textbook, and handouts
Background image of page 2
Page 3 of 31 CSE 100, UCSD: LEC 18 Open addressing vs. separate chaining Linear probing, double and random hashing are appropriate if the keys are kept as entries in the hashtable itself. .. doing that is called "open addressing" it is also called "closed hashing" Another idea: Entries in the hashtable are just pointers to the head of a linked list (“chain”); elements of the linked list contain the keys. .. this is called "separate chaining" it is also called "open hashing" Collision resolution becomes easy with separate chaining: no need to probe other table locations; just insert a key in its linked list if it is not already there. (It is possible to use fancier data structures than linked lists for this; but linked lists work very well in the average case, as we will see)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Page 4 of 31 CSE 100, UCSD: LEC 18 Separate chaining: basic algorithms When inserting a key K in a table with hash function H(K) 1. Set indx = H(K) 2. Insert key in linked list headed at indx. (Search the list first to avoid duplicates.) When searching for a key K in a table with hash function H(K) 1. Set indx = H(K) 2. Search for key in linked list headed at indx, using linear search. When deleting a key K in a table with hash function H(K) 1. Set indx = H(K) 2. Delete key in linked list headed at indx Advantages: average case performance stays good as number of entries approaches and even exceeds M; delete is easier to implement than with open addressing Disadvantages: requires dynamic data, requires storage for pointers in addition to data, can have poor locality which causes poor caching performance
Background image of page 4
Page 5 of 31 CSE 100, UCSD: LEC 18 Separate chaining, an example M = 7, H(K) = K mod M insert these keys 701, 145, 217, 19, 13, 749 in this table, using separate chaining: index: 0 1 2345 6
Background image of page 5

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

View Full DocumentRight Arrow Icon
Page 6 of 31 CSE 100, UCSD: LEC 18 Analysis of separate-chaining hashing Keep in mind the load factor measure of how full the table is: α = N/M where M is the size of the table, and N is the number of keys that have been inserted in the table With separate chaining, it is possible to have α > 1 Given a load factor α , we would like to know the time costs, in the best, average, and worst case of new-key insert and unsuccessful find (these are the same) successful find The best case is O(1) and worst case is O(N) for all of these. .. let’s analyze the average case
Background image of page 6
Page 7 of 31
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

lec18 - Lecture 18 Separate chaining Dictionary data types...

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

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