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

back along the search path. The general structure of the program will therefore be similar to balanced tree<br />

insertion, although the details are different. First of all, a definition of the page structure has to be<br />

formulated. We choose to represent the items in the form of an array.<br />

TYPE Page =<br />

POINTER TO PageDescriptor;<br />

Item =<br />

RECORD key: INTEGER;<br />

p: Page;<br />

count: INTEGER (*data*)<br />

END;<br />

PageDescriptor = RECORD m: INTEGER; (* 0 .. 2n *)<br />

p0: Page;<br />

e: ARRAY 2*n OF Item<br />

END<br />

Again, the item component count st<strong>and</strong>s for all kinds of other information that may be associated with<br />

each item, but it plays no role in the actual search process. Note that each page offers space for 2n items.<br />

The field m indicates the actual number of items on the page. As m ≥ n (except for the root page), a<br />

storage utilization of a least 50% is guaranteed.<br />

The algorithm of B-tree search <strong>and</strong> insertion is formulated below as a procedure called search. Its main<br />

structure is straightforward <strong>and</strong> similar to that for the balanced binary tree search, with the exception that<br />

the branching decision is not a binary choice. Instead, the "within-page search" is represented as a binary<br />

search on the array e of elements.<br />

The insertion algorithm is formulated as a separate procedure merely for clarity. It is activated after<br />

search has indicated that an item is to be passed up on the tree (in the direction toward the root). This fact<br />

is indicated by the Boolean result parameter h; it assumes a similar role as in the algorithm for balanced tree<br />

insertion, where h indicates that the subtree had grown. If h is true, the second result parameter, u,<br />

represents the item being passed up. Note that insertions start in hypothetical pages, namely, the "special<br />

nodes" of Fig. 4.19; the new item is immediately h<strong>and</strong>ed up via the parameter u to the leaf page for actual<br />

insertion. The scheme is sketched here:<br />

PROCEDURE search (x: INTEGER; a: Page; VAR h: BOOLEAN; VAR u: Item);<br />

BEGIN<br />

IF a = NIL THEN (*x not in tree, insert*)<br />

assign x to item u, set h to TRUE, indicating that an item<br />

u is passed up in the tree<br />

ELSE<br />

binary search for x in array a.e;<br />

IF found THEN process data<br />

ELSE<br />

search(x, descendant, h, u);<br />

IF h THEN (*an item was passed up*)<br />

IF no. of items on page a^ < 2n THEN<br />

insert u on page a^ <strong>and</strong> set h to FALSE<br />

ELSE split page <strong>and</strong> pass middle item up<br />

END<br />

END<br />

END<br />

END<br />

END search<br />

If the parameter h is true after the call of search in the main program, a split of the root page is requested.<br />

Since the root page plays an exceptional role, this process has to be programmed separately. It consists<br />

merely of the allocation of a new root page <strong>and</strong> the insertion of the single item given by the paramerter u.

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

Saved successfully!

Ooh no, something went wrong!