Solution9

advertisement
Chapter 9
1. a. false; b. true; c. false; d. false
2. a. 8; b. 6; c. 1; d. 8
3.
a.
Iteration
1
2
3
4
5
firs
t
0
0
0
1
2
last mid
10
4
1
1
1
5
2
0
1
list[mi
d]
55
17
2
10
Number of comparisons
2
2
2
2
the loop stops unsuccessful search
This is an unsuccessful search. The total number of comparisons is 8.
b.
Iteration
1
2
3
4
firs
t
0
0
3
4
last mid
10
4
4
4
5
2
3
4
list[mi
d]
55
17
45
49
Number of comparisons
2
2
2
1(found is true)
The item is found at location 4 and the total number of comparisons is 7.
c.
Iteration
1
2
3
firs
t
0
6
9
last mid
10
10
10
5
8
9
list[mi
d]
55
92
98
Number of comparisons
2
2
1(found is true)
The item is found at location 9 and the total number of comparisons is 5.
d.
Iteration
1
2
3
4
5
firs
t
0
6
9
10
11
last mid
10
10
10
10
10
5
8
9
10
list[mi
d]
55
92
98
110
Number of comparisons
2
2
2
2
the loop stops
This is an unsuccessful search. The total number of comparisons is 8.
4.
template<class elemType>
class orderedArrayListType: public arrayListType<elemType>
{
public:
int binarySearch(const elemType& item);
orderedArrayListType(int n = 100);
};
5.
There are 30 buckets in the hash table and each bucket can hold 5 items.
6.
Suppose that an item with key X is to be inserted in HT. We use the hash function to compute the index
h(X) of this item in HT. Suppose that h(X) = t. Then 0  h(X)  HTSize – 1. If HT[t] is empty, we store
this item into this array slot. Suppose that HT[t] is already occupied by another item and so we have a
collision. In linear probing, starting at location t, we search the array sequentially to find the next
available array slot. In linear probing, we assume that the array is circular so that if the lower portion
of the array is full, we can continue the search in the top portion of the array.
7.
Suppose that an item with key X is hashed at t, that is, h(X) = t, and 0  t  HTSize - 1. Further
suppose that position t is already occupied. In quadratic probing, starting at position t, we linearly
search the array at locations (t + 1) % HTSize, (t + 22 ) % HTSize = (t + 4) % HTSize, (t+32) % HTSize
= (t+9) % HTSize, ..., (t + i2) % HTSize. That is, the probe sequence is: t, (t + 1) % HTSize, (t + 22 ) %
HTSize, (t+32) % HTSize, ..., (t + i2) % HTSize.
8. In double hashing, if a collision occurs at h(X), the probe sequence is generated by using the rule:
(h(X) + i * h (X)) % HTSize
where h is the second hash function.
9.
30, 31, 34, 39, 46, and 55
10. a. The item with index 15 is inserted at HT[15]; the item with index 101 is inserted at HT[0]; the
item with index 116 is inserted at HT[16]; the item with index 0 is inserted at HT[1]; and the item with
index 217 is inserted at HT[17].
b. The item with index 15 is inserted at HT[15]; the item with index 101 is inserted at HT[0]; the
item with index 116 is inserted at HT[16]; the item with index 0 is inserted at HT[1]; and the item
with index 217 is inserted at HT[19].
11. 101
12. Suppose that an item, say R, is to be deleted from the hash table, HT. We first must find the index of R
in HT. To find the index of R, we apply the same criteria that was applied to R when R was inserted in
HT. Let us further assume that after inserting R another item, R, was inserted in HT and the home
position of R and R is the same. The probe sequence of R is contained in the probe sequence of R
because R was inserted in the hash table after R. Suppose that we simply delete R by marking the array
slot containing R as empty. If this array position stays empty, then while searching for R and following
its probe sequence, the search terminates at this empty array position. This gives the impression that R
is not in the table, which, of course, is incorrect. The item R cannot simply be deleted by marking its
position as empty from the hash table.
13. In open hashing, the hash table, HT, is an array of pointers. (For each j, 0  j  HTSize – 1, HT[j] is a
pointer to a linked list.) Therefore, items are inserted into and deleted from a linked list, and so item
insertion and deletion are simple and straightforward. If the hash function is efficient, few keys are
hashed to the same home position. Thus, an average linked list is short, which results in a shorter
search length.
14. Suppose there are 1000 items, each requiring 1 word of storage. Chaining then requires a total of 3000
words of storage. On the other hand, with quadratic probing, if the hash table size is twice the number
of items, only 2000 words are required for the hash table. Also, if the table size is three times the
number of items, then in quadratic probing the keys are reasonably spread out. This results in fewer
collisions and so the search is fast.
15. Suppose there are 1000 items and each item requires 10 words of storage. Further suppose that each
pointer requires 1 word of storage. We then need 1000 words for the hash table, 10000 words for the
items, and 1000 words for the link in each node. A total of 12000 words of storage space, therefore, is
required to implement the chaining. On the other hand, if we use quadratic probing, if the hash table
size is twice the number of items, we need 20000 words of storage.
16. The load factor α = 850 / 1001 ≈ .85.
17. The load factor α = 500 / 1001 ≈ .5.
a. (1/2){1 + (1/(1− α))} ≈ 1.5.
b. −log2 (1− α) / α ≈ 2 .
c. (1 + α /2) = 1.25.
18. Suppose that the size of the hash table is x. Then α = 550 / x.
a. (1/2){1 + (1/(1− α))} = 3. Substituting for α and then solving for x, gives x ≈ 690.
b. x ≈ 140
Download