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 183<br />

number of items m on the reduced page, because, if m < n, the primary characteristic of B-trees would be<br />

violated. Some additional action has to be taken; this underflow condition is indicated by the Boolean<br />

variable parameter h.<br />

The only recourse is to borrow or annect an item from one of the neighboring pages, say from Q. Since<br />

this involves fetching page Q into main store — a relatively costly operation — one is tempted to make the<br />

best of this undesirable situation <strong>and</strong> to annect more than a single item at once. The usual strategy is to<br />

distribute the items on pages P <strong>and</strong> Q evenly on both pages. This is called page balancing.<br />

Of course, it may happen that there is no item left to be annected since Q has already reached its minimal<br />

size n. In this case the total number of items on pages P <strong>and</strong> Q is 2n-1; we may merge the two pages into<br />

one, adding the middle item from the ancestor page of P <strong>and</strong> Q, <strong>and</strong> then entirely dispose of page Q. This is<br />

exactly the inverse process of page splitting. The process may be visualized by considering the deletion of<br />

key 22 in Fig. 4.45. Once again, the removal of the middle key in the ancestor page may cause its size to<br />

drop below the permissible limit n, thereby requiring that further special action (either balancing or merging)<br />

be undertaken at the next level. In the extreme case page merging may propagate all the way up to the<br />

root. If the root is reduced to size 0, it is itself deleted, thereby causing a reduction in the height of the B-<br />

tree. This is, in fact, the only way that a B-tree may shrink in height. Figure 4.46 shows the gradual decay<br />

of the B-tree of Fig. 4.45 upon the sequential deletion of the keys<br />

25 45 24; 38 32; 8 27 46 13 42; 5 22 18 26; 7 35 15;<br />

The semicolons again mark the places where the snapshots are taken, namely where pages are being<br />

eliminated. The similarity of its structure to that of balanced tree deletion is particularly noteworthy.<br />

a)<br />

25<br />

10 20<br />

30 40<br />

5 7 8 13 15 18 22 24 26 27 32 35 38 42 45 46<br />

b)<br />

10 22 30 40<br />

5 7 8 13 15 18 20 26 27 32 35 38 42 46<br />

c)<br />

10 22 30<br />

5 7 8 13 15 18 20 26 27 35 40 42 46<br />

d)<br />

10 22<br />

5 7 15 18 20 26 30 35 40<br />

e)<br />

15<br />

7 10 20 30 35 40<br />

f)<br />

10 20 30 40<br />

Fig. 4.46. Decay of B-tree of order 2<br />

TYPE Page = POINTER TO PageRec; (* ADenS471_Btrees *)<br />

Entry = RECORD

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

Saved successfully!

Ooh no, something went wrong!