Indexing and Hashing: Solutions To Practice Exercises
Indexing and Hashing: Solutions To Practice Exercises
53
54 Chapter 12 Indexing and Hashing
12.3 The following were generated by inserting values into the B+ -tree in ascending
order. A node (other than the root) was never allowed to have fewer than dn=2e
values/pointers.
a.
19
5 11 29
2 3 5 7 11 17 19 23 29 31
b.
7 19
2 3 5 7 11 17 19 23 29 31
c.
11
2 3 5 7 11 17 19 23 29 31
19
5 11 29
2 3 5 7 9 11 17 19 23 29 31
Exercises 55
Insert 10:
19
5 10 29
2 3 5 7 9 10 11 17 19 23 29 31
Insert 8:
19
5 8 10 29
2 3 5 7 8 9 10 11 17 19 23 29 31
Delete 23:
19
5 8 10 19
2 3 5 7 8 9 10 11 17 19 29 31
56 Chapter 12 Indexing and Hashing
Delete 19:
19
5 8 10 29
2 3 5 7 8 9 10 11 17 29 31
7 19
2 3 5 7 9 11 17 19 23 29 31
Insert 10:
7 19
2 3 5 7 9 10 11 17 19 23 29 31
Exercises 57
Insert 8:
7 10 19
2 3 5 7 8 9 10 11 17 9 23 29 31
Delete 23:
7 10 19
2 3 5 7 8 9 10 11 17 19 29 31
Delete 19:
7 10
2 3 5 7 8 9 10 11 17 29 31
2 3 5 7 9 11 17 19 23 29 31
Insert 10:
11
2 3 5 7 9 10 11 17 19 23 29 31
Insert 8:
11
2 3 5 7 8 9 10 11 17 19 23 29 31
Delete 23:
11
2 3 5 7 8 9 10 11 17 19 29 31
58 Chapter 12 Indexing and Hashing
Delete 19:
11
2 3 5 7 8 9 10 11 17 29 31
12.5 If there are K search-key values and m 1 siblings are involved in the redistri-
bution, the expected height of the tree is: logb(m 1)n =m c(K )
a.
5 17 29
2 3 7 11 19 23 31
b.
7 23
2 3 5 11 17 19 29 31
c.
11
2 3 5 7 17 19 23 29 31
60 Chapter 12 Indexing and Hashing
2
17
3 3
000 2
001
010 3
011 3
11
100 19
101 2
110 5
29
111
2
7
23
31
12.8 a. Delete 11: From the answer to Exercise 12.7, change the third bucket to:
3
3
19
At this stage, it is possible to coalesce the second and third buckets. Then it
is enough if the bucket address table has just four entries instead of eight.
For the purpose of this answer, we do not do the coalescing.
b. Delete 31: From the answer to 12.7, change the last bucket to:
2
7
23
c. Insert 1: From the answer to 12.7, change the first bucket to:
2
1
17
d. Insert 15: From the answer to 12.7, change the last bucket to:
Exercises 61
2
7
15
23
12.9 Let idenote the number of bits of the hash value used in the hash table. Let
bsize denote the maximum capacity of each bucket.
delete(value K l)
begin
j = first ihigh-order bits of h(K l);
delete value K l from bucket j;
coalesce(bucket j);
end
coalesce(bucket j)
begin
ij = bits used in bucket j;
k = any bucket with first (ij 1) bits same as that
of bucket j while the bit ij is reversed;
ik = bits used in bucket k;
= ik )
if(ij 6
return; /* buckets cannot be merged */
if(entries in j + entries in k > bsize)
return; /* buckets cannot be merged */
move entries of bucket k into bucket j;
coalesce(bucket j);
end
Note that we can only merge two buckets at a time. The common hash prefix
of the resultant bucket will have length one less than the two buckets merged.
Hence we look at the buddy bucket of bucket jdiffering from it only at the last
bit. If the common hash prefix of this bucket is not ij, then this implies that the
buddy bucket has been further split and merge is not possible.
When merge is successful, further merging may be possible, which is han-
dled by a recursive call to coalesce at the end of the function.
12.10 If the hash table is currently using i bits of the hash value, then maintain a
count of buckets for which the length of common hash prefix is exactly i.
Consider a bucket j with length of common hash prefix ij. If the bucket is
being split, and ij is equal to i, then reset the count to 1. If the bucket is being
62 Chapter 12 Indexing and Hashing
split and ij is one less that i, then increase the count by 1. It the bucket if being
coalesced, and ij is equal to ithen decrease the count by 1. If the count becomes
0, then the bucket address table can be reduced in size at that point.
However, note that if the bucket address table is not reduced at that point,
then the count has no significance afterwards. If we want to postpone the re-
duction, we have to keep an array of counts, i.e. a count for each value of com-
mon hash prefix. The array has to be updated in a similar fashion. The bucket
address table can be reduced if the ith entry of the array is 0, where iis the
number of bits the table is using. Since bucket table reduction is an expensive
operation, it is not always advisable to reduce the table. It should be reduced
only when sufficient number of entries at the end of count array become 0.
12.11 We reproduce the account relation of Figure 12.25 below.
Brighton 1 0 0 0 0 0 0 0 0
Downtown 0 1 1 0 0 0 0 0 0
Mianus 0 0 0 1 0 0 0 0 0
Perryridge 0 0 0 0 1 1 1 0 0
Redwood 0 0 0 0 0 0 0 1 0
Round hill 0 0 0 0 0 0 0 0 1
L1 0 0 0 0 0 0 0 0 0
L2 0 0 0 0 1 0 0 0 1
L3 0 1 1 1 0 0 1 1 0
L4 1 0 0 0 0 1 0 0 0
where, level L 1 is below 250, level L 2 is from 250 to below 500, L 3 from 500 to
below 750 and level L 4 is above 750.
Exercises 63
To find all accounts in Downtown with a balance of 500 or more, we find the
union of bitmaps for levels L 3 and L 4 and then intersect it with the bitmap for
Downtown.
Downtown 0 1 1 0 0 0 0 0 0
L3 0 1 1 1 0 0 1 1 0
L4 1 0 0 0 0 1 0 0 0
L3 [ L4 1 1 1 1 0 1 1 1 0
Downtown 0 1 1 0 0 0 0 0 0
Downtown \(L 3 [ L 4 ) 0 1 1 0 0 0 0 0 0