1 / 18

Data structure

MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE. Using C#. Information Technology , 3’rd Semester. Data structure. Lecture 17: An Overview of Hashing. Presented By: Mahmoud Rafeek Alfarra. Outline. What is Hashing? What is Hash tables?

kirra
Télécharger la présentation

Data structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Using C# Information Technology , 3’rd Semester Data structure Lecture 17: An Overview of Hashing Presented By: Mahmoud Rafeek Alfarra

  2. Outline • What is Hashing? • What is Hash tables? • What is a hash function? • Example: hash function using string • Emank X Mezank !!

  3. What is Hashing? • Hashing is a very common technique for storing data in such a way the data can be inserted and retrieved very quickly. • Hashing uses a data structure called a hash table. • Hash tables, sometimes also called dictionaries. • HTs are data structures that store values along with identification keys for easy access. Presented & Prepared by: Mahmoud R. Alfarra

  4. What is Hash tables? 4 • Hash tables, sometimes also called dictionaries. • HTs are data structures that store values along with identification keys for easy access. • A hash table data structure is designed around an array. Presented & Prepared by: Mahmoud R. Alfarra

  5. What is Hash tables? 5 Keys Given a student ID find the record (entry) Presented & Prepared by: Mahmoud R. Alfarra

  6. What is Hash tables? 6 • Each data item is stored in the array based on some piece of the data, called the key. • To store an element in the hash table, the key is mapped into a number in the range of 0 to the hash table size using a function called a hash function. Presented & Prepared by: Mahmoud R. Alfarra

  7. What is a hash function? 7 • The ideal goal of the hash function is to store each key in its own cell in the array. • However, because there are an unlimited number of possible keys and a finite number of array cells, a more realistic goal of the hash function is to attempt to distribute the keys as evenly as possible among the cells of the array. Presented & Prepared by: Mahmoud R. Alfarra

  8. What is a hash function? 8 • Choosing a hash function depends on the data type of the key you are using. • If your key is an integer, the simplest function is to return the key modulo the size of the array. f(k) = k%size f(4) = 4%10 = 4 Presented & Prepared by: Mahmoud R. Alfarra

  9. What is a hash function? 9 • A hash table supports • fast retrieval O(1) • fast deletion O(1) • fast insertion O(1) Presented & Prepared by: Mahmoud R. Alfarra

  10. Hash method works something like this Convert a String key into an integer that will be in the range of 0 through the maximum capacity-1 Assume the array capacity is 9997 hash(key) AAAAAAAA 8482 1273 zzzzzzzz hash(key) Domain: "!" .. "zzzzzzzz" Range: 0 ... 9996

  11. Hash method • What if the ASCII value of individual chars of the string key added up to a number from ("A") 65 to possibly 488 ("zzzz") 4 chars max • If the array has size = 309, mod the sum 390 % TABLE_SIZE = 81 394 % TABLE_SIZE = 85 404 % TABLE_SIZE = 95 • These array indices store these keys 81 85 95 abba abcd able

  12. Example: hash function using string 12 • Could use String keys each ASCII character equals some unique integer • "able" = 97 + 98 + 108 + 101 == 404 Presented & Prepared by: Mahmoud R. Alfarra

  13. A hash table after three insertions using the too simple hash code method 0 Keys ... insert objects with these three keys: "abba" "abcd" "abce" 80 "abba" 81 82 83 84 "abcd" 85 "abce" 86 ... 308

  14. Collision occurs while inserting "baab" can't insert "baab" where it hashes to same slot as "abba" 0 ... 80 "baab" Try [81] Put in [82] "abba" 81 "baab" 82 Linear probe forward by 1, inserting it at the next available slot 83 84 "abcd" 85 "abce" 86 ... 308

  15. Wrap around when collision occurs at end "IKLT" 0 Insert "KLMP" "IKLT" both of which have a hash value of 308 ... 80 "abba" 81 "baab" 82 83 84 "abcd" 85 "abce" 86 ... "KLMP" 308

  16. Find object with key "baab "IKLT" 0 "baab" still hashes to 81, but since [81] does not hold it, linear probe to [82] At this point, you could return a reference to it or remove it ... 80 "abba" 81 "baab" 82 83 84 "abcd" 85 "abce" 86 ... "KLMP" 308

  17. Emank X Mezank !! قال النبي صلى الله عليه وسلم: قولوا لا إله إلا الله تُفلحـوا

  18. Next Lecture Graphs

More Related