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

a)<br />

10<br />

b)<br />

10<br />

5 15<br />

5 15<br />

3 8 13 18<br />

3 8 18<br />

c)<br />

10<br />

d)<br />

10<br />

e)<br />

8<br />

5 18<br />

3 18<br />

3 18<br />

3 8<br />

8<br />

Fig. 4.28. Tree deletion<br />

4.4.5 Analysis of Tree Search <strong>and</strong> Insertion<br />

It is a natural reaction to be suspicious of the algorithm of tree search <strong>and</strong> insertion. At least one should<br />

retain some skepticism until having been given a few more details about its behaviour. What worries many<br />

programmers at first is the peculiar fact that generally we do not know how the tree will grow; we have no<br />

idea about the shape that it will assume. We can only guess that it will most probably not be the perfectly<br />

balanced tree. Since the average number of comparisons needed to locate a key in a perfectly balanced<br />

tree with n nodes is approximately log(n), the number of comparisons in a tree generated by this algorithm<br />

will be greater. But how much greater?<br />

First of all, it is easy to find the worst case. Assume that all keys arrive in already strictly ascending (or<br />

descending) order. Then each key is appended immediately to the right (left) of its predecessor, <strong>and</strong> the<br />

resulting tree becomes completely degenerate, i.e., it turns out to be a linear list. The average search effort<br />

is then n/2 comparisons. This worst case evidently leads to a very poor performance of the search<br />

algorithm, <strong>and</strong> it seems to fully justify our skepticism. The remaining question is, of course, how likely this<br />

case will be. More precisely, we should like to know the length a n of the search path averaged over all n<br />

keys <strong>and</strong> averaged over all n! trees that are generated from the n! permutations of the original n distinct<br />

keys. This problem of algorithmic analysis turns out to be fairly straightforward, <strong>and</strong> it is presented here as<br />

a typical example of analyzing an algorithm as well as for the practical importance of its result.<br />

Given are n distinct keys with values 1, 2, ... , n. Assume that they arrive in a r<strong>and</strong>om order. The<br />

probability of the first key — which notably becomes the root node — having the value i is 1/n. Its left<br />

subtree will eventually contain i−1 nodes, <strong>and</strong> its right subtree n−i nodes (see Fig. 4.29). Let the average<br />

path length in the left subtree be denoted by a i-1 , <strong>and</strong> the one in the right subtree is a n-i , again assuming<br />

that all possible permutations of the remaining n−1 keys are equally likely. The average path length in a tree<br />

with n nodes is the sum of the products of each node's level <strong>and</strong> its probability of access. If all nodes are<br />

assumed to be searched with equal likelihood, then<br />

a n<br />

= (Si: 1 ≤ i ≤ n: p i ) / n<br />

where p i is the path length of node i.

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

Saved successfully!

Ooh no, something went wrong!