complex libraries using hash dictionaries
play

Complex Libraries Using Hash Dictionaries 1 Playing Hash Table - PowerPoint PPT Presentation

Complex Libraries Using Hash Dictionaries 1 Playing Hash Table You are the new produce manager of the local grocery store. You want to use a dictionary to track your fruit inventory. Entries have the form (banana, 20) where o


  1. Complex Libraries

  2. Using Hash Dictionaries 1

  3. Playing Hash Table You are the new produce manager of the local grocery store. You want to use a dictionary to track your fruit inventory. Entries have the form (“banana”, 20) where o “banana” is the key o 20 is associated data, like the number of cases in stock  Let’s observe your initial interactions with a hypothetical hash dictionary library 2

  4. This is what This is what This is your side 0 is going on in is going on in the library the library 1 2 Client Implementation 3 4 You begin by 5 creating a Create a new hash dictionary 6 new dictionary 7 Here you go! 8 9  This library uses separate-chaining hash tables to implement dictionaries  It decides on an initial capacity of 10 o it’s probably self -resizing 3

  5. Client Implementation 0 1 Insert A = (“apple”, 20) 2 What’s the key of (A)? 3 4  new dictionary 5 Next, you insert 6 A = (“apple”,20) 7 8 9  Why is the library asking this? o it does not know what entries are  (A) is just a pointer to some struct  no sense of what’s in it  You need to tell it 4

  6. Client Implementation 0 1 Insert A = (“apple”, 20) 2 What’s the key of (A)? 3 4 “apple”  new dictionary 5 What’s its hash value? Next, you insert 6 A = (“apple”,20) 7 8 9  Why is the library asking this? o it does not know the type of keys o even if it did, there are many ways to hash them  You need to tell it 5

  7. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 Insert A = (“apple”, 20) "grape" -581390202 "lemon" -665562942 2 "lime" 2086736531 What’s the key of (A)? "pumpkin" -1189657311 3 4 “apple”  new dictionary 5 What’s its hash value? Next, you insert 6 A = (“apple”,20) -1290151091 7 8 9  -1290151091 % 10 is -1 in C0  o not a valid array index! o the library needs a more robust way to compute the hash index Exercise!  Let’s say it keeps the last digit 6

  8. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A Insert A = (“apple”, 20) "grape" -581390202 "lemon" -665562942 2 "lime" 2086736531 What’s the key of (A)? "pumpkin" -1189657311 3 4 “apple”  new dictionary 5 What’s its hash value? Next, you insert 6 A = (“apple”,20) -1290151091 7 Ok. The hash index is 1 . 8 9 This chain is empty. I can insert entry (A) there. Done  The library asked for o the key of the entry o the hash value of the key Funny! Libraries didn’t ask for anything in the past 7

  9. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A Insert B = (“banana”, 10) "grape" -581390202 "lemon" -665562942 2 "lime" 2086736531 What’s the key of (B)? "pumpkin" -1189657311 3 4 “banana”  new dictionary  insert A = (“apple”, 20) 5 What’s its hash value? 6 Next, you insert 207055587 7 B Ok. The B = (“banana”,10) hash index is 7 . 8 9 This chain is empty. I can insert entry (B) there. Done Same as for (A) 8

  10. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A Insert C = (“pumpkin”, 50) "grape" -581390202 "lemon" -665562942 2 "lime" 2086736531 What’s the key of (C)? "pumpkin" -1189657311 3 4 “pumpkin”  new dictionary  insert A = (“apple”, 20) 5  insert B = (“banana”, 10) What’s its hash value? 6 -1189657311 7 Ok. The hash index is 1 . B Next, you insert It points to a node C = (“pumpkin”,50) 8 for entry (A) 9 What’s the key of (A)?  Why is the library asking this? o it does not know what entries are  (A) is just a pointer to some struct  no sense of what’s in it  You need to tell it 9

  11. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A Insert C = (“pumpkin”, 50) "grape" -581390202 "lemon" -665562942 2 "lime" 2086736531 What’s the key of (C)? "pumpkin" -1189657311 3 4 “pumpkin”  new dictionary  insert A = (“apple”, 20) 5  insert B = (“banana”, 10) What’s its hash value? 6 -1189657311 7 Ok. The hash index is 1 . B Next, you insert It points to a node C = (“pumpkin”,50) 8 for entry (A) 9 What’s the key of (A)? “apple” Is it the same as “pumpkin” ?  Why is the library asking this? o it does not know the type of keys o even if it did, there are many ways to compare them  You need to tell it 10

  12. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A C Insert C = (“pumpkin”, 50) "grape" -581390202  "lemon" -665562942 2 "lime" 2086736531 What’s the key of (C)? "pumpkin" -1189657311 3 In practice, it is 4 easier to insert “pumpkin”  new dictionary new nodes at the  insert A = (“apple”, 20) 5 beginning of a chain  insert B = (“banana”, 10) What’s its hash value? 6 -1189657311 7 Ok. The hash index is 1 . B Next, you insert It points to a node C = (“pumpkin”,50) 8 for entry (A) 9 What’s the key of (A)? “apple” Is it the same as “pumpkin” ? There is no next node. I can insert entry (C) there. No  The library asked for o the key of the entry Done o the hash value of the key o whether two keys are the same 11

  13. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A C Look up “apple” "grape" -581390202 "lemon" -665562942 2 "lime" 2086736531 What’s its hash value? "pumpkin" -1189657311 3 4 -1290151091  new dictionary  insert A = (“apple”, 20) Ok. The hash index is 1 . 5  insert B = (“banana”, 10) It points to a node 6 for entry (A)  insert C = (“pumpkin”, 50) 7 B What’s the key of (A)? Next, you 8 look up “apple” “apple” 9 Is it the same as “apple” ? Yes Found (A)  Looking up a key follows the same steps as inserting an entry 12

  14. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A C Look up “lime” "grape" -581390202 "lemon" -665562942 2 "lime" 2086736531 What’s its hash value? "pumpkin" -1189657311 3 4 2086736531 Ok. The hash index is 1 .  new dictionary It points to a node  insert A = (“apple”, 20) 5 for entry (A)  insert B = (“banana”, 10) 6  insert C = (“pumpkin”, 50) What’s the key of (A)?  look up “apple” 7 B “apple” 8 Next, you look up “lime” 9 Is it the same as “lime” ?  The library goes through the chain node by node 13

  15. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1 A C Look up “lime” "grape" -581390202  "lemon" -665562942 2 "lime" 2086736531 What’s its hash value? "pumpkin" -1189657311 3 4 2086736531 Ok. The hash index is 1 .  new dictionary It points to a node  insert A = (“apple”, 20) 5 for entry (A)  insert B = (“banana”, 10) 6  insert C = (“pumpkin”, 50) What’s the key of (A)?  look up “apple” 7 B “apple” 8 Next, you look up “lime” 9 Is it the same as “lime” ? No Ok. The next node has entry (C) What’s the key of (C)?  The library goes through the “pumpkin” chain node by node Is it the same as “lime” ? 14

  16. Key Hash Client Implementation "apple" -1290151091 0 "berry" -514151789 "banana" 207055587 1  A C Look up “lime” "grape" -581390202   "lemon" -665562942 2 "lime" 2086736531 What’s its hash value? "pumpkin" -1189657311 3 4 2086736531 Ok. The hash index is 1 .  new dictionary It points to a node  insert A = (“apple”, 20) 5 for entry (A)  insert B = (“banana”, 10) 6  insert C = (“pumpkin”, 50) What’s the key of (A)?  lookup “apple” 7 B “apple” 8 Next, you look up “lime” 9 Is it the same as “lime” ? No Ok. The next node has entry (C) What’s the key of (C)?  Looking up a key can “pumpkin” o return the associated Is it the same as “lime” ? entry, or o signal there is no entry There is no No next node with this key I have no “lime” 15

Recommend


More recommend