Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hash Table (Separate Chaining) Assume we have 3 buckets, and the hash function is f(x) = (x % 3), sorting key is the integer itself After inserting 2,3,5,

Similar presentations


Presentation on theme: "Hash Table (Separate Chaining) Assume we have 3 buckets, and the hash function is f(x) = (x % 3), sorting key is the integer itself After inserting 2,3,5,"— Presentation transcript:

1 Hash Table (Separate Chaining) Assume we have 3 buckets, and the hash function is f(x) = (x % 3), sorting key is the integer itself After inserting 2,3,5, the hash table will become 0 1 2 25 3 NULL

2 Hash Table (Separate Chaining) If we further insert 0 and 4, then the hash table become 0 1 2 4 25 03

3 Student Table Assume we have 3 buckets, and the hash function is the sum of the digits of StudentID and then mod 3 Also, the sorting key is the alphabetic order of the StudentID

4 Student Table Inserting the following records –[“03123456”, “Tom”, Year 1, Male] –[“03000002”, “Mary”, Year 3, Female] –[“03123123”, “John”, Year 2, Male] 0 1 2 [“03123123”, “John”, Year 2, Male][“03123456”, “Tom”, Year 1, Male] [“03000002”, “Mary”, Year 3, Female] NULL

5 Registration Table Sorting key is firstly the StudentID, and then secondly the CourseCode Inserting the following records: –[“03123455”, “COMP171”, 10] –[“03123455”, “COMP151”, 50] –[“03000000”, “COMP104”, 30] [“03000000”, “COMP104”, 30][“03123455”, “COMP151”, 50][“03123455”, “COMP171”, 10] Root

6 Index (key: StudentID) The address are shown above the nodes 0 1 2 [“03000000”, pointer = 0x12345678] [“03123455”, pointer = 0x00001234][“03123455”, pointer = 0x111222AA] NULL 0x123456780x000012340x111222AA [“03000000”, “COMP104”, 30][“03123455”, “COMP151”, 50][“03123455”, “COMP171”, 10] Root (Same key, either one first)

7 Index (key: CourseCode) Assume the hash function is the sum of the three digits in the course code mod 3 0x123456780x000012340x111222AA [“03000000”, “COMP104”, 30][“03123455”, “COMP151”, 50][“03123455”, “COMP171”, 10] Root 0 1 2 [“COMP171”, pointer = 0x111222AA] [“COMP104”, pointer = 0x12345678] [“COMP151”, pointer = 0x00001234]

8 Example Assume there are 3 student [ID] (A,B,C) and 3 courses [code] (X,Y,Z) Now there are the following registrations: –Student A takes course X –Student B takes course Y –Student C takes course X –Student A takes course Z Assume the hash value of students (2 buckets) are –Student A = 0 –Student B = 1 –Student C = 0 Assume the hash value of courses (2 buckets) are –Course X = 0 –Course Y = 1 –Course Z = 1 Further assume the linked list is a singly-linked list (for illustration only)

9 [A, Z, 40] 0x123412340x111222AA [B, Y, 20] [C, pointer= 0x00001234] 0x12345678 [A, X, 10] 0x00001234 [C, X, 30] [A, pointer= 0x12345678][B, pointer= 0x111222AA][A, pointer= 0x12341234] Root Course Selection Linked List 0 1 Student Index Hash Table [X, pointer= 0x12345678][Y, pointer= 0x111222AA][X, pointer= 0x00001234] 0 1 Course Index Hash Table [Z, pointer= 0x12341234] Insertion (add registration): Student B takes course Y Student A takes course X Student C takes course X Student A takes course Z [Same sorting key, any ordering] Example (Cont’)


Download ppt "Hash Table (Separate Chaining) Assume we have 3 buckets, and the hash function is f(x) = (x % 3), sorting key is the integer itself After inserting 2,3,5,"

Similar presentations


Ads by Google