25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 180<br />

increasing order from left to right if the B-tree is squeezed into a single level by inserting the descendants in<br />

between the keys of their ancestor page. This arrangement represents a natural extension of binary search<br />

trees, <strong>and</strong> it determines the method of searching an item with given key. Consider a page of the form<br />

shown in Fig. 4.43 <strong>and</strong> a given search argument x. Assuming that the page has been moved into the<br />

primary store, we may use conventional search methods among the keys k 0 ... k m-1 . If m is sufficiently<br />

large, one may use binary search; if it is rather small, an ordinary sequential search will do. (Note that the<br />

time required for a search in main store is probably negligible compared to the time it takes to move the<br />

page from secondary into primary store.) If the search is unsuccessful, we are in one of the following<br />

situations:<br />

1. k i < x < k i+1 for 0 < i < m-1 The search continues on page p i^<br />

2. k m-1 < x The search continues on page p m-1^.<br />

3. x < k 0 The search continues on page p -1^.<br />

p -1 k 0 p 0 k 1 p 1 . . . p m-2 k m-1 p m-1<br />

Fig. 4.43. B-tree page with m keys<br />

If in some case the designated pointer is NIL, i.e., if there is no descendant page, then there is no item with<br />

key x in the whole tree, <strong>and</strong> the search is terminated.<br />

Surprisingly, insertion in a B-tree is comparatively simple too. If an item is to be inserted in a page with<br />

m < 2n items, the insertion process remains constrained to that page. It is only insertion into an already full<br />

page that has consequences upon the tree structure <strong>and</strong> may cause the allocation of new pages. To<br />

underst<strong>and</strong> what happens in this case, refer to Fig. 4.44, which illustrates the insertion of key 22 in a B-tree<br />

of order 2. It proceeds in the following steps:<br />

1. Key 22 is found to be missing; insertion in page C is impossible because C is already full.<br />

2. Page C is split into two pages (i.e., a new page D is allocated).<br />

3. The 2n+1 keys are equally distributed onto C <strong>and</strong> D, <strong>and</strong> the middle key is moved up one level into<br />

the ancestor page A.<br />

A<br />

20<br />

A<br />

20 30<br />

7 10 15 18 26 30 35 40<br />

B<br />

C<br />

7 10 15 18 22 26<br />

35 40<br />

B<br />

C<br />

D<br />

Fig. 4.44. Insertion of key 22 in B-tree<br />

This very elegant scheme preserves all the characteristic properties of B-trees. In particular, the split<br />

pages contain exactly n items. Of course, the insertion of an item in the ancestor page may again cause that<br />

page to overflow, thereby causing the splitting to propagate. In the extreme case it may propagate up to the<br />

root. This is, in fact, the only way that the B-tree may increase its height. The B-tree has thus a strange<br />

manner of growing: it grows from its leaves upward to the root.<br />

We shall now develop a detailed program from these sketchy descriptions. It is already apparent that a<br />

recursive formulation will be most convenient because of the property of the splitting process to propagate

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!