| 1 |
% This is epodd.tex, the description of the treetex macro package as it will |
| 2 |
% appear in EP-ODD in summer 89. It is in some aspects more general |
| 3 |
% than tree_doc.tex and corrects an error in the computation of |
| 4 |
% the number of registers used by treetex. The user interface of |
| 5 |
% treetex is explained in more detail in tree_doc.tex. |
| 6 |
|
| 7 |
\documentstyle[12pt,fullpage]{article} |
| 8 |
|
| 9 |
\clubpenalty=10000 |
| 10 |
\widowpenalty=10000 |
| 11 |
|
| 12 |
\def\addcontentsline#1#2#3{\relax}% Some captions are too long for some |
| 13 |
% TeX installations (buffer size too small) |
| 14 |
|
| 15 |
\newenvironment{lemma}{\begingroup\samepage\begin{lemmma}\ }{\end{lemmma}% |
| 16 |
\endgroup} |
| 17 |
\newtheorem{lemmma}{Lemma}[section] |
| 18 |
\newenvironment{proof}{\begin{prooof}\rm\ \nopagebreak}{\end{prooof}} |
| 19 |
\newcommand{\proofend}{\qquad\ifmmode\Box\else$\Box$\fi} |
| 20 |
\newtheorem{prooof}{Proof} |
| 21 |
\renewcommand{\theprooof}{} % makes shure that prooof doesn't get numbers |
| 22 |
\newenvironment{Figure}{\begin{figure}\vspace{1\baselineskip}}% |
| 23 |
{\vspace{1\baselineskip}\end{figure}} |
| 24 |
\newlength{\figspace} % space between figures in a single |
| 25 |
\setlength{\figspace}{30pt} % Figure environment |
| 26 |
|
| 27 |
\newcommand{\var}[1]{{\it #1\/}} % use it for names of variables |
| 28 |
\newcommand{\emph}[1]{{\em #1\/}} % use it for emphazided text |
| 29 |
% (This notion sticks to the |
| 30 |
% applicative style of markup.) |
| 31 |
\renewcommand{\O}{{\rm O}} % O-notation, also for math mode |
| 32 |
\newcommand{\T}{{\cal T}} % the set T in math mode |
| 33 |
\newcommand{\TreeTeX}{Tree\TeX} |
| 34 |
\newcommand{\fig}[1]{Figure~\ref{#1}} |
| 35 |
\let\p\par |
| 36 |
|
| 37 |
\input treetex |
| 38 |
\Treestyle{\vdist{20pt}\minsep{16pt}} |
| 39 |
\dummyhalfcenterdim@n=2pt |
| 40 |
|
| 41 |
% \def\Tree#1\end#2{\end{Tree}} % Trees are not processed |
| 42 |
% \let\endTree\relax % |
| 43 |
|
| 44 |
\def\Node(#1,#2){\put(#1,#2){\circle*{4}}} |
| 45 |
\def\Edge(#1,#2,#3,#4,#5){\put(#1,#2){\line(#3,#4){#5}}} |
| 46 |
|
| 47 |
\def\enode{\node{\external\type{dot}}} |
| 48 |
\def\inode{\node{\type{dot}}} |
| 49 |
|
| 50 |
\def\e{\node{\external\type{dot}}} |
| 51 |
\def\i{\node{\type{dot}}} |
| 52 |
\def\il{\node{\type{dot}\leftonly}} |
| 53 |
\def\ir{\node{\type{dot}\rightonly}} |
| 54 |
|
| 55 |
\newcommand{\stack}[3]{% |
| 56 |
\vtop{\settowidth{\hsize}{#1}% |
| 57 |
\setlength{\leftskip}{0pt plus 1fill}% |
| 58 |
\setlength{\baselineskip}{#2}#3}} |
| 59 |
|
| 60 |
\let\multic\multicolumn |
| 61 |
|
| 62 |
\newlength{\hd} % hidden digit |
| 63 |
\setbox0\hbox{1} |
| 64 |
\settowidth{\hd}{\usebox{0}} |
| 65 |
\newcommand{\ds}{\hspace{\hd}} % digit space |
| 66 |
|
| 67 |
\newcommand{\ccol}[1]{\multicolumn{1}{c}{#1}} |
| 68 |
|
| 69 |
\hyphenation{post-or-der sym-bol Karls-ruhe bool-ean} |
| 70 |
|
| 71 |
\begin{document} |
| 72 |
|
| 73 |
\bibliographystyle{plain} |
| 74 |
|
| 75 |
\title{Drawing Trees Nicely with \TeX\thanks{This work was supported by |
| 76 |
a Natural Sciences and Engineering Research Council of Canada |
| 77 |
Grant~A-5692, a Deutsche Forschungsgemeinschaft Grant~Sto167/1-1, |
| 78 |
and a grant from the Information Technology Research Centre. |
| 79 |
It was begun during the first author's stay with |
| 80 |
the Data Structuring Group in Waterloo.}} |
| 81 |
\author{Anne Br\"uggemann-Klein\thanks{Institut f\"ur Informatik, |
| 82 |
Universit\"at Freiburg, Rheinstr.~10--12, 7800~Freiburg, |
| 83 |
West~Germany}\ \and Derick Wood\thanks{Data |
| 84 |
Structuring Group, Department of Computer Science, University of |
| 85 |
Waterloo, Waterloo, Ontario N2L~3G1, Canada}} |
| 86 |
\date{} |
| 87 |
\maketitle |
| 88 |
|
| 89 |
\begin{abstract} |
| 90 |
|
| 91 |
We present a new solution to the tree drawing problem that |
| 92 |
integrates an excellent tree drawing algorithm into one of the best text |
| 93 |
processing systems available. More precisely, we present a \TeX{} macro package |
| 94 |
called \TreeTeX{} that produces drawings of trees from a purely logical |
| 95 |
description. Our approach has three advantages: Labels |
| 96 |
for nodes can be handled in a reasonable way; porting |
| 97 |
\TreeTeX{} to any site running \TeX{} is a trivial operation; and |
| 98 |
modularity in the description of a tree and \TeX{}'s macro capabilities |
| 99 |
allow for libraries of subtrees and tree classes. |
| 100 |
|
| 101 |
In addition, \TreeTeX{} has an option that produces |
| 102 |
drawings that make the |
| 103 |
\emph{structure} of the trees more obvious to the human eye, |
| 104 |
even though they may not be as aesthetically pleasing. |
| 105 |
|
| 106 |
\end{abstract} |
| 107 |
|
| 108 |
\section{Introduction} |
| 109 |
|
| 110 |
The problem of successfully integrating pictures and text in a |
| 111 |
document processing environment is tantalizing and difficult. |
| 112 |
Although there are systems available that allow such integration, they |
| 113 |
fall short in many ways, usually in document quality. Furthermore, |
| 114 |
most authors using document preparation systems are neither book |
| 115 |
designers nor graphic artists. Just as modern document preparation |
| 116 |
systems do not expect an author to be a book designer, so we would |
| 117 |
prefer that they do not expect an author to be a graphic artist. The |
| 118 |
second author, Wood, needed to draw many trees in a series of papers |
| 119 |
on trees and in a projected book on trees. This problem enabled us |
| 120 |
to tackle the integration issue for one subarea of graphics, namely, |
| 121 |
tree drawing. We had the decided advantage that there already existed good |
| 122 |
algorithms to draw trees {\em without any author intervention}. |
| 123 |
Previous experience of the integration of pictures and text had been |
| 124 |
uninspiring; the systems expected the author to prepare each picture |
| 125 |
in total. For example, a tree could be built up from smaller |
| 126 |
subtrees but the relative placement of them was left to the author. |
| 127 |
This situation continues to hold today with the drawing facilities |
| 128 |
available on most personal computers, and, because of this, the |
| 129 |
resulting figures still appear to be ``hand-drawn.'' Additionally, |
| 130 |
they are of inferior quality when compared with the quality of |
| 131 |
the surrounding text. |
| 132 |
|
| 133 |
In this paper we present an entirely new solution that integrates |
| 134 |
a tree drawing algorithm into one of the best text processing |
| 135 |
systems available. More precisely, we describe \TreeTeX{}, a |
| 136 |
\TeX{} macro package that produces an aesthetically pleasing |
| 137 |
drawing of a tree from a purely logical description. |
| 138 |
We made two fundamental design |
| 139 |
decisions that heavily influenced the method of implementation. |
| 140 |
First, we wanted to allow an author to label the nodes of a tree. |
| 141 |
This decision means that the tree drawing package must be able to |
| 142 |
typeset labels exactly as they would be typeset by the typesetting |
| 143 |
program. There are two reasons for this. Text should be typeset |
| 144 |
consistently, wherever it appears in a document, and the tree |
| 145 |
drawing program needs to know the dimensions of the typeset labels. |
| 146 |
Second, we wanted to ensure that the program could be ported |
| 147 |
easily to other installations and sites, so that other, putative |
| 148 |
users would be able to use it easily. |
| 149 |
Indeed, \TreeTeX{} has been used successfully to typeset trees in |
| 150 |
\cite{BaezaTrees}, \cite{KWIFIP}, and \cite{OAPD}. |
| 151 |
|
| 152 |
By basing our package on \TeX{}, which for more subjective |
| 153 |
reasons we preferred over other typesetting systems such as |
| 154 |
troff, we could ensure wide interest |
| 155 |
in the package. By implementing it as a \TeX{} macro package |
| 156 |
instead of a preprocessor |
| 157 |
we made porting trivial and, furthermore, this also ensured |
| 158 |
consistency of typeset text within a document. |
| 159 |
The down side of this decision is that we had to program with |
| 160 |
\TeX{} macros, not an experience to be recommended, and we had to live |
| 161 |
with the inherent register limitations of \TeX{}. |
| 162 |
|
| 163 |
This paper consists of a further nine sections. In Sections~2, 3 and~4, |
| 164 |
we discuss the aesthetics of tree drawing and the algorithm of |
| 165 |
Reingold and Tilford~\cite{TidierTrees}. In Sections~5, 6, and~7, we |
| 166 |
describe our method of incorporating tree drawing into \TeX{}. Then, |
| 167 |
in the last three short sections, we consider the expected number of |
| 168 |
registers \TeX{} needs to draw a tree, the user interface (and three |
| 169 |
\TreeTeX{} examples), and discussion of, among other things, the |
| 170 |
performance of \TreeTeX{}. |
| 171 |
|
| 172 |
\section{Aesthetical criteria for drawing trees} |
| 173 |
|
| 174 |
In this paper, we are dealing with ordered |
| 175 |
trees in the sense of~\cite{ACP}, specifically binary and unary-binary |
| 176 |
trees. A {\em binary tree\/} is a finite set of nodes that either |
| 177 |
is empty, or consists of a root and two disjoint binary trees called |
| 178 |
the left and right subtrees of the root. A {\em unary-binary tree\/} is |
| 179 |
a finite set of nodes that either is empty, or consists of a root and |
| 180 |
two disjoint unary-binary trees, or consists of a root and one |
| 181 |
nonempty unary-binary tree. An {\em extended binary tree\/} is a binary tree |
| 182 |
in which each node has either two nonempty subtrees or two |
| 183 |
empty subtrees. |
| 184 |
|
| 185 |
There are some basic agreements on how such trees should be drawn, reflecting |
| 186 |
the top-down and left-right ordering of nodes in a tree. |
| 187 |
In \cite{TidierTrees} and \cite{TidyTrees} these basic agreements were |
| 188 |
formalized as the following axioms. |
| 189 |
|
| 190 |
\begin{enumerate} |
| 191 |
\item[1.] Trees impose a distance on the nodes; no node |
| 192 |
should be closer to the root than any of its |
| 193 |
ancestors. |
| 194 |
\item[2.] Nodes on the same level should lie on a straight |
| 195 |
line, and the straight lines defining the levels should be |
| 196 |
parallel. |
| 197 |
\item[3.] The relative order of nodes on any level should be the same |
| 198 |
as in the level order traversal of the tree. |
| 199 |
\end{enumerate} |
| 200 |
|
| 201 |
These axioms guarantee that trees are drawn as planar graphs: edges do |
| 202 |
not intersect except at nodes. Two further axioms improve the aesthetical |
| 203 |
appearance of trees. |
| 204 |
|
| 205 |
\begin{enumerate} |
| 206 |
\item[4.] In a unary-binary tree, each left child should be positioned |
| 207 |
to the left of its parent, each |
| 208 |
right child to the right of its parent, and each unary child |
| 209 |
should be positioned below its parent. |
| 210 |
\item[5.] A parent should be centered over its children. |
| 211 |
\end{enumerate} |
| 212 |
|
| 213 |
An additional axiom deals with the problem of tree drawings becoming too wide |
| 214 |
and therefore exceeding the physical limit of the output medium: |
| 215 |
|
| 216 |
\begin{enumerate} |
| 217 |
\item[6.] Tree drawings should occupy as little width as possible without |
| 218 |
violating the other axioms. |
| 219 |
\end{enumerate} |
| 220 |
|
| 221 |
In \cite{TidyTrees}, Wetherell and Shannon introduce two algorithms for |
| 222 |
tree drawings, the first of which fulfills axioms~1--5, and the second |
| 223 |
1--6. However, as Reingold and Tilford in \cite{TidierTrees} |
| 224 |
point out, there is a lack of symmetry in the algorithms of |
| 225 |
Wetherell and Shannon which may lead to unpleasant results; |
| 226 |
therefore, Reingold and Tilford introduce a new structured |
| 227 |
axiom. |
| 228 |
|
| 229 |
\begin{enumerate} |
| 230 |
\item[7.] A subtree of a given tree should be |
| 231 |
drawn the same way regardless of where it occurs in the tree. |
| 232 |
\end{enumerate} |
| 233 |
|
| 234 |
Axiom~7 allows the same tree to be drawn differently only when it occurs as |
| 235 |
a subtree in different trees. |
| 236 |
Reingold and Tilford give an algorithm which fulfills axioms~1--5 |
| 237 |
and~7. Although |
| 238 |
this algorithm doesn't fulfill axiom~6, |
| 239 |
the aesthetical improvements are well worth the additional space. |
| 240 |
\fig{algorithms} illustrates the benefits of axiom~7, and \fig{narrowtrees} |
| 241 |
shows that the algorithm of Reingold and Tilford violates axiom~6. |
| 242 |
|
| 243 |
\begin{Figure} |
| 244 |
\centering |
| 245 |
\leavevmode\noindent |
| 246 |
\begin{Tree} |
| 247 |
\enode |
| 248 |
\enode\enode\inode\enode\enode\inode\inode\inode |
| 249 |
\node{\external\type{dot}\rght{\unskip\hskip2\mins@p\hskip2\dotw@dth}} |
| 250 |
\enode\enode\inode\enode\enode\inode\inode\inode |
| 251 |
\inode |
| 252 |
\end{Tree} |
| 253 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\qquad |
| 254 |
\begin{Tree} |
| 255 |
\enode |
| 256 |
\enode\enode\inode\enode\enode\inode\inode\inode |
| 257 |
\enode |
| 258 |
\enode\enode\inode\enode\enode\inode\inode\inode |
| 259 |
\inode |
| 260 |
\end{Tree} |
| 261 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\ |
| 262 |
\caption{The left tree is drawn by the algorithm of Wetherell and Shannon, |
| 263 |
and the tidier right one is drawn by the algorithm of Reingold and Tilford.} |
| 264 |
\label{algorithms} |
| 265 |
|
| 266 |
\vspace{\figspace} |
| 267 |
\centering |
| 268 |
\leavevmode\noindent |
| 269 |
\begin{Tree} |
| 270 |
\enode\enode\enode\enode\enode\enode\enode\enode\enode |
| 271 |
\enode\inode\inode\inode |
| 272 |
\enode\inode\inode\inode |
| 273 |
\enode\inode\inode\inode |
| 274 |
\enode\inode\inode\inode |
| 275 |
\end{Tree} |
| 276 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\qquad |
| 277 |
\begin{Tree} |
| 278 |
\enode\enode\enode\enode\enode\enode\enode\enode |
| 279 |
\node{\external\type{dot}\rght{\unskip\hskip\mins@p\hskip\dotw@dth}} |
| 280 |
\enode\inode\inode\node{\type{dot}\rght{\unskip\hskip\mins@p\hskip\dotw@dth}} |
| 281 |
\enode\inode\inode\node{\type{dot}\rght{\unskip\hskip\mins@p\hskip\dotw@dth}} |
| 282 |
\enode\inode\inode\node{\type{dot}\rght{\unskip\hskip\mins@p\hskip\dotw@dth}} |
| 283 |
\enode\inode\inode\inode |
| 284 |
\end{Tree} |
| 285 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\ |
| 286 |
\caption{The left tree is drawn by the algorithm of Reingold and Tilford, but |
| 287 |
the right tree shows that narrower drawings fulfilling all aesthetic axioms |
| 288 |
are possible.} |
| 289 |
\label{narrowtrees} |
| 290 |
\end{Figure} |
| 291 |
|
| 292 |
|
| 293 |
\section{The algorithm of Reingold and Tilford} |
| 294 |
|
| 295 |
The algorithm of Reingold and Tilford (hereafter called ``the RT~algorithm'') |
| 296 |
takes a modular approach to the |
| 297 |
positioning of nodes. The relative positions of the nodes in a subtree |
| 298 |
are calculated independently of the rest of the tree. After the |
| 299 |
relative positions of two subtrees have been calculated, they can be |
| 300 |
joined as siblings in a larger tree by placing them as close |
| 301 |
together as possible and centering the parent node above them. |
| 302 |
Incidentally, this modular approach is the reason that the |
| 303 |
algorithm fails to fulfill axiom~6; see~\cite{Complexity}. |
| 304 |
Two sibling subtrees are placed as close together as possible, |
| 305 |
during a postorder traversal, as follows. |
| 306 |
Imagine that the two subtrees of a binary node |
| 307 |
have been drawn and cut out of paper along |
| 308 |
their contours. Then, starting with the two subtrees superimposed at their |
| 309 |
roots, move them apart until a minimal agreed upon distance |
| 310 |
between the trees is obtained at each level. This can be done gradually. |
| 311 |
Initially, their roots are separated by some agreed upon minimum |
| 312 |
distance; then, at the next level, they are pushed |
| 313 |
apart until the minimum separation is established there. |
| 314 |
This process is continued at successively lower levels until the |
| 315 |
last level of the shorter subtree is reached. At some levels no movement may be |
| 316 |
necessary, but at no level are the two subtrees moved closer |
| 317 |
together. When the process is complete, the position of the |
| 318 |
subtrees is fixed relative to their parent, which is centered over them. |
| 319 |
Assured that the subtrees will never be placed closer together, |
| 320 |
the postorder traversal is continued. |
| 321 |
|
| 322 |
A nontrivial implementation of |
| 323 |
this algorithm has been obtained by Reingold and Tilford in~\cite{TidierTrees} |
| 324 |
that runs in time $\O(N)$, where $N$ is the number of |
| 325 |
nodes of the tree to be drawn. |
| 326 |
Their crucial idea is to keep track of the contour of the subtrees |
| 327 |
by special pointers, called threads, such that whenever |
| 328 |
two subtrees are joined, only the |
| 329 |
top part of the trees down to the lowest level of the |
| 330 |
smaller tree need to be taken into account. |
| 331 |
|
| 332 |
The nodes are positioned on a fixed grid and are |
| 333 |
considered to have zero width; labeling is not provided. |
| 334 |
Although the algorithm only draws binary trees, it is easily |
| 335 |
extended to multiway trees. |
| 336 |
|
| 337 |
\section{Improving human perception of trees} |
| 338 |
|
| 339 |
It is common understanding in book design that aesthetics and readability |
| 340 |
don't necessarily coincide, and---as Lamport (\cite{LaTeX}) puts it---% |
| 341 |
``documents are meant to be read, not hung in museums.'' |
| 342 |
Therefore, readability is more important than aesthetics. |
| 343 |
|
| 344 |
When it comes to tree drawings, readability means that the structure of |
| 345 |
a tree must be easily recognizable. This criterion is not always met |
| 346 |
by the RT~algorithm. As an example, there are trees whose structure is |
| 347 |
different even though they have the same number |
| 348 |
of nodes on each level. The RT~algorithm might assign identical positions to |
| 349 |
these nodes making it very hard to perceive the structural differences. |
| 350 |
Hence, we have modified the RT~algorithm such that additional white space |
| 351 |
is inserted between subtrees of |
| 352 |
\emph{significant} nodes. Here a binary node |
| 353 |
is called significant if the minimum distance |
| 354 |
between its two subtrees is achieved \emph{below} their root level. |
| 355 |
Setting the amount of additional white space to zero retains the original RT~% |
| 356 |
placement. The effect of having nonzero additional white space between |
| 357 |
the subtrees of significant |
| 358 |
nodes is illustrated in \fig{addspace}. |
| 359 |
|
| 360 |
Another feature we have added to the RT~algorithms is the possibility to draw |
| 361 |
an unextended binary tree with the same placement of nodes as its |
| 362 |
associated extended version; |
| 363 |
this makes the structure of a tree more prominent; see \fig{extended}. |
| 364 |
We define the \emph{associated extended version} |
| 365 |
of a binary tree to be the binary tree obtained by replacing each empty subtree |
| 366 |
having a nonempty sibling with a subtree consisting of one node. |
| 367 |
|
| 368 |
\begin{Figure} |
| 369 |
\centering |
| 370 |
\leavevmode\noindent |
| 371 |
\begin{Tree} |
| 372 |
\e\il\e\e\i\i\il % the left subtree |
| 373 |
\e\ir\il % the right subtree |
| 374 |
\i |
| 375 |
\end{Tree} |
| 376 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\qquad |
| 377 |
\begin{Tree} |
| 378 |
\e\il\il\il % the left subtree |
| 379 |
\e\e\i\e\i\il % the right subtree |
| 380 |
\i |
| 381 |
\end{Tree} |
| 382 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\qquad |
| 383 |
\adds@p10pt |
| 384 |
\begin{Tree} |
| 385 |
\e\il\e\e\i\node{\type{dot}\lft{$\longrightarrow$}}\il % the left subtree |
| 386 |
\e\ir\il % the right subtree |
| 387 |
\node{\type{dot}\lft{$\longrightarrow$}} |
| 388 |
\end{Tree} |
| 389 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\qquad |
| 390 |
\begin{Tree} |
| 391 |
\e\il\il\il % the left subtree |
| 392 |
\e\e\i\e\i\il % the right subtree |
| 393 |
\node{\type{dot}\lft{$\longrightarrow$}} |
| 394 |
\end{Tree} |
| 395 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\ |
| 396 |
\adds@p0pt |
| 397 |
|
| 398 |
\caption{The nodes of the first two trees are placed in the same positions |
| 399 |
by the RT~algorithm, although the structure of the two trees is different. |
| 400 |
The alternative drawings highlight the structural differences |
| 401 |
of the trees by adding additional white space between the subtrees of |
| 402 |
($\longrightarrow$) significant nodes.} |
| 403 |
\label{addspace} |
| 404 |
\end{Figure} |
| 405 |
|
| 406 |
\begin{Figure} |
| 407 |
\centering |
| 408 |
\leavevmode\noindent |
| 409 |
\begin{Tree} |
| 410 |
\e\e\i\il\e\e\i\i |
| 411 |
\end{Tree} |
| 412 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\hbox{}\qquad |
| 413 |
\begin{Tree} |
| 414 |
\e\e\i\e\i\e\ir\i |
| 415 |
\end{Tree} |
| 416 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\hbox{}\\ |
| 417 |
\extended |
| 418 |
\begin{Tree} |
| 419 |
\e\e\i\il\e\e\i\i |
| 420 |
\end{Tree} |
| 421 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\hbox{}\qquad |
| 422 |
\begin{Tree} |
| 423 |
\e\e\i\e\i\e\ir\i |
| 424 |
\end{Tree} |
| 425 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\hbox{}\\ |
| 426 |
\noextended |
| 427 |
\begin{Tree} |
| 428 |
\e\e\i\e\i\e\e\i\i |
| 429 |
\end{Tree} |
| 430 |
\hskip\leftdist\box\TeXTree\hskip\rightdist\ |
| 431 |
\caption{As in the previous figure, the nodes of the first two trees |
| 432 |
are placed in the same position by the RT algorithm, |
| 433 |
although their structure is different. The modified |
| 434 |
RT~algorithms highlights the structural differences of the trees by |
| 435 |
drawing them like their identical extended |
| 436 |
version (given in the third row), but suppressing the additional nodes.} |
| 437 |
\label{extended} |
| 438 |
\end{Figure} |
| 439 |
|
| 440 |
|
| 441 |
\section{Trees in a document preparation environment} |
| 442 |
|
| 443 |
Drawings of trees do not usually appear by themselves, |
| 444 |
but are included in some text |
| 445 |
that is itself typeset by a text processing system. Therefore, a typical |
| 446 |
scenario is a pipe of three stages. First, we have a tree drawing |
| 447 |
program that calculates the positioning of the nodes of the tree to |
| 448 |
be drawn and outputs a description of the tree drawing in |
| 449 |
some graphics language; this is followed by |
| 450 |
a graphics system that transforms this |
| 451 |
description into an intermediate language that can be interpreted by the output |
| 452 |
device; and, finally, we have the |
| 453 |
text processing system that integrates the output of the |
| 454 |
graphics system into the text. |
| 455 |
|
| 456 |
This scenario loses its linear structure once nodes have to be labeled, since |
| 457 |
the labeling influences the positioning of the nodes. Labels usually occur |
| 458 |
inside, to the left of, to the right of, or beneath nodes (the latter only for |
| 459 |
external nodes). Their widths should certainly be taken into account |
| 460 |
by the tree drawing algorithm. But the labels have to be typeset first |
| 461 |
to determine their extensions, |
| 462 |
preferably by the typesetting program that |
| 463 |
is used for the regular text, because this ensures uniformity in the textual |
| 464 |
parts of the document and provides the author with the full power of a |
| 465 |
text processing system for composing the labels. Hence, a more complex |
| 466 |
communication scheme than a simple pipe is required. |
| 467 |
|
| 468 |
Although a system of two processes running simultaneously might be the most |
| 469 |
elegant solution, we wanted a system that is easily portable to |
| 470 |
widely different machines at our sites |
| 471 |
including personal computers with single process |
| 472 |
operating systems. |
| 473 |
Therefore, we decided to use a text processing system |
| 474 |
having programming facilities powerful enough to |
| 475 |
program a tree drawing algorithm and graphics facilities powerful enough |
| 476 |
to draw a tree. One text processing system |
| 477 |
rendering outstanding typographic quality and satisfactory programming |
| 478 |
facilities is \TeX, developed by Knuth at Stanford University; |
| 479 |
see~\cite{TeXbook}. |
| 480 |
The \TeX{} system includes the following programming facilities. |
| 481 |
|
| 482 |
\begin{enumerate} |
| 483 |
\item[1.] Datatypes:\\ |
| 484 |
integers~(256), dimensions\footnote{The term \emph{dimension} is used |
| 485 |
in \TeX\ to describe physical measurements of typographical objects; |
| 486 |
for example, the length of a word.}~(512), |
| 487 |
boxes~(256), tokenlists~(256), and |
| 488 |
boolean variables~(unrestricted). |
| 489 |
\item[2.] Elementary statements:\\ |
| 490 |
$a:=\rm const$, $a:=b$ (all types);\\ |
| 491 |
$a:=a+b$, $a:=a*b$, $a:=a/b$ (integers and dimensions); and\\ |
| 492 |
horizontal and vertical nesting of boxes. |
| 493 |
\item[3.] Control constructs:\\ |
| 494 |
if-then-else statements testing relations between integers, |
| 495 |
dimensions, boxes, or boolean variables. |
| 496 |
\item[4.] Modularization constructs:\\ |
| 497 |
macros with up to 9~parameters (can be viewed as procedures without |
| 498 |
the concept of local variables). |
| 499 |
\end{enumerate} |
| 500 |
|
| 501 |
Although the programming |
| 502 |
facilities of \TeX{} hardly exceed the abilities of a Turing machine, |
| 503 |
they are sufficient to |
| 504 |
handle small programs. How about the graphics facilities? |
| 505 |
Although \TeX{} has no built-in graphics facilities, it |
| 506 |
allows the placement of characters in arbitrary positions on |
| 507 |
the page. Therefore, complex pictures can be synthesized from elementary |
| 508 |
picture elements treated as characters. Lamport has included such |
| 509 |
a picture drawing environment in his macro package \LaTeX, using |
| 510 |
quarter circles of different sizes and line segments (with and without |
| 511 |
arrow heads) of different slopes as basic elements; see~\cite{LaTeX}. |
| 512 |
These elements are sufficient for drawing trees. |
| 513 |
|
| 514 |
This survey of \TeX's capabilities implies that \TeX{} may be a suitable |
| 515 |
text processing system to implement a tree drawing algorithm directly. |
| 516 |
We base our algorithm on the RT~algorithm, because this algorithm |
| 517 |
gives, aesthetically, the most pleasing results. In the first version |
| 518 |
presented here, we |
| 519 |
restrict ourselves to unary-binary trees, although our method is |
| 520 |
applicable to arbitrary multiway trees. But to take advantage |
| 521 |
of the text processing environment, we expand the algorithm to allow |
| 522 |
labeled nodes. |
| 523 |
|
| 524 |
In contrast to previous tree drawing programs, we feel no necessity to |
| 525 |
position the nodes of a tree on a fixed grid. While this may be |
| 526 |
reasonable for a plotter with a coarse resolution, it is certainly not |
| 527 |
necessary for \TeX, a system that is capable of handling |
| 528 |
arbitrary dimensions |
| 529 |
and producing device \emph{independent} output. |
| 530 |
|
| 531 |
|
| 532 |
\section{A representation method for \TeX{}trees} |
| 533 |
|
| 534 |
The first problem to be solved in implementing our tree drawing algorithm |
| 535 |
is how to choose a good internal representation |
| 536 |
for trees. A straightforward adaptation |
| 537 |
of the implementation by Reingold and Tilford requires, for each node, |
| 538 |
at least: |
| 539 |
% |
| 540 |
\begin{enumerate} |
| 541 |
\item two pointers to the children of the node, |
| 542 |
\item two dimensions for the offset to the left and the right child (these |
| 543 |
may be different once there are labels of different widths to the |
| 544 |
left and right of the nodes), |
| 545 |
\item two dimensions for the $x$- and $y$-coordinates of the final |
| 546 |
position of the nodes, |
| 547 |
\item three or four labels, and |
| 548 |
\item one token to store the geometric shape (circle, square, framed text, etc.) |
| 549 |
of the node. |
| 550 |
\end{enumerate} |
| 551 |
% |
| 552 |
Because these data are used frequently in calculations, they should be |
| 553 |
stored in registers (that's what variables are called in \TeX) |
| 554 |
rather than being recomputed, to obtain |
| 555 |
reasonably fast performance. This gives a total of $10N$ registers for |
| 556 |
a tree with $N$ nodes, which quickly exceeds |
| 557 |
\TeX's limited supply of registers. Therefore, we present a |
| 558 |
modified algorithm hand-tailored to the abilities of \TeX{}. |
| 559 |
We start with the following observation. |
| 560 |
Suppose a unary-binary tree is built bottom-up, using a postorder |
| 561 |
traversal. This can be done by repeating the following three steps in |
| 562 |
an order determined by the tree to be built. |
| 563 |
|
| 564 |
\begin{enumerate} |
| 565 |
\item Create a new subtree consisting of one external node. |
| 566 |
\item Create a new subtree by appending the two subtrees last created |
| 567 |
to a new binary node; see \fig{Construct}. |
| 568 |
\item Create a new subtree by appending the subtree created last as a left, |
| 569 |
right, or unary subtree of a new node; see \fig{Construct}. |
| 570 |
\end{enumerate} |
| 571 |
|
| 572 |
(A pointer to) each subtree that has been |
| 573 |
created in steps 1--3 is pushed onto a stack, and |
| 574 |
steps 2 and 3 remove two trees or one tree, respectively, |
| 575 |
from the stack before the push |
| 576 |
operation is carried out. The tree to be built is |
| 577 |
the tree remaining on the stack. |
| 578 |
|
| 579 |
\begin{Figure} |
| 580 |
\centering |
| 581 |
\begin{Tree} |
| 582 |
\treesymbol{\lvls{2}}% |
| 583 |
\hspace{-\l@stlmoff}\usebox{\l@sttreebox}\hspace{\l@strmoff} |
| 584 |
$+$ |
| 585 |
\treesymbol{\lvls{2}}% |
| 586 |
\hspace{-\l@stlmoff}\usebox{\l@sttreebox}\hspace{\l@strmoff}\quad |
| 587 |
$\Longrightarrow$\quad |
| 588 |
\treesymbol{\lvls{2}}% |
| 589 |
\treesymbol{\lvls{2}}% |
| 590 |
\node{\type{dot}}% |
| 591 |
\hspace{-\l@stlmoff}\raisebox{\vd@st}{\usebox\l@sttreebox}\hspace{\l@strmoff}% |
| 592 |
\end{Tree} |
| 593 |
|
| 594 |
\vskip\baselineskip |
| 595 |
|
| 596 |
\begin{Tree} |
| 597 |
\treesymbol{\lvls{2}}% |
| 598 |
\hspace{-\l@stlmoff}\usebox{\l@sttreebox}\hspace{\l@strmoff}\quad |
| 599 |
$\Longrightarrow$\quad |
| 600 |
\treesymbol{\lvls{2}}% |
| 601 |
\node{\leftonly\type{dot}}% |
| 602 |
\hspace{-\l@stlmoff}\raisebox{\vd@st}{\usebox\l@sttreebox}\hspace{\l@strmoff}% |
| 603 |
\quad or\quad |
| 604 |
\treesymbol{\lvls{2}}% |
| 605 |
\node{\unary\type{dot}}% |
| 606 |
\hspace{-\l@stlmoff}\raisebox{\vd@st}{\usebox\l@sttreebox}\hspace{\l@strmoff}% |
| 607 |
\quad or\quad |
| 608 |
\treesymbol{\lvls{2}}% |
| 609 |
\node{\rightonly\type{dot}}% |
| 610 |
\hspace{-\l@stlmoff}\raisebox{\vd@st}{\usebox\l@sttreebox}\hspace{\l@strmoff}% |
| 611 |
\end{Tree} |
| 612 |
|
| 613 |
\caption{Construction steps 2 and 3} |
| 614 |
\label{Construct} |
| 615 |
\end{Figure} |
| 616 |
|
| 617 |
This tree traversal is performed twice in the RT~algorithm. |
| 618 |
During the first pass, |
| 619 |
at each execution of steps 2 or~3, the relative positions of the |
| 620 |
subtree(s) and of the new node are computed. |
| 621 |
A closer examination of the RT~algorithm reveals that information about the |
| 622 |
subtree's coordinates is not needed during this pass; the contour information |
| 623 |
alone is sufficient. Complete information is only needed in the second |
| 624 |
traversal, when the tree is really drawn. This is where we can use |
| 625 |
a special feature of \TeX{} that allows us to save registers. |
| 626 |
Unlike Pascal, \TeX{} has the capability of |
| 627 |
storing a drawing in a single box register that can be positioned freely in |
| 628 |
later drawings. This means that in our implementation the two passes |
| 629 |
of the original RT~algorithm can be woven into a single pass, |
| 630 |
storing the contour and drawing of each subtree on the stack. |
| 631 |
Although the latter is a complex object, it takes only one of |
| 632 |
\TeX's precious registers. |
| 633 |
|
| 634 |
|
| 635 |
\section{The internal representation} |
| 636 |
|
| 637 |
Given a tree, the corresponding \TeX{}tree is a box containing |
| 638 |
the ``drawing'' of the tree, together with some additional |
| 639 |
information about the contour of the tree. |
| 640 |
The reference point of a \TeX{}tree-box is always in the root of the |
| 641 |
tree. The height, depth, and width of the box of a \TeX{}tree are |
| 642 |
of no importance in this context. |
| 643 |
|
| 644 |
The additional information about the contour of the tree is stored in some |
| 645 |
registers for numbers and dimensions and |
| 646 |
is needed in order to put subtrees together to form a larger tree. |
| 647 |
An array \var{loff} of dimensions contains for each |
| 648 |
level of the tree the horizontal offset between the |
| 649 |
left end of the leftmost node at the current level and the |
| 650 |
left end of the leftmost node at the next level. |
| 651 |
The horizontal offset between the root |
| 652 |
and the leftmost node of the whole tree is hold in \var{lmoff}, and |
| 653 |
the horizontal offset between the root and the leftmost node at |
| 654 |
the bottom level of the tree is hold in \var{lboff}. |
| 655 |
Finally, \var{ltop} holds the distance between the reference point |
| 656 |
of the tree and the leftmost end of the root. |
| 657 |
We use |
| 658 |
\var{roff}, \var{rmoff}, \var{rboff}, and \var{rtop} |
| 659 |
as the corresponding variables for ``left'' replaced by ``right.'' Finally, |
| 660 |
\var{height} holds the height of the tree, and \var{type} holds the |
| 661 |
geometric shape of the root of the tree. \fig{TeXtree} shows an example |
| 662 |
\TeX{}tree, that is a tree drawing and the corresponding additional information. |
| 663 |
|
| 664 |
\begin{Figure} |
| 665 |
\centering |
| 666 |
\begin{Tree} |
| 667 |
\e\ir\ir\e |
| 668 |
\node{\type{dot}\rightonly\rght{\unskip\vrule height.8pt width5pt depth0pt}}% |
| 669 |
\i % A |
| 670 |
\end{Tree} |
| 671 |
\leavevmode |
| 672 |
\stack{-10pt}{\vd@st}{% |
| 673 |
-10pt\\10pt\\10pt\\\var{loff}}% |
| 674 |
\hspace{1em}% |
| 675 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist}% |
| 676 |
\hspace{1em}% |
| 677 |
\stack{-10pt}{\vd@st}{% |
| 678 |
15pt\\5pt\\-10pt\\\var{roff}}% |
| 679 |
|
| 680 |
\vskip\baselineskip\raggedright |
| 681 |
height:~3, type:~dot, ltop:~2pt, rtop:~2pt, lmoff:~-10pt, rmoff:~20pt, lboff:~10pt, |
| 682 |
rboff:~10pt. |
| 683 |
|
| 684 |
\caption{A \TeX{}tree consists of the drawing of the tree and the additional |
| 685 |
information. The width of the dots is 4pt, the minimal separation between |
| 686 |
adjacent nodes is 16pt, making for a distance of 20pt center to center. |
| 687 |
The length of the small rule labeling |
| 688 |
one of the nodes is 5pt. The column left (right) |
| 689 |
of the tree drawing is the array \var{loff} (\var{roff}), |
| 690 |
describing the left (right) contour of the tree. At each level, |
| 691 |
the dimension given is the horizontal |
| 692 |
offset between the border at the current and |
| 693 |
at the next level. The offset between |
| 694 |
the left border of the root node and the leftmost node at level~1 is -10pt, |
| 695 |
the offset between the right border of the root node and the rightmost node at |
| 696 |
level~1 is 15pt, etc.} |
| 697 |
\label{TeXtree} |
| 698 |
\end{Figure} |
| 699 |
|
| 700 |
Given two \TeX{}trees \var{A} and \var{B}, |
| 701 |
how can a new \TeX{}tree \var{C} be built that |
| 702 |
consists of a new root and has \var{A} and \var{B} as subtrees? |
| 703 |
An example is given in \fig{AddInfo}. |
| 704 |
First we determine which tree is higher; this is |
| 705 |
\var{B} in the example. |
| 706 |
Then we have to compute the minimal distance |
| 707 |
between the roots of \var{A} and \var{B}, such that at all levels |
| 708 |
of the trees there is free space of at least \var{minsep} between |
| 709 |
the trees when they are drawn side by side. |
| 710 |
For this purpose we keep track of two values, \var{totsep} and |
| 711 |
\var{currsep}. The variables \var{totsep} and \var{currsep} |
| 712 |
hold the total distance between the roots and the distance |
| 713 |
between the rightmost node of \var{A} and the leftmost node |
| 714 |
of \var{B} at the current level. To calculate |
| 715 |
\var{totsep} and \var{currsep}, we start at level 0 and |
| 716 |
visit each level of the trees until we reach the bottommost level |
| 717 |
of the smaller tree; this is \var{A} in our example. |
| 718 |
|
| 719 |
\begin{Figure} |
| 720 |
\centering |
| 721 |
\begin{Tree} |
| 722 |
\e\ir\ir\e |
| 723 |
\node{\type{dot}\rightonly\rght{\unskip\vrule height.8pt width5pt depth0pt}}% |
| 724 |
\i % A |
| 725 |
\end{Tree} |
| 726 |
\leavevmode |
| 727 |
A: \stack{-10pt}{\vd@st}{% |
| 728 |
-10pt\\10pt\\10pt\\\ \\\var{loff}(\var{A})}% |
| 729 |
\hspace{1em}% |
| 730 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist}% |
| 731 |
\hspace{1em}% |
| 732 |
\stack{-10pt}{\vd@st}{% |
| 733 |
15pt\\5pt\\-10pt\\\ \\\var{roff}(\var{A})}% |
| 734 |
\qquad |
| 735 |
\begin{Tree} |
| 736 |
\e\il\e\i\il\il\ir % B |
| 737 |
\end{Tree} |
| 738 |
\leavevmode |
| 739 |
B: \stack{-10pt}{\vd@st}{% |
| 740 |
10pt\\-10pt\\-10pt\\-10pt\\-10pt\\\ \\\var{loff}(\var{B})}% |
| 741 |
\hspace{1em}% |
| 742 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist}% |
| 743 |
\hspace{1em}% |
| 744 |
\stack{-10pt}{\vd@st}{% |
| 745 |
10pt\\-10pt\\-10pt\\10pt\\-30pt\\\ \\\var{roff}(\var{B})}% |
| 746 |
\\[\figspace] |
| 747 |
\begin{Tree} |
| 748 |
\e\ir\ir\e |
| 749 |
\node{\type{dot}\rightonly\rght{\unskip\vrule height.8pt width5pt depth0pt}}% |
| 750 |
\i % A |
| 751 |
\e\il\e\i\il\il\ir % B |
| 752 |
\i % C |
| 753 |
\end{Tree} |
| 754 |
\leavevmode |
| 755 |
C: \stack{-10pt}{\vd@st}{% |
| 756 |
-20\\-10pt\\% |
| 757 |
\makebox[0pt][r]{\var{loff}(\var{A})$\smash{\left\{\vrule height\vd@st |
| 758 |
depth\vd@st width0pt\right.}$ }% |
| 759 |
10pt\\10pt\\% |
| 760 |
\makebox[0pt][r]{$\longrightarrow$ }% |
| 761 |
10pt\\% |
| 762 |
\makebox[0pt][r]{\raisebox{-.5\vd@st}{\var{loff}(\var{B})$\smash |
| 763 |
{\left\{\vrule height.5\vd@st |
| 764 |
depth.5\vd@st width0pt\right.}$ }}% |
| 765 |
\makebox[0pt][r]{-}10pt\\\ \\\var{loff}(\var{C})}% |
| 766 |
\hspace{1em}% |
| 767 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist}% |
| 768 |
\hspace{1em}% |
| 769 |
\stack{-10pt}{\vd@st}{% |
| 770 |
20pt\\10pt\\-10pt\\-10pt% |
| 771 |
\makebox[0pt][l]{\raisebox{-.5\vd@st}{ |
| 772 |
$\smash{\left\}\vrule height2.5\vd@st |
| 773 |
depth2.5\vd@st width0pt\right.}$\var{roff}(\var{B})}}% |
| 774 |
\\10pt\\-30pt\\\ \\\var{roff}(\var{C})}% |
| 775 |
|
| 776 |
\vspace{\figspace} |
| 777 |
\centering |
| 778 |
\begin{tabular}{|l|r|r|r|} |
| 779 |
\hline |
| 780 |
&\multic{1}{c|}{\var{A}}&\multic{1}{c|}{\var{B}}&\multic{1}{c|}{\var{C}}\\ |
| 781 |
\hline |
| 782 |
height&\multic{1}{c|}{3}& \multic{1}{c|}{5}& \multic{1}{c|}{6}\\ |
| 783 |
type& \multic{1}{c|}{dot}&\multic{1}{c|}{dot}&\multic{1}{c|}{dot}\\ |
| 784 |
ltop& 2pt& 2pt& 2pt\\ |
| 785 |
rtop& 2pt& 2pt& 2pt\\ |
| 786 |
lmoff& -10pt& -30pt& -30pt\\ |
| 787 |
rmoff& 20pt& 10pt& 30pt\\ |
| 788 |
lboff& 10pt& -30pt& -10pt\\ |
| 789 |
rboff& 10pt& -30pt& -10pt\\ |
| 790 |
\hline |
| 791 |
\end{tabular}\qquad |
| 792 |
\begin{tabular}{|c|r|r|} |
| 793 |
\hline |
| 794 |
\multic{1}{|c|}{level}&\multic{1}{c|}{\var{totsep}}& |
| 795 |
\multic{1}{c|}{\var{currsep}}\\ |
| 796 |
\hline |
| 797 |
0&20pt&0/16pt\\ |
| 798 |
1&25pt&11/16pt\\ |
| 799 |
2&40pt&1/16pt\\ |
| 800 |
3&40pt&16pt\\ |
| 801 |
\hline |
| 802 |
\end{tabular} |
| 803 |
\caption{The \TeX{}trees \var{A} and~\var{B} are combined to form the |
| 804 |
larger \TeX{}\-tree~\var{C}. The first table gives the additional |
| 805 |
information of the three \TeX{}trees, |
| 806 |
and the second table gives the |
| 807 |
history of the computation for \var{totsep} and \var{currsep}.} |
| 808 |
\label{AddInfo} |
| 809 |
\end{Figure} |
| 810 |
|
| 811 |
At level 0, the distance between the roots of \var{A} and \var{B} |
| 812 |
should be at least \var{minsep}. Therefore, we set |
| 813 |
$\var{totsep}:=\var{minsep} + \var{rtop}(\var{A}) |
| 814 |
+ \var{ltop}(\var{B})$ and $\var{currsep}:=\var{minsep}$. |
| 815 |
Using $\var{roff}(\var{A})$ and $\var{loff}(\var{B})$, we can |
| 816 |
calculate \var{currsep} for the next level. |
| 817 |
If $\var{currsep} < \var{minsep}$, we have to increase \var{totsep} by |
| 818 |
the difference and update \var{currsep}. This process is |
| 819 |
repeated until we reach the lowest level of \var{A} |
| 820 |
at which point \var{totsep} holds the final distance between the |
| 821 |
nodes of \var{A} and \var{B}, as calculated by the RT~algorithm. |
| 822 |
If the root of \var{C} is a significant node, then the additional space, |
| 823 |
which is 0pt by default, is added to \var{totsep}. |
| 824 |
However, the approach of synthesizing |
| 825 |
drawings from simple graphics characters allows only a finite |
| 826 |
number of orientations for the tree edges; therefore, \var{totsep} |
| 827 |
must be increased slightly to fit the next orientation |
| 828 |
available. |
| 829 |
|
| 830 |
Now we are ready to build the box of \TeX{}tree~\var{C}. |
| 831 |
Simply put \var{A} and~\var{B} side by side, with the reference |
| 832 |
points \var{totsep}~units apart, insert a new node |
| 833 |
above them, and connect the parent and children by edges. |
| 834 |
Next, we compute the additional information |
| 835 |
for \var{C}. This can be done by using the additional information |
| 836 |
for \var{A} and~\var{B}. |
| 837 |
Note that most components of $\var{roff}(\var{C})$ and |
| 838 |
$\var{lroff}(\var{C})$ are the same as in the higher tree, which |
| 839 |
is \var{B} in our case. |
| 840 |
So, if we can avoid moving this information around, |
| 841 |
the number of counters we have to access to update the additional information |
| 842 |
for \var{C} is within a small constant of the height of~\var{A}. |
| 843 |
Hence, we can apply the same argument as |
| 844 |
in~\cite{TidierTrees}, which gives |
| 845 |
us a running time of $\O(N)$ for drawing a tree with N nodes. |
| 846 |
|
| 847 |
We must design the allocation of storage registers for |
| 848 |
the additional information of \TeX{}trees carefully to fulfill the |
| 849 |
following requirement. If a new tree is built from |
| 850 |
two subtrees, the additional information of the new tree |
| 851 |
shares storage with its larger subtree. |
| 852 |
Organizational overhead, that is, |
| 853 |
pointers that keep track of the locations of different parts of additional |
| 854 |
information, must be avoided. |
| 855 |
This means that the additional information |
| 856 |
for one \TeX{}tree should be stored in a sequence |
| 857 |
of consecutive dimension registers |
| 858 |
such that only one pointer for access to the first element |
| 859 |
in this sequence is needed. On the other hand, each parent |
| 860 |
tree is higher and, therefore, needs more storage than its subtrees. |
| 861 |
So we must ensure that there is always enough space in the sequence |
| 862 |
for more information. |
| 863 |
|
| 864 |
The obvious way to fulfill these requirements is to use a stack and to |
| 865 |
allow only the topmost \TeX{}trees of this stack to be |
| 866 |
combined into a larger tree at any time. |
| 867 |
This leads to the following allocation of registers: A contiguous sequence of |
| 868 |
box registers contains the treeboxes of the subtrees in the stack. A |
| 869 |
contiguous sequence of token registers contains the type information for the |
| 870 |
nodes of the subtrees in the stack. For each subtree in the stack, |
| 871 |
a contiguous sequence of dimension registers contains the contour |
| 872 |
information of the subtree. The ordering of these groups of dimension |
| 873 |
registers reflects the ordering of the subtrees in the |
| 874 |
stack. Finally, a contiguous sequence of counter registers contains |
| 875 |
the height and the address of the first dimension register for |
| 876 |
each subtree in the stack. Four address counters store the addresses |
| 877 |
of the last treebox, type information, height, and address of contour |
| 878 |
information. A sketch of the register organization for a stack of \TeX{}trees |
| 879 |
is provided in \fig{Registers}. |
| 880 |
|
| 881 |
\begin{Figure} |
| 882 |
Dimension registers\\ |
| 883 |
\var{lmoff}(1) \var{rmoff}(1) \var{lboff}(1) \var{rboff}(1) \var{ltop}(1) |
| 884 |
\var{rtop}(1)\\ |
| 885 |
\var{loff}($h_1$) \var{roff}($h_1$) \dots\ \var{loff}(1) \var{roff}(1)\\ |
| 886 |
\dots\\ |
| 887 |
\var{lmoff}($n$) \var{rmoff}($n$) \var{lboff}($n$) \var{rboff}($n$) |
| 888 |
\var{ltop}($n$) \var{rtop}($n$)\\ |
| 889 |
\var{loff}($h_n$) \var{roff}($h_n$) \dots\ \var{loff}(1) \var{roff}(1)\\ |
| 890 |
\mbox{}\\ |
| 891 |
Counter registers\\ |
| 892 |
\var{lasttreebox} \var{lasttreeheight} \var{lasttreeinfo} \var{lasttreetype}\\ |
| 893 |
\var{treeheight}(1) \var{diminfo}(1) \dots\ \var{treeheight}($n$) |
| 894 |
\var{diminfo}($n$)\\ |
| 895 |
\mbox{}\\ |
| 896 |
Box registers\\ |
| 897 |
\var{treebox}(1) \dots\ \var{treebox}($n$)\\ |
| 898 |
\mbox{}\\ |
| 899 |
Token registers\\ |
| 900 |
\var{type}(1) \dots\ \var{type}($n$) |
| 901 |
|
| 902 |
\caption{\var{lasttreebox}, \var{lasttreeheight}, \var{lasttreeinfo}, |
| 903 |
\var{lasttreetype} contain pointers to \var{treebox}($n$) |
| 904 |
\var{treeheight}($n$), \var{lmoff}($n$), \var{type}($n$), |
| 905 |
\var{diminfo}($i$) contains a pointer to |
| 906 |
\var{lmoff}($i$). Unused dimension registers are |
| 907 |
allowed between the dimension registers of subsequent trees. The counter |
| 908 |
registers \var{lasttreebox},\ldots,\var{diminfo}($n$) serve as a directory |
| 909 |
mechanism to access the \TeX{}trees on the stack.} |
| 910 |
\label{Registers} |
| 911 |
\end{Figure} |
| 912 |
|
| 913 |
|
| 914 |
When a new node is pushed onto the stack, the treebox, type information, |
| 915 |
height, address of contour information, and contour information are |
| 916 |
stored in the next free registers of the appropriate type, and the |
| 917 |
four address counters are updated accordingly. |
| 918 |
|
| 919 |
When a new tree is formed from the topmost subtrees on the stack, |
| 920 |
the treebox, type information, height, and address of contour information |
| 921 |
of the new tree are sorted in the registers formerly used by the bottommost |
| 922 |
subtree that has occurred in the construction step, |
| 923 |
and the four address registers are |
| 924 |
updated accordingly. This means that this information for the subtrees |
| 925 |
is no longer accessible. The contour information of the new subtree |
| 926 |
is stored in the same registers as the contour information of the larger |
| 927 |
subtree used in the construction, apart from the left and right offset |
| 928 |
of the root to the left and right child, which are stored in the |
| 929 |
following dimension registers. This means that gaps can occur |
| 930 |
between the contour information of subtrees in the |
| 931 |
stack, namely when the right subtree, which is in a higher position in the |
| 932 |
stack, is higher than the left one. To avoid these |
| 933 |
gaps, the user can specify an option \verb.\lefttop. when entering a |
| 934 |
binary node, which makes the topmost tree in the stack the |
| 935 |
left subtree of the node. |
| 936 |
|
| 937 |
This stack concept also has consequences for the design of the user interface |
| 938 |
that is discussed in Section~\ref{Interface}. |
| 939 |
|
| 940 |
\section{Space cost analysis} |
| 941 |
|
| 942 |
Suppose we want to draw a unary-binary tree $T$ of height $h$ having |
| 943 |
$N$ nodes\footnote{The height $h$ and the number of nodes $N$ refer to the |
| 944 |
drawing of the tree. $N$ is the number of circles, squares,~etc., actually |
| 945 |
drawn, and $h$ is the number of levels in the drawing minus 1.}. |
| 946 |
According to our internal representation, |
| 947 |
for each subtree in the stack we need: |
| 948 |
|
| 949 |
\begin{enumerate} |
| 950 |
\item one box register to store the box of the \TeX{}tree; |
| 951 |
\item one token register to store the type of the root of the subtree; |
| 952 |
\item $2h^\prime+6$ dimension registers to store the additional |
| 953 |
information, where $h^\prime$ is the height of the |
| 954 |
subtree; and |
| 955 |
\item three counter registers to store the register numbers of the |
| 956 |
box register, the token register, and the first dimension register above. |
| 957 |
\end{enumerate} |
| 958 |
|
| 959 |
\begin{lemma} |
| 960 |
Let $T$ be a unary-binary tree of height~$h$ and size~$N$; then: |
| 961 |
\begin{enumerate} |
| 962 |
\item at any time, there are at most $h+1$ subtrees of $T$ on the |
| 963 |
stack; and |
| 964 |
\item for each set $\T$ of subtrees of $T$ that are on the stack |
| 965 |
simultaneously we have |
| 966 |
$$\sum_{T^\prime\in \T}({\rm ht}(T^\prime)+1) \le N$$ |
| 967 |
\end{enumerate} |
| 968 |
\end{lemma} |
| 969 |
|
| 970 |
The lemma implies that our implementation |
| 971 |
uses at most $9h+2N$~registers. |
| 972 |
To compare this with the |
| 973 |
$10N$ registers used in the straightforward implementation, |
| 974 |
an estimation of the average height of a tree with $N$ nodes is |
| 975 |
needed. Several results, depending on the type of trees and of the |
| 976 |
randomization model, are cited in \fig{Stat}, which |
| 977 |
compares the number of registers used in a straightforward |
| 978 |
implementation with the average number of registers used in our |
| 979 |
implementation. This table shows clearly the advantage of our |
| 980 |
implementation. |
| 981 |
|
| 982 |
\begin{Figure} |
| 983 |
\centering |
| 984 |
|
| 985 |
\begin{tabular}{|c|c|c|c|c|} |
| 986 |
\hline |
| 987 |
®isters&\multicolumn{3}{c|}{average registers}\\ |
| 988 |
\cline{3-5} |
| 989 |
nodes&(straight-&&unary-binary&binary\\ |
| 990 |
&forward)&binary trees&trees&search trees\\ |
| 991 |
$N$&&($2\sqrt{\pi N}$) \cite{BinaryTrees}& |
| 992 |
($\sqrt{3\pi N}$) ~\cite{BinaryTrees}& |
| 993 |
($4.311\log N$) \cite{BinarySearchTrees}\\ |
| 994 |
\hline |
| 995 |
\ds10 & \ds100 & 120.89 & 107.37 & 109.34 \\ |
| 996 |
\ds20 & \ds200 & 182.68 & 163.56 & 156.23 \\ |
| 997 |
\ds30 & \ds300 & 234.75 & 211.33 & 191.96 \\ |
| 998 |
\ds40 & \ds400 & 281.78 & 254.75 & 223.12 \\ |
| 999 |
\ds50 & \ds500 & 325.60 & 295.37 & 251.78 \\ |
| 1000 |
\ds60 & \ds600 & 367.13 & 334.02 & 278.86 \\ |
| 1001 |
\ds70 & \ds700 & 406.93 & 371.17 & 304.84 \\ |
| 1002 |
\ds80 & \ds800 & 445.36 & 407.13 & 330.02 \\ |
| 1003 |
\ds90 & \ds900 & 482.67 & 442.12 & 354.59 \\ |
| 1004 |
100 & 1000 & 519.04 & 476.30 & 378.68 \\ |
| 1005 |
\hline |
| 1006 |
\end{tabular} |
| 1007 |
|
| 1008 |
\caption{The numbers of registers used by a straightforward implementation |
| 1009 |
(second column) and by our modified implementation (third to fifth column) |
| 1010 |
of the RT~algorithm are |
| 1011 |
given for different types of trees and randomization models. |
| 1012 |
The formulas in parentheses indicate the average height of the respective |
| 1013 |
classes of trees.} |
| 1014 |
\label{Stat} |
| 1015 |
\end{Figure} |
| 1016 |
|
| 1017 |
|
| 1018 |
\section{The user interface}\label{Interface} |
| 1019 |
|
| 1020 |
The user interface of \TreeTeX{} has been designed in the spirit of |
| 1021 |
the thorough separation of the logical description of document components |
| 1022 |
and their layout; see~\cite{DocumentFormatting,GML}. This concept |
| 1023 |
ensures both uniformity and flexibility of document layout and frees |
| 1024 |
authors from layout problems that have nothing to do with the |
| 1025 |
substance of their work. For some powerful implementations and projects |
| 1026 |
see \cite{Tables,Karlsruhe,LaTeX,Grif,Scribe}. |
| 1027 |
|
| 1028 |
The description of a tree consists of a description of its nodes |
| 1029 |
in postorder. Each description of a node, in turn, has to specify |
| 1030 |
the outdegree, the geometric shape and the labels of the node. |
| 1031 |
Defaults are provided for all specifications, |
| 1032 |
thereby allowing the user to omit many definitions |
| 1033 |
if the defaults match what he or she wants. |
| 1034 |
|
| 1035 |
A separate style command defines layout parameters for tree drawings |
| 1036 |
that are valid for all trees of a document. |
| 1037 |
Layout parameters include the font to be used for labels, the diameter |
| 1038 |
of circle nodes, the vertical distance between two subsequent levels |
| 1039 |
of the tree, and the minimal horizontal distance between nodes. |
| 1040 |
|
| 1041 |
Standard versions of \TeX{} provide only a limited number of |
| 1042 |
font and circle sizes. Hence, the user of the style command must make |
| 1043 |
sure that the specified sizes can be realized. This is especially |
| 1044 |
cumbersome when everything has to be magnified for later reproduction |
| 1045 |
with reduction. But the style variables can be made parametric for |
| 1046 |
installations that provide scalable fonts and replace \LaTeX{}'s |
| 1047 |
circle- and line-drawing commands with routines that provide arbitrary |
| 1048 |
diameters and slopes. |
| 1049 |
|
| 1050 |
Three examples of tree descriptions are given in |
| 1051 |
Figures~\ref{firstex}--\ref{lastex}. |
| 1052 |
A more detailed description of the user interface is |
| 1053 |
given in~\cite{Exeter}. |
| 1054 |
|
| 1055 |
\section{Conclusions} |
| 1056 |
|
| 1057 |
We hope that, by now, we have convinced the reader of the main advantages |
| 1058 |
of \TreeTeX{}: It integrates graphics and text; it is portable to all |
| 1059 |
sites running \TeX{}; |
| 1060 |
and it is easy to use for the author, because it derives the drawing |
| 1061 |
of a tree from a purely structural description. But our decision to |
| 1062 |
implement \TreeTeX{} as a \TeX{} macro package has also some |
| 1063 |
drawbacks, both for the programmer and for the user of the system. |
| 1064 |
|
| 1065 |
>From the programmer's point of view, \TeX{}'s macro language is |
| 1066 |
a low level programming language. Hence, maintaining and extending |
| 1067 |
the package is a more tedious task than it would be if we had used |
| 1068 |
a higher level language with better support for modularization. |
| 1069 |
|
| 1070 |
>From the author's point of view, \TreeTeX{}'s limitations lie in |
| 1071 |
speed, size of trees, and graphical primitives. |
| 1072 |
Typesetting all the trees in this article takes about two~minutes on |
| 1073 |
a VAX~750, and typesetting a complete binary tree with 63~internal |
| 1074 |
and 64~external nodes takes about one~minute on the same machine. |
| 1075 |
The size of the trees is limited by three factors, namely, |
| 1076 |
the number of registers, the complexity of the nested boxes that |
| 1077 |
contain the drawing of a tree, and the limited number of slopes |
| 1078 |
that are available for the edges, the latter being the most severe |
| 1079 |
problem at present. Hence, the main area of application for |
| 1080 |
\TreeTeX{} is modest use such as in textbooks; displaying |
| 1081 |
large amounts of statistical data, for example, is out of the question. |
| 1082 |
|
| 1083 |
Currently edges and circular nodes are drawn from \LaTeX{}'s set of |
| 1084 |
predefined graphical characters. Hence, \TreeTeX{} cannot draw |
| 1085 |
arbitrarily wide trees or large circular nodes. We consider |
| 1086 |
this restriction, however, to be a temporary one, since a committee inside |
| 1087 |
the \TeX{} Users Group is working on standard graphic |
| 1088 |
extensions to \TeX{} that will remove these limitations. |
| 1089 |
|
| 1090 |
As to further developments of \TreeTeX{}, it would be desirable to |
| 1091 |
draw larger classes of trees, for example multiway trees, and to allow |
| 1092 |
labels not only for nodes, but also for edges and whole subtrees. |
| 1093 |
|
| 1094 |
|
| 1095 |
\Treestyle{\vdist{60pt}} |
| 1096 |
\dummyhalfcenterdim@n=10pt |
| 1097 |
|
| 1098 |
\begin{Figure} |
| 1099 |
\centering |
| 1100 |
\begin{Tree} |
| 1101 |
\node{\external\bnth{first}\cntr{1}\lft{Beeton}} |
| 1102 |
\node{\external\cntr{3}\rght{Kellermann}} |
| 1103 |
\node{\cntr{2}\lft{Carnes}} |
| 1104 |
\node{\external\cntr{6}\lft{Plass}} |
| 1105 |
\node{\external\bnth{last}\cntr{8}\rght{Tobin}} |
| 1106 |
\node{\cntr{7}\rght{Spivak}} |
| 1107 |
\node{\leftonly\cntr{5}\rght{Lamport}} |
| 1108 |
\node{\cntr{4}\rght{Knuth}} |
| 1109 |
\end{Tree} |
| 1110 |
|
| 1111 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist}\ |
| 1112 |
|
| 1113 |
\begin{verbatim} |
| 1114 |
\begin{Tree} |
| 1115 |
\node{\external\bnth{first}\cntr{1}\lft{Beeton}} |
| 1116 |
\node{\external\cntr{3}\rght{Kellermann}} |
| 1117 |
\node{\cntr{2}\lft{Carnes}} |
| 1118 |
\node{\external\cntr{6}\lft{Plass}} |
| 1119 |
\node{\external\bnth{last}\cntr{8}\rght{Tobin}} |
| 1120 |
\node{\cntr{7}\rght{Spivak}} |
| 1121 |
\node{\leftonly\cntr{5}\rght{Lamport}} |
| 1122 |
\node{\cntr{4}\rght{Knuth}} |
| 1123 |
\end{Tree} |
| 1124 |
|
| 1125 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist} |
| 1126 |
\end{verbatim} |
| 1127 |
|
| 1128 |
\caption{This is an example of a tree that includes labels.} |
| 1129 |
\label{firstex} |
| 1130 |
\end{Figure} |
| 1131 |
|
| 1132 |
\begin{Figure} |
| 1133 |
\centering |
| 1134 |
\begin{Tree} |
| 1135 |
\node{\external\type{frame}\bnth{first}\cntr{Beeton}} |
| 1136 |
\node{\external\type{frame}\cntr{Kellermann}} |
| 1137 |
\node{\type{frame}\cntr{Carnes}} |
| 1138 |
\node{\external\type{frame}\cntr{Plass}} |
| 1139 |
\node{\external\type{frame}\bnth{last}\cntr{Tobin}} |
| 1140 |
\node{\type{frame}\cntr{Spivak}} |
| 1141 |
\node{\leftonly\type{frame}\cntr{Lamport}} |
| 1142 |
\node{\type{frame}\cntr{Knuth}} |
| 1143 |
\end{Tree} |
| 1144 |
|
| 1145 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist}\ |
| 1146 |
|
| 1147 |
\begin{verbatim} |
| 1148 |
\begin{Tree} |
| 1149 |
\node{\external\type{frame}\bnth{first}\cntr{Beeton}} |
| 1150 |
\node{\external\type{frame}\cntr{Kellermann}} |
| 1151 |
\node{\type{frame}\cntr{Carnes}} |
| 1152 |
\node{\external\type{frame}\cntr{Plass}} |
| 1153 |
\node{\external\type{frame}\bnth{last}\cntr{Tobin}} |
| 1154 |
\node{\type{frame}\cntr{Spivak}} |
| 1155 |
\node{\leftonly\type{frame}\cntr{Lamport}} |
| 1156 |
\node{\type{frame}\cntr{Knuth}} |
| 1157 |
\end{Tree} |
| 1158 |
|
| 1159 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist} |
| 1160 |
\end{verbatim} |
| 1161 |
\caption{This is an example of a tree with framed center labels.} |
| 1162 |
\end{Figure} |
| 1163 |
|
| 1164 |
\begin{Figure} |
| 1165 |
\Treestyle{\treefonts{\small\it}\nodesize{16pt}\vdist{40pt}\minsep{16pt}} |
| 1166 |
\centering |
| 1167 |
\begin{Tree} |
| 1168 |
\node{\external\bnth{first}\cntr{1}\lft{Beeton}} |
| 1169 |
\node{\external\cntr{3}\rght{Kellermann}} |
| 1170 |
\node{\cntr{2}\lft{Carnes}} |
| 1171 |
\node{\external\cntr{6}\lft{Plass}} |
| 1172 |
\node{\external\bnth{last}\cntr{8}\rght{Tobin}} |
| 1173 |
\node{\cntr{7}\rght{Spivak}} |
| 1174 |
\node{\leftonly\cntr{5}\rght{Lamport}} |
| 1175 |
\node{\cntr{4}\rght{Knuth}} |
| 1176 |
\end{Tree} |
| 1177 |
|
| 1178 |
\hspace{\leftdist}\usebox{\TeXTree}\hspace{\rightdist}\ |
| 1179 |
|
| 1180 |
\caption{This tree was produced from the same logical description as in |
| 1181 |
Figure~\ref{firstex}, but with different style parameters} |
| 1182 |
\label{lastex} |
| 1183 |
\end{Figure} |
| 1184 |
|
| 1185 |
\clearpage |
| 1186 |
\bibliography{trees} |
| 1187 |
\end{document} |
| 1188 |
|
| 1189 |
|