Overview
On the homework, when we remove, what do we do?
The same thing you do with remove in normal linked lists, except at greater scale: Every previous pointer skips over the node.
What code should I use for that?
Something like
prev.next.set(level, prev.next.get(level).next.get(level));
What was your favorite part of this morning’s CSC 151?
(define deep-reverse
(lambda (val)
(if (list? val)
(reverse (map deep-reverse lst))
val)))
Suppose I didn’t do the reading for today and that I’d never seen hash tables before. What would you tell me to give me a high-level understanding of hash tables? [note: I did the reading and I’ve seen hash tabales.]
Consider the following hash function for strings:
For example:
What do you think?
Run the algorithm on your name (real, nickname). Make sure you know the number.
A:1, B:2, C:3, D:4, E:5, F:6, G:7, H:8, I:9, J:10, K:11, L:12, M:13,
N:14, O:15, P:16, Q:17, R:18, S:19, T:20, U:21, V:22, W:23, X:24, Y:25, Z:26
0: 10: H5 20: A4
1: 11: K5 21:
2: E5 12: R8 22:
3: 13: S7 23: M3
4: P7 14: 24:
5: C6 15: A7 25:
6: K5 16: 26: R3
7: Z8 17: 27:
8: G5 18: 28:
9: M7 19: 29:
ODO - hashes to 15 + 4 + 15 = 34, 34 mod 30 = 4. It took a bit to find out that ODO is not in our class.
Would it be any better with bucketing? Probably, because we end up chaining the collisions. But bucketing adds overhead and complexity.
Note: Probing is generally better if you add 7 (or any number other than 1).
Approaches to maps.
Costs for set
.
set
is
expected O(1).Additional costs
Detour on expansion
add
n times.
Suppose also that the initial size of the array is 1 and the policy
of expanding is “double the size and copy”.set
is amortized expected O(1).How would you write a better hash function for strings? (Hint: What does PM suggest for an arbitrary object, which I think he took from J. Bloch?)
PM suggests:
Why?
For strings.
(define hash
(let ([start (random 1000)])
(lambda (str)
(let kernel ([code start]
[remaining (string->list str)])
(if (null? remaining)
code
(kernel (+ (* code 31) (char->integer (car remaining)))
(cdr remaining)))))))