25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 193<br />

emerges as an alternative to the AVL-balancing criterion. A performance comparison is therefore both<br />

possible <strong>and</strong> desirable.<br />

We refrain from involved mathematical analysis <strong>and</strong> concentrate on some basic differences. It can be<br />

proven that the AVL-balanced trees are a subset of the SBB-trees. Hence, the class of the latter is larger.<br />

It follows that their path length is on the average larger than in the AVL case. Note in this connection the<br />

worst-case tree (4) in Fig. 4.50. On the other h<strong>and</strong>, node rearrangement is called for less frequently. The<br />

balanced tree is therefore preferred in those applications in which key retrievals are much more frequent<br />

than insertions (or deletions); if this quotient is moderate, the SBB-tree scheme may be preferred. It is very<br />

difficult to say where the borderline lies. It strongly depends not only on the quotient between the<br />

frequencies of retrieval <strong>and</strong> structural change, but also on the characteristics of an implementation. This is<br />

particularly the case if the node records have a densely packed representation, <strong>and</strong> if therefore access to<br />

fields involves part-word selection.<br />

The SBB-tree has later found a rebirth under the name of red-black tree. The difference is that whereas<br />

in the case of the symmetric, binary B-tree every node contains two h-fields indicating whether the<br />

emanating pointers are horizontal, every node of the red-black tree contains a single h-field, indicating<br />

whether the incoming pointer is horizontal. The name stems from the idea to color nodes with incoming<br />

down-pointer black, <strong>and</strong> those with incoming horizontal pointer red. No two red nodes can immediately<br />

follow each other on any path. Therefore, like in the cases of the BB- <strong>and</strong> SBB-trees, every search path is<br />

at most twice as long as the height of the tree. There exists a canonical mapping from binary B-trees to<br />

red-black trees.<br />

4.8 Priority Search Trees<br />

Trees, <strong>and</strong> in particular binary trees, constitute very effective organisations for data that can be ordered<br />

on a linear scale. The preceding chapters have exposed the most frequently used ingenious schemes for<br />

efficient searching <strong>and</strong> maintenance (insertion, deletion). Trees, however, do not seem to be helpful in<br />

problems where the data are located not in a one-dimensional, but in a multi-dimensional space. In fact,<br />

efficient searching in multi-dimensional spaces is still one of the more elusive problems in computer science,<br />

the case of two dimensions being of particular importance to many practical applications.<br />

Upon closer inspection of the subject, trees might still be applied usefully at least in the two-dimensional<br />

case. After all, we draw trees on paper in a two-dimensional space. Let us therefore briefly review the<br />

characteristics of the two major kinds of trees so far encountered.<br />

1. A search tree is governed by the invariants<br />

p.left ≠ NIL implies p.left.x < p.x,<br />

p.right ≠ NIL implies p.x < p.right.x,<br />

holding for all nodes p with key x. It is apparent that only the horizontal position of nodes is at all<br />

constrained by the invariant, <strong>and</strong> that the vertical positions of nodes can be arbitrarily chosen such that<br />

access times in searching, (i.e. path lengths) are minimized.<br />

2. A heap, also called priority tree, is governed by the invariants<br />

p.left ≠ NIL implies p.y ≤ p.left.y,<br />

p.right ≠ NIL implies p.y ≤ p.right.y,<br />

holding for all nodes p with key y. Here evidently only the vertical positions are constrained by the<br />

invariants.<br />

It seems straightforward to combine these two conditions in a definition of a tree organization in a twodimensional<br />

space, with each node having two keys x <strong>and</strong> y which can be regarded as coordinates of the<br />

node. Such a tree represents a point set in a plane, i.e. in a two-dimensional Cartesian space; it is therefore

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

Saved successfully!

Ooh no, something went wrong!