Algorithms and Data Structures
Algorithms and Data Structures
Algorithms and Data Structures
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 147<br />
(*input phase*)<br />
Texts.Scan(S);<br />
WHILE S.class = Texts.Int DO<br />
x := S.i; Texts.Scan(S); y := S.i; p := find(x); q := find(y);<br />
NEW(t); t.id := q; t.next := p.trail;<br />
p.trail := t; INC(q.count); Texts.Scan(S)<br />
END;<br />
(*search for leaders without predecessors*)<br />
p := head; head := NIL;<br />
WHILE p # tail DO<br />
q := p; p := q.next;<br />
IF q.count = 0 THEN (*insert q in new chain*)<br />
q.next := head; head := q<br />
END<br />
END;<br />
(*output phase*) q := head;<br />
WHILE q # NIL DO<br />
Texts.WriteLn(W); Texts.WriteInt(W, q.key, 8); DEC(n);<br />
t := q.trail; q := q.next;<br />
WHILE t # NIL DO<br />
p := t.id; DEC(p.count);<br />
IF p.count = 0 THEN (*insert p in leader list*)<br />
p.next := q; q := p<br />
END;<br />
t := t.next<br />
END<br />
END;<br />
IF n # 0 THEN<br />
Texts.WriteString(W, "This set is not partially ordered")<br />
END;<br />
Texts.WriteLn(W)<br />
END TopSort.<br />
4.4 Tree <strong>Structures</strong><br />
4.4.1 Basic Concepts <strong>and</strong> Definitions<br />
We have seen that sequences <strong>and</strong> lists may conveniently be defined in the following way: A sequence<br />
(list) with base type T is either<br />
1. The empty sequence (list).<br />
2. The concatenation (chain) of a T <strong>and</strong> a sequence with base type T.<br />
Hereby recursion is used as an aid in defining a structuring principle, namely, sequencing or iteration.<br />
Sequences <strong>and</strong> iterations are so common that they are usually considered as fundamental patterns of<br />
structure <strong>and</strong> behaviour. But it should be kept in mind that they can be defined in terms of recursion,<br />
whereas the reverse is not true, for recursion may be effectively <strong>and</strong> elegantly used to define much more<br />
sophisticated structures. Trees are a well-known example. Let a tree structure be defined as follows: A<br />
tree structure with base type T is either<br />
1. The empty structure.