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

4 Dynamic Information <strong>Structures</strong><br />

4.1 Recursive <strong>Data</strong> Types<br />

In Chap. 1 the array, record, <strong>and</strong> set structures were introduced as fundamental data structures. They<br />

are called fundamental because they constitute the building blocks out of which more complex structures<br />

are formed, <strong>and</strong> because in practice they do occur most frequently. The purpose of defining a data type,<br />

<strong>and</strong> of thereafter specifying that certain variables be of that type, is that the range of values assumed by<br />

these variables, <strong>and</strong> therefore their storage pattern, is fixed once <strong>and</strong> for all. Hence, variables declared in<br />

this way are said to be static. However, there are many problems which involve far more complicated<br />

information structures. The characteristic of these problems is that not only the values but also the<br />

structures of variables change during the computation. They are therefore called dynamic structures.<br />

Naturally, the components of such structures are — at some level of resolution — static, i.e., of one of the<br />

fundamental data types. This chapter is devoted to the construction, analysis, <strong>and</strong> management of dynamic<br />

information structures.<br />

It is noteworthy that there exist some close analogies between the methods used for structuring<br />

algorithms <strong>and</strong> those for structuring data. As with all analogies, there remain some differences, but a<br />

comparison of structuring methods for programs <strong>and</strong> data is nevertheless illuminating.<br />

The elementary, unstructured statement is the assignment of an expression's value to a variable. Its<br />

corresponding member in the family of data structures is the scalar, unstructured type. These two are the<br />

atomic building blocks for composite statements <strong>and</strong> data types. The simplest structures, obtained through<br />

enumeration or sequencing, are the compound statement <strong>and</strong> the record structure. They both consist of a<br />

finite (usually small) number of explicitly enumerated components, which may themselves all be different<br />

from each other. If all components are identical, they need not be written out individually: we use the for<br />

statement <strong>and</strong> the array structure to indicate replication by a known, finite factor. A choice among two or<br />

more elements is expressed by the conditional or the case statement <strong>and</strong> by extensions of record types,<br />

respectively. And finally, a repetiton by an initially unknown (<strong>and</strong> potentially infinite) factor is expressed by<br />

the while <strong>and</strong> repeat statements. The corresponding data structure is the sequence (file), the simplest kind<br />

which allows the construction of types of infinite cardinality.<br />

The question arises whether or not there exists a data structure that corresponds in a similar way to the<br />

procedure statement. Naturally, the most interesting <strong>and</strong> novel property of procedures in this respect is<br />

recursion. Values of such a recursive data type would contain one or more components belonging to the<br />

same type as itself, in analogy to a procedure containing one or more calls to itself. Like procedures, data<br />

type definitions might be directly or indirectly recursive.<br />

A simple example of an object that would most appropriately be represented as a recursively defined<br />

type is the arithmetic expression found in programming languages. Recursion is used to reflect the<br />

possibility of nesting, i.e., of using parenthesized subexpressions as oper<strong>and</strong>s in expressions. Hence, let an<br />

expression here be defined informally as follows:<br />

An expression consists of a term, followed by an operator, followed by a term. (The two terms<br />

constitute the oper<strong>and</strong>s of the operator.) A term is either a variable — represented by an identifier — or<br />

an expression enclosed in parentheses.<br />

A data type whose values represent such expressions can easily be described by using the tools already<br />

available with the addition of recursion:<br />

TYPE expression = RECORD op: INTEGER;<br />

opd1, opd2: term<br />

END

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

Saved successfully!

Ooh no, something went wrong!