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

prints the resulting tree: The empty tree results in no printing, the subtree at level L in first printing its own<br />

left subtree, then the node, properly indented by preceding it with L tabs, <strong>and</strong> finally in printing its right<br />

subtree.<br />

4.4.2 Basic Operations on Binary Trees<br />

There are many tasks that may have to be perfomed on a tree structure; a common one is that of<br />

executing a given operation P on each element of the tree. P is then understood to be a parameter of the<br />

more general task of visting all nodes or, as it is usually called, of tree traversal. If we consider the task as a<br />

single sequential process, then the individual nodes are visited in some specific order <strong>and</strong> may be<br />

considered as being laid out in a linear arrangement. In fact, the description of many algorithms is<br />

considerably facilitated if we can talk about processing the next element in the tree based in an underlying<br />

order. There are three principal orderings that emerge naturally from the structure of trees. Like the tree<br />

structure itself, they are conveniently expressed in recursive terms. Referring to the binary tree in Fig. 4.24<br />

in which R denotes the root <strong>and</strong> A <strong>and</strong> B denote the left <strong>and</strong> right subtrees, the three orderings are<br />

1. Preorder: R, A, B (visit root before the subtrees)<br />

2. Inorder: A, R, B<br />

3. Postorder: A, B, R (visit root after the subtrees)<br />

R<br />

A<br />

B<br />

Fig. 4.24. Binary tree<br />

Traversing the tree of Fig. 4.20 <strong>and</strong> recording the characters seen at the nodes in the sequence of<br />

encounter, we obtain the following orderings:<br />

1. Preorder: * + a / b c - d * e f<br />

2. Inorder: a + b / c * d - e * f<br />

3. Postorder: a b c / + d e f * - *<br />

We recognize the three forms of expressions: preorder traversal of the expression tree yields prefix<br />

notation; postorder traversal generates postfix notation; <strong>and</strong> inorder traversal yields conventional infix<br />

notation, although without the parentheses necessary to clarify operator precedences.<br />

Let us now formulate the three methods of traversal by three concrete programs with the explicit<br />

parameter t denoting the tree to be operated upon <strong>and</strong> with the implicit parameter P denoting the operation<br />

to be performed on each node. Assume the following definitions:<br />

TYPE Node = POINTER TO RECORD ... left, right: Node END<br />

The three methods are now readily formulated as recursive procedures; they demonstrate again the fact<br />

that operations on recursively defined data structures are most conveniently defined as recursive algorithms.

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

Saved successfully!

Ooh no, something went wrong!