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 154<br />
PROCEDURE preorder (t: Node);<br />
BEGIN<br />
IF t # NIL THEN<br />
P(t); preorder(t.left); preorder(t.right)<br />
END<br />
END preorder<br />
PROCEDURE inorder (t: Node);<br />
BEGIN<br />
IF t # NIL THEN<br />
inorder(t.left); P(t); inorder(t.right)<br />
END<br />
END inorder<br />
PROCEDURE postorder (t: Node);<br />
BEGIN<br />
IF t # NIL THEN<br />
postorder(t.left); postorder(t.right); P(t)<br />
END<br />
END postorder<br />
Note that the pointer t is passed as a value parameter. This expresses the fact that the relevant entity is<br />
the reference to the considered subtree <strong>and</strong> not the variable whose value is the pointer, <strong>and</strong> which could be<br />
changed in case t were passed as a variable parameter.<br />
An example of a tree traversal routine is that of printing a tree, with appropriate indentation indicating<br />
each node's level.<br />
Binary trees are frequently used to represent a set of data whose elements are to be retrievable through<br />
a unique key. If a tree is organized in such a way that for each node t i , all keys in the left subtree of t i are<br />
less than the key of t i , <strong>and</strong> those in the right subtree are greater than the key of t i , then this tree is called a<br />
search tree. In a search tree it is possible to locate an arbitrary key by starting at the root <strong>and</strong> proceeding<br />
along a search path switching to a node's left or right subtree by a decision based on inspection of that<br />
node's key only. As we have seen, n elements may be organized in a binary tree of a height as little as<br />
log(n). Therefore, a search among n items may be performed with as few as log(n) comparsions if the tree<br />
is perfectly balanced. Obviously, the tree is a much more suitable form for organizing such a set of data<br />
than the linear list used in the previous section. As this search follows a single path from the root to the<br />
desired node, it can readily be programmed by iteration:<br />
PROCEDURE locate (x: INTEGER; t: Node): Node;<br />
BEGIN<br />
WHILE (t # NIL) & (t.key # x) DO<br />
IF t.key < x THEN t := t.right ELSE t := t.left END<br />
END;<br />
RETURN t<br />
END locate<br />
The function locate(x, t) yields the value NIL, if no key with value x is found in the tree with root t. As in<br />
the case of searching a list, the complexity of the termination condition suggests that a better solution may<br />
exist, namely the use of a sentinel. This technique is equally applicable in the case of a tree. The use of<br />
pointers makes it possible for all branches of the tree to terminate with the same sentinel. The resulting<br />
structure is no longer a tree, but rather a tree with all leaves tied down by strings to a single anchor point