CSC211 ยท 2077 ยท 5 marks
Write short notes on: a. Tail recursion b. Collision resolution techniques
Write short notes on: a. Tail recursion b. Collision resolution techniques [5]
Quick answer
Example: Insert {89, 49, 18, 58} with h(x) = x % 10 | Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |-------|---|---|---|---|---|---|---|---|---|---| | Value | 49| 58| - | - | - | - | - | - | 18| 89|
Short Notes: Tail Recursion and Collision Resolution Techniques
a. Tail Recursion (2.5 marks)
Definition: Tail recursion is a special form of recursion in which the recursive call is the last operation performed in the function. There is no pending computation left to be done after the recursive call returns.
Key Characteristics:
- The recursive call appears at the tail (end) of the function body.
- Since no task is left after the recursive call, the current stack frame does not need to be preserved.
- As stated in the notes: "Tail recursion is better than non-tail recursion. As there is no task left after the recursive call, it will be easier for the compiler to organize and optimize the code."
Example:
// Non-tail recursive factorial
int fact(int n) {
if (n == 0) return 1;
return n * fact(n-1); // pending multiplication after call
}
// Tail recursive factorial
int fact_tail(int n, int acc) {
if (n == 0) return acc;
return fact_tail(n-1, n * acc); // last operation is the call itself
}
Advantages over Normal Recursion:
- Compiler can optimize tail recursion into iteration (tail call optimization), saving stack space.
- Avoids stack overflow for large inputs.
- More memory efficient since intermediate results need not be stored on the system stack.
b. Collision Resolution Techniques (2.5 marks)
Collision: A collision occurs when two distinct keys produce the same hash value and map to the same slot in a hash table.
Collision Resolution: When two items hash to the same slot, a systematic method is needed to place the second item. This process is called collision resolution.
The major techniques are:
1. Open Addressing
When a collision occurs, another location in the array is sought. It has three main methods:
i) Linear Probing:
- The colliding item is placed in the next empty slot in the array.
- Formula:
h(x) = (hash value + i) % table sizefor i = 1, 2, 3, ... - Disadvantage: Causes primary clustering (long chains of occupied slots).
ii) Quadratic Probing:
- Eliminates primary clustering by using a quadratic function.
- Formula:
h(x) = (hash value + jยฒ) % table sizefor j = 1, 2, 3, ... - On 1st collision:
(hash value + 1ยฒ) % table size - On 2nd collision:
(hash value + 2ยฒ) % table size
iii) Double Hashing:
- Uses a second hash function to determine the step size when a collision occurs.
- Reduces clustering more effectively than linear or quadratic probing.
2. Chaining
- Each slot in the hash table holds a linked list of all elements that hash to that slot.
- Colliding elements are simply appended to the list at that index.
- Easy to implement and handles multiple collisions gracefully.
3. Rehashing
- When a collision occurs, a new hash function is applied to find an alternate location.
4. Hashing Using Buckets
- Each hash table slot points to a bucket (array or block) that can hold multiple values.
Summary Table:
| Technique | Approach | Clustering |
|---|---|---|
| Linear Probing | Next empty slot | Primary clustering |
| Quadratic Probing | Quadratic step | Reduced clustering |
| Double Hashing | Second hash function | Minimal clustering |
| Chaining | Linked list at slot | No clustering |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.