Slot

advertisement
CS 2604
Homework 4: Hashing
Spring 2002
Name:
Answer each question in the space provided. Fill in your answers in the spaces provided in this file, and submit your
solution to the Curator as an MS Word document.
1.
Consider a hash table consisting of M = 11 slots, and suppose integer key values are hashed into the table
using the hash function h1:
int h1 (int key) {
x = (key + 5) * (key + 5);
x = x/16;
x = x + key;
x = x % 11;
return x;
}
Suppose that collisions are resolved using linear probing. The probe sequence is given therefore by
h1(K) + i (mod 11)
The integer key values listed below are to be inserted, in the order given. Show the home slot (the slot to which
the key hashes, before any probing), the probe sequence (if any) for each key, and the final contents of the hash
table after the following key values have been inserted in the given order:
Key
Value
Home
Slot
Probe Sequence
43
23
1
0
15
31
4
7
11
3
Final Hash Table:
Slot
0
1
2
3
4
5
6
7
8
9
10
Contents
Due: Friday, April 19, 2002 at 11:59:59PM
Page 1 of 5
CS 2604
2.
Homework 4: Hashing
Spring 2002
Repeat problem 1, but use quadratic probing instead of linear probing to resolve collisions. . The probe
sequence is given therefore by
h1(K) + i2 (mod 11)
The integer key values listed below are to be inserted, in the order given. Show the home slot (the slot to which
the key hashes, before any probing), the probe sequence (if any) for each key, and the final contents of the hash
table after the following key values have been inserted in the given order:
Key
Value
Home
Slot
Probe Sequence
43
23
1
0
15
31
4
7
11
3
Final Hash Table:
Slot
0
1
2
3
4
5
6
7
8
9
10
Contents
Due: Friday, April 19, 2002 at 11:59:59PM
Page 2 of 5
CS 2604
3.
(a)
Homework 4: Hashing
Spring 2002
Assume that you begin with the hash table attained at the end of problem 2. Show the contents of the hash
table after each indicated operation is performed. Be sure to clearly indicate which slots are empty (leave
these blank) and which are tombstones (use Tomb for each tombstone). Assume the same hash function
and probe strategy as for problem 2. Recycle tombstones as described in the textbook. The parts are
cumulative.
Delete the key value: 15
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(a)
Delete the key value: 1
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(a)
Delete the key value: 43
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(b)
Delete the key value: 11
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(c)
Insert the key value: 22
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(d)
Delete the key value: 31
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(e)
Insert the key value: 99
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(Continued)
Due: Friday, April 19, 2002 at 11:59:59PM
Page 3 of 5
CS 2604
(f)
Homework 4: Hashing
Spring 2002
Insert the key value: 145
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(g)
Insert the key value: 83
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(h)
Delete the key value: 23
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(i)
Delete the key value: 4
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
(j)
Insert the key value: 49
Slot
0
1
Number of slots that were probed:
2
3
4
5
6
7
8
9
10
Contents
For each of questions 4 through 8, start with the following AVL tree:
4
3
1
5
4
8
9
For questions 4 through 8, draw the final AVL tree that would result from performing the indicated
actions on the AVL tree given above (i.e., always start with the given tree, the questions are not
accumulative).
4) Insert the key 10.
Due: Friday, April 19, 2002 at 11:59:59PM
Page 4 of 5
CS 2604
Homework 4: Hashing
Spring 2002
5) Insert the key 95.
6) Insert the key 80, and then insert the key 77.
7) Delete the key 8.
8) Delete the key 5.
For each of questions 7 through 9, start with the following BST tree:
E
C
B
W
A
Y
V
D
F
X
Z
For questions 9 through 11, draw the final BST tree that would result from performing the
indicated actions on the BST tree given above (i.e., always start with the given tree, the questions
are not accumulative).
9) Insert the key W.
10) Delete the key V and then delete the key F.
11) Delete the key E.
12) Draw the Heap that results from inserting the following values in the order given:
53 72 91 42 17 89 3 88
Due: Friday, April 19, 2002 at 11:59:59PM
Page 5 of 5
Download