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

(1) 1 2; 3; 4 5 6; 7;<br />

(2) 5 4; 3; 1 2 7 6;<br />

(3) 6 2; 4; 1 7 3 5;<br />

(4) 4 2 6; 1 7; 3 5;<br />

(1)<br />

1 2<br />

2<br />

2 4<br />

4<br />

1<br />

3 1<br />

3<br />

5 6 2<br />

6<br />

1 3<br />

5 7<br />

(2)<br />

4 5<br />

4<br />

2 4<br />

6<br />

3<br />

5 1<br />

3<br />

5<br />

7<br />

(3)<br />

2 6<br />

4<br />

4<br />

2<br />

6 1 2<br />

3<br />

5 6<br />

7<br />

(4)<br />

2 4<br />

6<br />

2<br />

6<br />

2 6<br />

1<br />

4<br />

7<br />

1<br />

3 4<br />

5<br />

7<br />

Fig. 4.50. Insertion of keys 1 to 7<br />

These pictures make the third property of B-trees particularly obvious: all terminal nodes appear on the<br />

same level. One is therefore inclined to compare these structures with garden hedges that have been<br />

recently trimmed with hedge scissors.<br />

The algorithm for the construction of SBB-trees is show below. It is based on a definition of the type<br />

Node with the two components lh <strong>and</strong> rhindicating whether or not the left <strong>and</strong> right pointers are horizontal.<br />

TYPE Node = RECORD<br />

key, count: INTEGER;<br />

left, right: POINTER TO Node;<br />

lh, rh: BOOLEAN<br />

END<br />

The recursive procedure search again follows the pattern of the basic binary tree insertion algorithm. A<br />

third parameter h is added; it indicates whether or not the subtree with root p has changed, <strong>and</strong> it<br />

corresponds directly to the parameter h of the B-tree search program. We must note, however, the<br />

consequence of representing pages as linked lists: a page is traversed by either one or two calls of the<br />

search procedure. We must distinguish between the case of a subtree (indicated by a vertical pointer) that<br />

has grown <strong>and</strong> a sibling node (indicated by a horizontal pointer) that has obtained another sibling <strong>and</strong> hence<br />

requires a page split. The problem is easily solved by introducing a three-valued h with the following<br />

meanings:

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

Saved successfully!

Ooh no, something went wrong!