Algorithms ROBERT SEDGEWICK KEVIN WAYNE - Princeton University

Transcription

RX

Linear-probing hash table demo: insertLinear-probing hash table demo: insertHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Insert. Put at table index i if free; if not try i 1, i 2, etc.Insert. Put at table index i if free; if not try i 1, i 2, etc.linear-probing hash tableinsert Lhash(L) 4567ACSH891011Linear-probing hash table demo: insertLinear-probing hash table demo: insertHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Insert. Put at table index i if free; if not try i 1, i 2, etc.Insert. Put at table index i if free; if not try i 1, i 2, etc.insert Lhash(L) 6keys[]1213E1415RX1415RXinsert Lhash(L) CSHL8910E111213

Linear-probing hash table demo: insertLinear-probing hash table demo: insertHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Insert. Put at table index i if free; if not try i 1, i 2, etc.Insert. Put at table index i if free; if not try i 1, i 2, etc.insert Linsert Lhash(L) 6hash(L) 45678ACSHL910111213E1415RXLLinear-probing hash table demo: insertLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Insert. Put at table index i if free; if not try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.linear-probing hash tablekeys[]01PM23linear-probing hash SHL910E1112131415RX

Linear-probing hash table demo: searchLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.search Esearch Ehash(E) 10hash(E) 2345678ACSHL910111213E1415RXEsearch hit(return corresponding value)Linear-probing hash table demo: searchLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.linear-probing hash tablekeys[]01PM23search Lhash(L) 10E1112131415RX

Linear-probing hash table demo: searchLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.search Lsearch Lhash(L) 6hash(L) 345678ACSHLL910111213E1415RXLLinear-probing hash table demo: searchLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.linear-probing hash tablesearch Lhash(L) 6keys[]01PM2345678ACSHL910ELsearch hit(return corresponding 1415RX

Linear-probing hash table demo: searchLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.search Ksearch Khash(K) 5hash(K) 345678ACSHL910111213E1415RXKLinear-probing hash table demo: searchLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.search Khash(K) 5keys[]search Khash(K) 8ACSHLK910E1112131415RX

Linear-probing hash table demo: searchLinear-probing hash table demo: searchHash. Map key to integer i between 0 and M 1.Hash. Map key to integer i between 0 and M 1.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.Search. Search table index i; if occupied but no match, try i 1, i 2, etc.search Ksearch Khash(K) 5hash(K) 345678ACSHLK91011E12131415RXKsearch miss(return null)Linear-probing symbol table: Java implementationLinear-probing symbol table: Java implementationpublic class LinearProbingHashST Key, Value {private int M 30001;private Value[] vals (Value[]) new Object[M];private Key[]keys (Key[])new Object[M];private int hash(Key key){ /* as beforepublic class LinearProbingHashST Key, Value {private int M 30001;private Value[] vals (Value[]) new Object[M];private Key[]keys (Key[])new

・Collision resolution: Algorithm and data structure to handle two keys that hash to the same array index. Classic space-time tradeoff. ・No space limitation: trivial hash function with key as index. ・No time limitation: trivial collision resolution with sequential search. ・Space and time limitations: hashing (the real world).