Download presentation
Presentation is loading. Please wait.
Published byIlene Watts Modified over 9 years ago
1
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Information Technology, 3’rd Semester Lecture 17: An Overview of Hashing
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. dictionaries Hash tables, sometimes also called dictionaries. HTs are data structures that store values along with identification keys for easy access. 3 Presented & Prepared by: Mahmoud R. Alfarra
4
What is Hash tables? dictionaries 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. 4 Presented & Prepared by: Mahmoud R. Alfarra
5
What is Hash tables? 5 Presented & Prepared by: Mahmoud R. Alfarra Keys Given a student ID find the record (entry)
6
What is Hash tables? 6 Presented & Prepared by: Mahmoud R. Alfarra key Each data item is stored in the array based on some piece of the data, called the key. hash function 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.
7
hash function What is a hash function ? 7 Presented & Prepared by: Mahmoud R. Alfarra 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.
8
What is a hash function? 8 Presented & Prepared by: Mahmoud R. Alfarra 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
9
What is a hash function? 9 Presented & Prepared by: Mahmoud R. Alfarra A hash table supports fast retrieval O(1) fast deletion O(1) fast insertion O(1)
10
Hash method works something like this zzzzzzzz Domain: "!".. "zzzzzzzz"Range: 0... 9996 hash(key) AAAAAAAA 8482 hash(key) 1273 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
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 Presented & Prepared by: Mahmoud R. Alfarra Could use String keys each ASCII character equals some unique integer "able" = 97 + 98 + 108 + 101 == 404
13
81 82 83 84 85 86 308 A hash table after three insertions using the too simple hash code method "abba" Keys 80... 0 insert objects with these three keys: "abba" "abcd" "abce"... "abcd" "abce"
14
Collision occurs while inserting "baab" can't insert "baab" where it hashes to same slot as "abba" Linear probe forward by 1, inserting it at the next available slot "baab" Try [81] Put in [82] 81 82 83 84 85 86 308 "abba" 80... 0 "abcd" "abce" "baab"
15
Wrap around when collision occurs at end Insert "KLMP" "IKLT" both of which have a hash value of 308 81 82 83 84 85 86 308 "abba" 80... 0 "abcd" "abce" "baab" "KLMP" "IKLT"
16
Find object with key "baab 81 82 83 84 85 86 308 "abba" 80... 0 "abcd" "abce" "baab" "KLMP" "IKLT" "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
17
Emank X Mezank !! قال النبي صلى الله عليه وسلم: قولوا لا إله إلا الله تُفلحـوا
18
Next Lecture Graphs
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.