180 likes | 526 Views
0. 1. 2. 3. NULL. 2. 5. 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. 0. 3. 4. 2. 5. Hash Table (Separate Chaining).
E N D
0 1 2 3 NULL 2 5 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 0 3 4 2 5 Hash Table (Separate Chaining) • If we further insert 0 and 4, then the hash table become
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
0 1 2 [“03123123”, “John”, Year 2, Male] [“03123456”, “Tom”, Year 1, Male] NULL [“03000002”, “Mary”, Year 3, Female] Student Table • Inserting the following records • [“03123456”, “Tom”, Year 1, Male] • [“03000002”, “Mary”, Year 3, Female] • [“03123123”, “John”, Year 2, Male]
Root [“03000000”, “COMP104”, 30] [“03123455”, “COMP151”, 50] [“03123455”, “COMP171”, 10] 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]
0 1 2 [“03000000”, pointer = 0x12345678] NULL [“03123455”, pointer = 0x111222AA] [“03123455”, pointer = 0x00001234] 0x12345678 0x00001234 0x111222AA Root [“03000000”, “COMP104”, 30] [“03123455”, “COMP151”, 50] [“03123455”, “COMP171”, 10] Index (key: StudentID) • The address are shown above the nodes (Same key, either one first)
0 1 2 [“COMP171”, pointer = 0x111222AA] [“COMP151”, pointer = 0x00001234] [“COMP104”, pointer = 0x12345678] 0x12345678 0x00001234 0x111222AA Root [“03000000”, “COMP104”, 30] [“03123455”, “COMP151”, 50] [“03123455”, “COMP171”, 10] Index (key: CourseCode) • Assume the hash function is the sum of the three digits in the course code mod 3
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)
Course Selection Linked List 0x12345678 0x12341234 0x111222AA 0x00001234 [A, X, 10] [A, Z, 40] [B, Y, 20] [C, X, 30] Root Student Index Hash Table [C, pointer= 0x00001234] [A, pointer= 0x12345678] 0 [A, pointer= 0x12341234] 1 [B, pointer= 0x111222AA] Course Index Hash Table [X, pointer= 0x12345678] 0 [X, pointer= 0x00001234] 1 [Z, pointer= 0x12341234] [Y, pointer= 0x111222AA] • Insertion (add registration): Example (Cont’) • Student A takes course X • Student B takes course Y • Student C takes course X • Student A takes course Z [Same sorting key, any ordering] [Same sorting key, any ordering]