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

As a consequence, the new root page contains a single item only. The details can be gathered from the<br />

program presented below, <strong>and</strong> Fig. 4.45 shows the result of using the program to construct a B-tree with<br />

the following insertion sequence of keys:<br />

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

The semicolons designate the positions of the snapshots taken upon each page allocation. Insertion of the<br />

last key causes two splits <strong>and</strong> the allocation of three new pages.<br />

a)<br />

20<br />

b)<br />

20<br />

10 15 30 40<br />

c)<br />

20 30<br />

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

d)<br />

10 20 30<br />

5 7 15 18 22 26 35 40<br />

e)<br />

10 20 30 40<br />

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

f)<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 />

Fig. 4.45. Growth of B-tree of order 2<br />

Since each activation of search implies one page transfer to main store, k = log n (N) recursive calls are<br />

necessary at most, if the tree contains N items. Hence, we must be capable of accommodating k pages in<br />

main store. This is one limiting factor on the page size 2n. In fact, we need to accommodate even more<br />

than k pages, because insertion may cause page splitting to occur. A corollary is that the root page is best<br />

allocated permanently in the primary store, because each query proceeds necessarily through the root<br />

page.<br />

Another positive quality of the B-tree organization is its suitability <strong>and</strong> economy in the case of purely<br />

sequential updating of the entire data base. Every page is fetched into primary store exactly once.<br />

Deletion of items from a B-tree is fairly straight-forward in principle, but it is complicated in the details.<br />

We may distinguish two different circumstances:<br />

1. The item to be deleted is on a leaf page; here its removal algorithm is plain <strong>and</strong> simple.<br />

2. The item is not on a leaf page; it must be replaced by one of the two lexicographically adjacent<br />

items, which happen to be on leaf pages <strong>and</strong> can easily be deleted.<br />

In case 2 finding the adjacent key is analogous to finding the one used in binary tree deletion. We descend<br />

along the rightmost pointers down to the leaf page P, replace the item to be deleted by the rightmost item<br />

on P, <strong>and</strong> then reduce the size of P by 1. In any case, reduction of size must be followed by a check of the

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

Saved successfully!

Ooh no, something went wrong!