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

END<br />

END delete;<br />

PROCEDURE ShowTree (VAR W: Texts.Writer; p: Page; level: INTEGER);<br />

VAR i: INTEGER;<br />

BEGIN<br />

IF p # NIL THEN<br />

FOR i := 1 TO level DO Texts.Write(W, 9X) END;<br />

FOR i := 0 TO p.m-1 DO Texts.WriteInt(W, p.e[i].key, 4) END;<br />

Texts.WriteLn(W);<br />

IF p.m > 0 THEN ShowTree(W, p.p0, level+1) END;<br />

FOR i := 0 TO p.m-1 DO ShowTree(W, p.e[i].p, level+1) END<br />

END<br />

END ShowTree;<br />

Extensive analysis of B-tree performance has been undertaken <strong>and</strong> is reported in the referenced article<br />

(Bayer <strong>and</strong> McCreight). In particular, it includes a treatment of the question of optimal page size, which<br />

strongly depends on the characteristics of the storage <strong>and</strong> computing system available.<br />

Variations of the B-tree scheme are discussed in Knuth, Vol. 3, pp. 476-479. The one notable<br />

observation is that page splitting should be delayed in the same way that page merging is delayed, by first<br />

attempting to balance neighboring pages. Apart from this, the suggested improvements seem to yield<br />

marginal gains. A comprehensive survey of B-trees may be found in [4-8].<br />

4.7.2 Binary B-Trees<br />

The species of B-trees that seems to be least interesting is the first order B-tree (n = 1). But sometimes<br />

it is worthwhile to pay attention to the exceptional case. It is plain, however, that first-order B-trees are not<br />

useful in representing large, ordered, indexed data sets involving secondary stores; approximately 50% of<br />

all pages will contain a single item only. Therefore, we shall forget secondary stores <strong>and</strong> again consider the<br />

problem of search trees involving a one-level store only.<br />

A binary B-tree (BB-tree) consists of nodes (pages) with either one or two items. Hence, a page<br />

contains either two or three pointers to descendants; this suggested the term 2-3 tree. According to the<br />

definition of B-trees, all leaf pages appear at the same level, <strong>and</strong> all non-leaf pages of BB-trees have either<br />

two or three descendants (including the root). Since we now are dealing with primary store only, an<br />

optimal economy of storage space is m<strong>and</strong>atory, <strong>and</strong> the representation of the items inside a node in the<br />

form of an array appears unsuitable. An alternative is the dynamic, linked allocation; that is, inside each<br />

node there exists a linked list of items of length 1 or 2. Since each node has at most three descendants <strong>and</strong><br />

thus needs to harbor only up to three pointers, one is tempted to combine the pointers for descendants <strong>and</strong><br />

pointers in the item list as shown in Fig. 4.47. The B-tree node thereby loses its actual identity, <strong>and</strong> the<br />

items assume the role of nodes in a regular binary tree. It remains necessary, however, to distinguish<br />

between pointers to descendants (vertical) <strong>and</strong> pointers to siblings on the same page (horizontal). Since<br />

only the pointers to the right may be horizontal, a single bit is sufficient to record this distiction. We<br />

therefore introduce the Boolean field h with the meaning horizontal. The definition of a tree node based on<br />

this representation is given below. It was suggested <strong>and</strong> investigated by R. Bayer [4-3] in 1971 <strong>and</strong><br />

represents a search tree organization guaranteeing p = 2*log(N) as maximum path length.<br />

TYPE Node = POINTER TO RECORD<br />

key: INTEGER;<br />

...........<br />

left, right: Node;<br />

h: BOOLEAN (*right branch horizontal*)

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

Saved successfully!

Ooh no, something went wrong!