Solutions to assignment 3

advertisement
Solutions to assignment 3
11.12 h(x) = x mod 8 is the remainder of x when divided by 8. To represent all the 8
possible remainders, three bits are needed.
Initially, there is one bucket and the directory has one entry pointing to the bucket. Since there is
only one bucket, the hash value is not used. That is, both the global depth and local depth are 0
initially. After inserting records with search-key values 2, 3 and 5, the extendible hashing
structure looks like:
Global depth
0
0
local depth
2,
3,
5
Directory
To insert the next record with search-key 7, need to create a new bucket (as the bucket is full).
However, there is only one pointer (or directory entry). There will be no pointer for the new
bucket. Hence, we first double the size of the directory and increase the global depth by 1.
Global depth
1
0
0
local depth
2,
3,
5
1
Directory
Next, we make a new bucket and set the pointer in the second directory entry to point to the new
bucket. We split the records in the hash structure and the new record between the old bucket and
the new bucket. If the first bit of the hash value is 0, the record is put in the old bucket; and if
the first bit is 1, the record is put in the new bucket. Since now the first bit is used to distinguish
the records in these two buckets, the local depths of both buckets are 1.
Global depth
1
0
1
local depth
2,
3
1
Directory
0
1
local depth
5,
7
1
Next, the record with search-key value 11 is inserted and the hash structure becomes
Global depth
1
0
1
local depth
2,
3,
1
Directory
1
11
1
local depth
5,
7
0
1
The record to be inserted next has search-key value 17, which has hash value 001. That is, it
should be inserted into the first bucket, which is now full. Hence, we need to create a new
bucket. As there is no more pointer for this new bucket, we need to increase the global depth by
1 and double the size of the directory.
Global depth
1
local depth
2,
3,
2
00
01
1
local depth
5,
7
10
11
0
11
1
Directory
Note that for a record to be put into the first bucket, we require the first bit of its hash value to be
0. Since the first bit of the first two index values (00, 01) are both 0, the first two directory
entries both point to the first bucket. Similarly, the 3rd and 4th directory entries both point to the
second bucket. Now, we create a new bucket and split the records in the first bucket and the new
record between the new bucket and the first bucket. Since the first bits of the hash values of
these 4 records are the same, we need to look at the second bit to do the separation. In other
words, for these records, we use the first 2 bits of their hash values. Thus, the local depths of
these buckets are both 2.
2
local depth
00
17
Global depth
2
w
2
2,
00
01
local depth
3,
11
1
local depth
10
11
5,
7
01
1
The next record to be inserted has search-key value 19, which has hash value 3. That is, this
record should be inserted into the second bucket. Now, this bucket is full and there is only one
pointer pointing to it. Thus, we first need to increase the size of the directory before we make a
new bucket.
2
Global depth
3
2
000
local depth
00
17
001
2
w
010
011
2,
local depth
3,
11
01
100
101
1
local depth
110
5,
7
1
111
Note that for a record to be put into the third bucket, we only require that the first bit of its hash
value to be 1. As the first bits of the index values 100, 101, 110 and 111 are 1, the
directory entries indexed by these values all point to the third bucket. For a similar reason, the
first two directory entries point to the first bucket, and the 3rd and 4th directory entries point to the
second bucket.
Now, make a new bucket and split the records in the second bucket and the new record between
the new bucket and the second bucket. The resulting hash structure becomes:
2
Global depth
local depth
00
17
3
000
001
3
w
local depth
3
local depth
3,
11,
1
local depth
5,
7
010
2
010
011
100
101
110
111
19
011
1
The next record to be inserted has search-key 23 with hash value 7 (or 111). It is placed in
the 4th bucket.
3
local depth
2
Global depth
00
17
3
000
001
3
w
local depth
3
local depth
3,
11,
1
local depth
5,
7,
010
2
010
011
100
101
110
111
19
23
011
1
The next record has search key 29 which has hash value 5. It should be placed in the 4th bucket.
However, this bucket is full. A new bucket is introduced.
2
Global depth
local depth
00
17
3
000
001
3
w
local depth
3
local depth
3,
11,
2
local depth
5,
29
2
local depth
7,
23
010
2
010
011
100
101
110
111
The final record with search key 31 is inserted into the 5th bucket.
4
19
011
10
11
The final extendible hash structure is
Global depth
2
local depth
00
17
3
000
001
3
w
local depth
3
local depth
3,
11,
2
local depth
5,
29
2
local depth
7,
23,
010
2
010
011
100
101
110
111
19
011
10
31
11
11.14 A range query cannot be answered efficiently using a hash structure. This is because
records with search-key values in the range do not necessarily occupy the same bucket or
adjacent buckets. This is a result of the uniform and random properties of a hash function. Thus,
for each search-key value in the specified range, we need to search the hash structure.
5
Download