next up previous contents
Next: Simple Examples of Binary Up: The Queue and Binary Previous: Specification of Stack and

Algebraic Specification of a Binary Tree

Our next example features the algebraic specification of a familiar data structure, the binary tree. Trees are one of the most important nonlinear structures in computer science. In part, this is due to the fact that they provide natural representations for many kinds of hierarchical and nested data that arise in computer applications. File index schemes and hierarchical data base management systems, for example, often make use of tree structures.

This example of the specification of a binary tree (a tree where each node has no more than two child nodes) will be further explored later when we discuss hidden or private operations. For the binary tree, the operations normally required are

As with the previous examples, we assume for the sake of expediency, that the data elements are natural numbers. The individual signatures of the operations are then given by

   empty     :  ->  binary-tree
 
   make      :  binary-tree  nat  binary-tree ->  binary-tree

   left      :  binary-tree  ->  binary-tree

   right     :  binary-tree  ->  binary-tree

   node      :  binary-tree  ->  nat

   is-empty? :  binary-tree  ->   bool

   is-in?    :  binary-tree  nat  ->  bool
where binary-tree is the sort introduced by the abstract data type Binary-tree.

The atomic constructors for this abstract data type are empty and make since any binary tree can be constructed using a composition of these two constructors (the operations left and right are non-atomic constructors for this example and such operations are sometimes referred to as ``destructors''). The complete specification is shown in Fig. 9.4. We have introduced the nullary operation nat-error    :     $\rightarrow$    nat to deal with the result of trying to access the value corresponding to the root of an empty tree.


  
Figure 9.4: Algebraic specification for a binary tree
\begin{figure}\footnotesize
\begin{tex2html_preform}\begin{verbatim}SPEC Binary-...
...-in?(r,n1) ENDIF
ENDSPEC\end{verbatim}\end{tex2html_preform}\par
\end{figure}



 
next up previous contents
Next: Simple Examples of Binary Up: The Queue and Binary Previous: Specification of Stack and
Lee McCluskey
2002-12-18