Certainly! Let's create two hash tables for the given set of strings with the specified configurations, and provide detailed illustrations and explanations for insertion and search operations.
Given Strings:
Top, Tree, Root, AVL, Exam, Pass, Fail, DS, PQ, Stack, Pot, Spot, Stop, Post, Queue
2.1 Hash Table 1
Configuration:
- Hash Function: Sum of ASCII codes of characters
- Array Size: 11
- Collision Handling: Separate Chaining (Linked lists at each index)
Step 1: Define the hash function
For a string s,
hash(s) = (sum of ASCII codes of characters in s) % 11
Step 2: Calculate hash values for each string
| String | ASCII Sum | Hash (mod 11) |
|---|
| Top | T(84)+o(111)+p(112) = 307 | 307 % 11 = 10 |
| Tree | T(84)+r(114)+e(101)+e(101) = 400 | 400 % 11 = 4 |
| Root | R(82)+o(111)+o(111)+t(116) = 420 | 420 % 11 = 2 |
| AVL | A(65)+V(86)+L(76) = 227 | 227 % 11 = 7 |
| Exam | E(69)+x(120)+a(97)+m(109) = 395 | 395 % 11 = 10 |
| Pass | P(80)+a(97)+s(115)+s(115) = 407 | 407 % 11 = 0 |
| Fail | F(70)+a(97)+i(105)+l(108) = 380 | 380 % 11 = 6 |
| DS | D(68)+S(83) = 151 | 151 % 11 = 8 |
| PQ | P(80)+Q(81) = 161 | 161 % 11 = 7 |
| Stack | S(83)+t(116)+a(97)+c(99)+k(107) = 502 | 502 % 11 = 7 |
| Pot | P(80)+o(111)+t(116) = 307 | 307 % 11 = 10 |
| Spot | S(83)+p(112)+o(111)+t(116) = 422 | 422 % 11 = 4 |
| Stop | S(83)+t(116)+o(111)+p(112) = 422 | 422 % 11 = 4 |
| Post | P(80)+o(111)+s(115)+t(116) = 422 | 422 % 11 = 4 |
| Queue | Q(81)+u(117)+e(101)+u(117)+e(101) = 517 | 517 % 11 = 0 |
Step 3: Insert into hash table with separate chaining
| Index | Linked List (Chain) |
|---|
| 0 | Pass -> Queue |
| 1 | (empty) |
| 2 | Root |
| 3 | (empty) |
| 4 | Tree -> Spot -> Stop -> Post |
| 5 | (empty) |
| 6 | Fail |
| 7 | AVL -> PQ -> Stack |
| 8 | DS |
| 9 | (empty) |
| 10 | Top -> Exam -> Pot |
Visual Representation (Hash Table 1):
Index | Chain
---------------------------
0 | Pass -> Queue
1 | -
2 | Root
3 | -
4 | Tree -> Spot -> Stop -> Post
5 | -
6 | Fail
7 | AVL -> PQ -> Stack
8 | DS
9 | -
10 | Top -> Exam -> Pot
Search Example (Hash Table 1)
Search for existing value: "Spot"
- Compute hash: sum ASCII = 422, 422 % 11 = 4
- Go to index 4: Chain = Tree -> Spot -> Stop -> Post
- Traverse chain:
- Compare "Tree" != "Spot"
- Compare "Spot" == "Spot" → Found!
Search for non-existing value: "Hash"
- Compute hash: H(72)+a(97)+s(115)+h(104) = 388, 388 % 11 = 3
- Go to index 3: Chain is empty
- Not found.
2.2 Hash Table 2
Configuration:
- Hash Function: Polynomial Hash Function
( h(s) = \left(\sum_{i=0}^{n-1} s[i] \times p^i \right) \mod m )
where ( p = 31 ) (a common choice), ( m = 17 ) (array size)
- Array Size: 17
- Collision Handling: Linear Probing
Step 1: Define the hash function
For string s of length n:
[
h(s) = \left(\sum_{i=0}^{n-1} \text{ASCII}(s[i]) \times 31^i \right) \mod 17
]
Step 2: Calculate hash values and insert with linear probing
We calculate ( h(s) ) for each string and insert at the computed index or next available slot.
Calculations (mod 17):
We calculate ( h(s) ) stepwise for each string:
-
Top:
( T(84)31^0 + o(111)31^1 + p(112)31^2 ) mod 17
= ( 84 + 11131 + 11231^2 ) mod 17
Calculate powers mod 17:
(31 \equiv 14 \mod 17)
(31^2 = 14^2 = 196 \equiv 9 \mod 17)
So:
(84 + 11114 + 112*9 \mod 17)
= (84 + 1554 + 1008 \mod 17)
= (84 + 1554 + 1008 = 2646 \mod 17)
(2646 \div 17 = 155) remainder (11)
So hash = 11
-
Tree:
( T(84)31^0 + r(114)31^1 + e(101)31^2 + e(101)31^3 ) mod 17
Powers:
(31^0=1), (31^1=14), (31^2=9), (31^3=149=126 \equiv 7 \mod 17)
Sum:
(841 + 11414 + 1019 + 101*7) mod 17
= (84 + 1596 + 909 + 707) mod 17
= (3296 \mod 17)
(3296 \div 17 = 193) remainder (5)
Hash = 5
-
Root:
( R(82)*1 + o(111)*14 + o(111)*9 + t(116)*7 ) mod 17
= (82 + 1554 + 999 + 812) mod 17
= (3447 \mod 17)
(3447 \div 17 = 202) remainder (11)
Hash = 11
-
AVL:
( A(65)*1 + V(86)*14 + L(76)*9 ) mod 17
= (65 + 1204 + 684) mod 17
= (1953 \mod 17)
(1953 \div 17 = 114) remainder (15)
Hash = 15
-
Exam:
( E(69)*1 + x(120)*14 + a(97)*9 + m(109)*7 ) mod 17
= (69 + 1680 + 873 + 763) mod 17
= (3385 \mod 17)
(3385 \div 17 = 199) remainder (2)
Hash = 2
-
Pass:
( P(80)*1 + a(97)*14 + s(115)*9 + s(115)*7 ) mod 17
= (80 + 1358 + 1035 + 805) mod 17
= (3278 \mod 17)
(3278 \div 17 = 192) remainder (14)
Hash = 14
-
Fail:
( F(70)*1 + a(97)*14 + i(105)*9 + l(108)*7 ) mod 17
= (70 + 1358 + 945 + 756) mod 17
= (3129 \mod 17)
(3129 \div 17 = 184) remainder (1)
Hash = 1
-
DS:
( D(68)*1 + S(83)*14 ) mod 17
= (68 + 1162) mod 17
= (1230 \mod 17)
(1230 \div 17 = 72) remainder (6)
Hash = 6
-
PQ:
( P(80)*1 + Q(81)*14 ) mod 17
= (80 + 1134) mod 17
= (1214 \mod 17)
(1214 \div 17 = 71) remainder (7)
Hash = 7
-
Stack:
( S(83)*1 + t(116)13) mod 17
= (83 + 1624 + 873 + 693 + 1391) mod 17
= (4664 \mod 17)
(4664 \div 17 = 274) remainder (6)
Hash = 6
Step 3: Insert with linear probing
| Index | Value | Notes (if collision) |
|---|
| 0 | Queue | Inserted at 0 |
| 1 | Fail | Inserted at 1 |
| 2 | Exam | Inserted at 2 |
| 3 | - | Empty |
| 4 | - | Empty |
| 5 | Tree | Inserted at 5 |
| 6 | DS | Inserted at 6 |
| 7 | PQ | Inserted at 7 |
| 8 | - | Empty |
| 9 | Pot | Inserted at 9 |
| 10 | - | Empty |
| 11 | Top | Inserted at 11 |
| 12 | Spot | Collision at 11 (Top), linear probe to 12 |
| 13 | Post | Inserted at 13 |
| 14 | Pass | Inserted at 14 |
| 15 | AVL | Inserted at 15 |
| 16 | - | Empty |
Note:
- Root (hash 11) collides with Top (11), linear probe to 12 (occupied by Spot), next 13 (Post), next 14 (Pass), next 15 (AVL), next 16 empty? Actually, Root's hash is 11, but Spot is 12, Post 13, Pass 14, AVL 15. We need to place Root.
Wait, Root hash is 11, but 11 is occupied by Top, 12 by Spot, 13 by Post, 14 by Pass, 15 by AVL, 16 empty.
So Root goes to 16.
-
Stack (hash 6) collides with DS (6), linear probe to 7 (PQ), 8 empty → Stack at 8.
-
Stop (hash 5) collides with Tree (5), linear probe to 6 (DS), 7 (PQ), 8 (Stack), 9 (Pot), 10 empty → Stop at 10.
Final Table (Hash Table 2):
| Index | Value |
|---|
| 0 | Queue |
| 1 | Fail |
| 2 | Exam |
| 3 | - |
| 4 | - |
| 5 | Tree |
| 6 | DS |
| 7 | PQ |
| 8 | Stack |
| 9 | Pot |
| 10 | Stop |
| 11 | Top |
| 12 | Spot |
| 13 | Post |
| 14 | Pass |
| 15 | AVL |
| 16 | Root |
Visual Representation (Hash Table 2):
Index | Value
----------------
0 | Queue
1 | Fail
2 | Exam
3 | -
4 | -
5 | Tree
6 | DS
7 | PQ
8 | Stack
9 | Pot
10 | Stop
11 | Top
12 | Spot
13 | Post
14 | Pass
15 | AVL
16 | Root
Search Example (Hash Table 2)
Search for existing value: "Stop"
- Compute hash: 5
- Check index 5: Tree != Stop
- Linear probe to 6: DS != Stop
- Probe 7: PQ != Stop
- Probe 8: Stack != Stop
- Probe 9: Pot != Stop
- Probe 10: Stop == Stop → Found!
Search for non-existing value: "Hash"
- Compute hash for "Hash":
( H(72)*1 + a(97)*14 + s(115)*9 + h(104)*7 ) mod 17
= (72 + 1358 + 1035 + 728) mod 17
= (3193 \mod 17)
(3193 \div 17 = 187) remainder (4)
- Check index 4: empty → Not found.
Summary
| Hash Table | Hash Function | Collision Handling | Array Size | Example Search (Found) | Example Search (Not Found) |
|---|
| 1 | Sum ASCII % 11 | Separate Chaining | 11 | "Spot" at index 4 chain | "Hash" at index 3 empty |
| 2 | Polynomial Hash (p=31) % 17 | Linear Probing | 17 | "Stop" found after probing | "Hash" at index 4 empty |
If you want, I can also provide diagrams or code implementations for these hash tables. Would you like that?