20.03.2013 Views

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

NOTES ON DATA STRUCTURING 145<br />

The final example shows how the binary forking tree familiar to LISP<br />

programmers may be defined as a recursive data structure.<br />

type list = (atom:sequence character, c<strong>on</strong>s: (car, cdr:list)).<br />

A list which in LISP dot-notati<strong>on</strong> would be expressed<br />

((A. (B. NIL)). NIL)<br />

can be expressed as a value of type list in almost exactly the same way as it is<br />

in LISP:<br />

c<strong>on</strong>s (c<strong>on</strong>s (atom ("A"),<br />

c<strong>on</strong>s (atom ("B"), atom ("NIL"))),<br />

atom ("NIL")<br />

);<br />

where the type transfer to list type is left implicit.<br />

As an example of the processing of a list, we write a functi<strong>on</strong> to reverse a<br />

complete tree, so that every "left fork" in it becomes a "right fork" and<br />

vice-versa.<br />

functi<strong>on</strong> reverse (l: list): list;<br />

9.1. REPRESENTATION<br />

with l do<br />

(atom:reverse: = 1,<br />

c<strong>on</strong>s:reverse: = c<strong>on</strong>s (reverse (cdr), reverse (car)))<br />

The standard representati<strong>on</strong> of a recursive type is also very similar to<br />

that of a similarly structured n<strong>on</strong>-recursive type, with the excepti<strong>on</strong> that each<br />

comp<strong>on</strong>ent specified as bel<strong>on</strong>ging to the recursive type itself is represented<br />

by a locati<strong>on</strong> c<strong>on</strong>taining a pointer to its value, rather than the value itself.<br />

This use of a pointer is motivated by the fact that the comp<strong>on</strong>ent value may<br />

be of arbitrary size; and it is not possible to allocate any fixed amount<br />

of storage to c<strong>on</strong>tain it. This is known as the "tree representati<strong>on</strong>", and is<br />

similar to the tree representati<strong>on</strong> of an array or sequence, except that the<br />

branches may grow to arbitrary and varying heights.<br />

An alternative method of representati<strong>on</strong> is the linear sequence or bitstream.<br />

In this representati<strong>on</strong> it is possible to avoid the use of pointers, and place<br />

the values of recursive substructures c<strong>on</strong>tiguous with the rest of the infor-<br />

mati<strong>on</strong>, just as they are in the familiar bracketed character representati<strong>on</strong>s<br />

of expressi<strong>on</strong>s. However instead of using brackets, we can reestablish the<br />

bracketing structure by c<strong>on</strong>text, and if necessary by scanning the tag of<br />

uni<strong>on</strong> values. This method is usually associated with packed representati<strong>on</strong>s<br />

of the other comp<strong>on</strong>ents, and a very significant reducti<strong>on</strong> in storage may be<br />

achieved, at the expense of enforcing serial access to the comp<strong>on</strong>ents of the

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

Saved successfully!

Ooh no, something went wrong!