summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/pst-tvz/pst-tvz-doc.tex
blob: 5c17402e300e72926d94837be446d6e3dc4b6778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
%% $Id: pst-tvz-doc.tex 524 2011-06-14 15:19:21Z herbert $
\documentclass[11pt,english,BCOR10mm,DIV12,bibliography=totoc,parskip=false,smallheadings
    headexclude,footexclude,oneside]{pst-doc}
\usepackage[utf8]{inputenc}
\usepackage{pst-tvz}
\let\pstTreeFV\fileversion
\lstset{pos=t,language=PSTricks,
    morekeywords={psGammaDist,psChiIIDist,psTDist,psFDist,psBetaDist,psPlotImpl},basicstyle=\footnotesize\ttfamily}
%
\def\bgImage{%
  \psTree[radius=2pt,nodesep=3pt]
    \TC* \\
    \psTree
      \TC* \\
      \TC* \TC* \\
      \TC*
    \endpsTree
    \psTree
      \TC* \TC* \\
      \TC* \\
      \TC* \TC* \TC*
    \endpsTree
  \endpsTree}
%
\begin{document}

\title{A recursive alignment algorithm -- \texttt{pst-tvz}}
\subtitle{Trees; v.\pstTreeFV}
\author{Timothy Van Zandt\\Herbert Vo\ss}
%\docauthor{Timothy Van Zandt\\Herbert Vo\ss}
\date{\today}
\maketitle
\let\titlepafe\relax
\tableofcontents

\clearpage% 

\part{Using the package}
\begin{abstract}
The node and node connections are perfect tools for making trees, but
positioning the nodes using \Lcs{rput} would be rather tedious,
unless you have a computer program that generates the coordinates.

The files \nxLPack{pst-tvz.tex}/\nxLPack{pst-tvz.sty} contains a high-level interface for
making trees.

It should be noted that the correct result is not guaranteed with every \Lprog{dvips} driver.
This package was written for Rokicki's\index{Rokicki} 
\Lprog{dvips} programme, which is practically part of every \TeX{}
distribution.
\end{abstract}

\vfill
thanks to:
Olivier Guibé;
\clearpage
\section{Overview}


The tree commands are
\begin{BDef}
\Lcs{pstree}\Largb{<root>}\Largb{<successors>}
\end{BDef}

\begin{BDef}
\begin{tabular}{@{}l@{\kern30pt}l}
\TeX\ version & \LaTeX\ version\\
\Lcs{psTree}\Largb{<root>}\qquad & \LBEG{psTree}\Largb{root}\\
\qquad<successors>\DBS     & \qquad<successors> \DBS\\  
\qquad<successors>\DBS     & \qquad<successors> \DBS\\  
\qquad\ldots               & \qquad\ldots\\
\Lcs{endpsTree}            & \LEND{psTree} 
\end{tabular}
\end{BDef}


These do the same thing, but just have different syntax. \Lcs{psTree} is the ``long'' version.
These macros make a box that encloses all the nodes, and whose baseline passes
through the center of the root.
Most of the nodes has a variant for use within a tree and are called tree nodes (see Section~\ref{treenodes}).

Trees and tree nodes are called \emph{\Index{tree objects}}. The \Larg{root} of a tree
should be a single tree object, and the \Larg{successors} should be one or more
tree objects. Here is an example with only nodes:
\begin{LTXexample}[pos=l]
  \pstree[radius=3pt]{\Toval{root}}{\TC* \TC* \TC* \TC*}
\end{LTXexample}
There is no difference between a terminal node and a root node, other than
their position in the \Lcs{pstree}\Largb{} command.
  
Here is an example where a tree is included in the list of successors, and
hence becomes \Index{subtree}:
\begin{LTXexample}[pos=l]
  \pstree[radius=3pt]{\Tp}{%
    \TC*
    \pstree{\TC}{\TC* \TC*}
    \TC*}
\end{LTXexample}





\section{Tree Nodes}\label{treenodes}

In each case, the name of the tree node is
formed by omitting "`node"' from the end of the name and adding "T" at the
beginning. For example, \Lcs{psovalnode} becomes \Lcs{Toval}. Here is the list of such
tree nodes:
\begin{BDef}
\LcsStar{Tp}\OptArgs\\
\LcsStar{Tc}\OptArgs\Largb{dim}\\
\LcsStar{TC}\OptArgs\\
\LcsStar{Tf}\OptArgs\\
\LcsStar{Tdot}\OptArgs\\
\LcsStar{Tr}\OptArgs\Largb{stuff}\\
\LcsStar{TR}\OptArgs\Largb{stuff}\\
\LcsStar{Tcircle}\OptArgs\Largb{stuff}\\
\LcsStar{TCircle}\OptArgs\Largb{stuff}\\
\LcsStar{Toval}\OptArgs\Largb{stuff}\\
\LcsStar{Tdia}\OptArgs\Largb{stuff}\\
\LcsStar{Ttri}\OptArgs\Largb{stuff}
\end{BDef}

The syntax of a tree node is the same as of its corresponding ``normal'' node,
except that:
\begin{compactitem}
  \item There is always an optional argument for setting graphics parameters,
  even if the original node did not have one;
  \item There is no argument for specifying the name of the node;
  \item There is never a coordinate argument for positioning the node; and
  \item To set the reference point with \Lcs{Tr}, set the \Lkeyword{ref} parameter.
\end{compactitem}

Figure~\ref{allnodes} gives a reminder of what the nodes look like.



\begin{figure}[!htb]
\begin{LTXexample}
\small\psset{armB=1cm, levelsep=3cm, treesep=-3mm, 
             angleB=-90, angleA=90, nodesepA=3pt, nodesepB=0}
\def\psedge#1#2{\ncangle{#2}{#1}}

\psTree[treenodesize=2.5cm]{\Toval{Tree nodes}}     \\
  \Tp~{\tt\string\Tp}  \Tc{.5}~{\tt\string\Tc}  \TC~{\tt\string\TC}
  \psTree[levelsep=4cm,armB=2cm]{\Tp[edge=\ncline]} \\
    \Tcircle{\tt\string\Tcircle}              \Tdot~{\tt\string\Tdot}
    \TCircle[radius=1.2]{\tt\string\TCircle}  \Tn[name=Tn]\uput[0](Tn){\tt\string\Tn}
    \Toval{\tt\string\Toval}                  \Ttri{\tt\string\Ttri}
    \Tdia{\tt\string\Tdia}
  \endpsTree%
  \Tf~{\tt\string\Tf}  \Tr{\tt\string\Tr}  \TR{\tt\string\TR}
\endpsTree
\end{LTXexample}
\caption{The tree nodes.}\label{allnodes}
\end{figure}

The difference between \Lcs{Tr} and \Lcs{TR} (variants of \Lcs{rnode} and \Lcs{Rnode},
respectively) is important with trees. Usually, you want to use \Lcs{TR} with
vertical trees because the baselines of the text in the nodes line up
horizontally. For example:


\begin{LTXexample}[pos=l]
$ \pstree[nodesepB=3pt]{\Tcircle{X}}{%
     \TR{\tilde{\tilde{X}}}
     \TR{x}
     \TR{y}} $
\end{LTXexample}

Compare with this example, which uses \Lcs{Tr}:

\begin{LTXexample}[pos=l]
$ \pstree[nodesepB=3pt]{\Tcircle{X}}{%
     \Tr{\tilde{\tilde{X}}}
     \Tr{x}
     \Tr{y}} $
\end{LTXexample}


There is also a null tree node: 
\begin{BDef}
\Lcs{Tn}
\end{BDef}
It is meant to be just a place holder. Look at the tree in Figure
page~\pageref{allnodes}. The bottom row has a node missing in the middle.
\Lcs{Tn}\Largb{} was used for this missing node. 

There is also a special tree node that doesn't have a ``normal'' version and
that can't be used as the root node of a whole tree:
\begin{BDef}
\LcsStar{Tfan}\OptArgs 
\end{BDef}
This draws a triangle whose base is \Lkeyword{fansize} %=\makeatletter\psk@fansize\makeatother
and whose opposite corner is the predecessor node, adjusted by the value of
\Lkeyword{nodesepA} and \Lkeyword{offsetA}.
For example:

\begin{LTXexample}[pos=l]
\pstree[dotstyle=oplus,dotsize=8pt,nodesep=2pt]{\Tcircle{11}}{%
  \Tdot
  \pstree{\Tfan}{\Tdot}
  \pstree{\Tdot}{\Tfan[linestyle=dashed]}}
\end{LTXexample}


\section{Tree orientation}

Trees can grow down, up, right or left, depending on the \Lkeyword{treemode=}
\Lkeyval{D}, \Lkeyval{U}, \Lkeyval{R}, or \Lkeyval{L} parameter.

Here is what the previous example looks like when it grows to the right:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \pstree[dotstyle=oplus,dotsize=8pt,
    nodesep=2pt,treemode=R]
    {\Tcircle{11}}{%
      \Tdot
      \pstree{\Tfan}{\Tdot}
      \pstree{\Tdot}{\Tfan[linestyle=dashed]}}
\end{LTXexample}


You can change the \Lkeyword{treemode} in the middle of the tree.
For example, here is a tree that grows up, and that has a subtree which grows
to the left:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \footnotesize
  \pstree[treemode=U,dotstyle=otimes,dotsize=8pt,nodesep=2pt]
    {\Tdot}{%
      \pstree[treemode=L]{\Tdot}{\Tcircle{1} \Tcircle{2}}
      \pstree{\Tdot}{\Tcircle{3} \Tcircle{4}}}
\end{LTXexample}

Since you can change a tree's orientation, it can make sense to include a tree
(<treeB>) as a root node (of <treeA>). This makes a single logical tree, whose
root is the root of <treeB>, and that has successors going off in different
directions, depending on whether they appear as a successor to <treeA> or to
<treeB>.

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \pstree{\pstree[treemode=L]{\Tcircle{root}}{\Tr{B}}}{%
    \Tr{A1}
    \Tr{A2}}
\end{LTXexample}




%%DG: to do

On a semi-related theme, note that any node that creates an LR-box can contain
a tree. However, nested trees of this kind are not related in any way to the
rest of the tree. Here is an example:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \psTree{\Tcircle{\pstree[treesep=0.4,levelsep=0.6,
                    nodesepB=-6pt]{\Tdot}{%
              \TR{a} \TR{b}}}}\\
    \TC
    \TC
  \endpsTree
\end{LTXexample}

When the tree grows up or down, the successors are lined up from left to right
in the order they appear in \Lcs{pstree}. When the tree grows to the left or
right, the successors are lined up from top to bottom. As an afterthought, you
might want to flip the order of the nodes. The keyword \Lkeyword{treeflip}=\true/\false
let's you do this. For example:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
\footnotesize
\pstree[treemode=U,dotstyle=otimes,dotsize=8pt,
  nodesep=2pt,treeflip=true]{\Tdot}{%
    \pstree[treemode=R]{\Tdot}{\Tcircle{1} \Tcircle{2}}
    \pstree{\Tdot}{\Tcircle{3} \Tcircle{4}}}
\end{LTXexample}
Note that I still have to go back and change the \Lkeyword{treemode} of the subtree
that used to grow to the left.


\section{The distance between successors}

The distance between successors is set by the key \Lkeyword{treesep}.
The rest of this section describes ways to fine-tune the spacing between
successors.
You can change the method for calculating the distance between subtrees by
setting the  \Lkeyword{treefit}=\Lkeyval{tight}/\Lkeyval{loose}
parameter. Here are the two methods:
\begin{compactdesc}
  \item[\Lkeyval{tight}] When \Lkeyset{treefit=tight}, which is the default, \Lkeyword{treesep} is
  the minimum distance between each of the levels of the subtrees.
  \item[\Lkeyval{loose}] When \Lkeyset{treefit=loose}, \Lkeyword{treesep} is the distance between the
  subtrees' bounding boxes. Except when you have large intermediate nodes, the
  effect is that the horizontal distance (or vertical distance, for horizontal
  trees) between all the terminal nodes is the same (even when they are on
  different levels).\footnote{%
  When all the terminal nodes are on the same level, and the intermediate
  nodes are not wider than the base of their corresponding \Index{subtree}s, then
  there is no difference between the two methods.}
\end{compactdesc}

Compare:
\begin{center}
\tabcolsep=1cm
\begin{tabular}{cc}
  \psset{radius=2pt}
  \pstree{\TC*}{%
    \TC
    \pstree{\TC*}{%
      \pstree{\Tc{3pt}}{\TC \TC}
      \TC*}}
  &
  \psset{radius=2pt}
  \pstree[treefit=loose]{\TC*}{%
    \TC
    \pstree{\TC*}{%
      \pstree{\Tc{3pt}}{\TC \TC}
      \TC*}}
\end{tabular}
\end{center}

With \Lkeyset{treefit=loose}, trees take up more space, but sometimes the structure
of the tree is emphasized.



%Another (orthogonal) way to highlight the structure of the tree is by setting
%\begin{Ex}
%  \Par{xtreesep=dim}
%\end{Ex}
%to a positive value. The effect is that adjacent nodes with different parents
%are farther apart than adjacent nodes with the same parent.\footnote{%
%When \p{treefit=tight}, the minimum distance between levels other than the top
%of the subtrees is increased by \p{xtreesep}. When \p{treefit=loose}, the
%minimum distance between subtrees is increased by \p{xtreesep}}.

%This would have no effect in the previous two examples, but compare the
%spacing between the two subtrees in
%\begin{LTXexample}
%  \psTree[radius=2pt,levelsep=1.5,xtreesep=.25cm] \TC* \\
%    \pstree{\TC*}{\TC* \TC*}
%    \pstree{\TC*}{\TC* \TC*}
%  \endpsTree
%\end{LTXexample}
%with the spacing in
%\begin{LTXexample}
%  \psTree[radius=2pt,levelsep=1.5] \TC* \\
%    \pstree{\TC*}{\TC* \TC*}
%    \pstree{\TC*}{\TC* \TC*}
%  \endpsTree
%\end{LTXexample}

Sometimes you want the spacing between the centers of the nodes to be regular
even though the nodes have different sizes. If you set \Lkeyword{treenodesize}
to a non-negative value, then PSTricks sets the width (or height+depth for
vertical trees) to \Lkeyword{treenodesize}, \emph{for the purpose of calculating the
distance between successors}.


For example, ternary trees look nice when they are symmetric, as in the
following example:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \pstree[nodesepB=-8pt,treenodesize=.85]{\Tc{3pt}}{%
    \TR{$x=y$}
    \TR{$x_1=y_1$}
    \TR{$x_{11}=y_{11}$}}%$
\end{LTXexample}

Compare with this example, where the spacing varies with the size of the
nodes:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \pstree[nodesepB=-8pt]{\Tc{3pt}}{%
    \TR{$x=y$}
    \TR{$x_1=y_1$}
    \TR{$x_{11}=y_{11}$}}%$
\end{LTXexample}

Finally, if all else fails, you can adjust the distance between two successors 
by inserting \Lcs{tspace}\Largb{length} between them:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
\psTree{\Tc{3pt}}\\
  \Tdia{foo}
%  \tspace{-0.5}
  \Toval{and}
  \Ttri{bar}
\endpsTree
\end{LTXexample}




\section{Spacing between the root and successors}

The distance between the center lines of the tree levels is \Lkeyword{levelsep}.
If you want the spacing between levels to vary with the size of the levels,
use the * convention. Then \Lkeyword{levelsep} is the distance between the bottom of
one level and the top of the next level (or between the sides of the two
levels, for horizontal trees).

Note: PSTricks has to write some information to your \Lext{aux} file if using
\LaTeX, or to \Lcs{jobname}.pst otherwise, in order to calculate the spacing.
You have to run your input file a few times before PSTricks gets the spacing
right.



%%DG: to do
%You are most likely to want to set \p{varlevelsep} to "true" in horizontal
trees. Compare the following example:

\begin{LTXexample}
\def\psedge#1#2{\ncdiagg[nodesep=3pt,angleA=180,armA=0]{#2}{#1}}
\pstree[treemode=R,varlevelsep]{\Tr{George Alexander Kopf VII}}{%
  \pstree{\Tr{Barry Santos}}{\Tr{James Kyle} \Tr{Ann Ada}}
  \pstree{\Tr{Terri Maloney}}{\Tr{Uwe Kopf} \Tr{Vera Kan}}}
\end{LTXexample}

with this one, were the spacing between levels is fixed:

\begin{LTXexample}
  \def\psedge#1#2{\ncdiagg[nodesep=3pt,angleA=180,armA=0]{#2}{#1}}
  \pstree[treemode=R,levelsep=3cm]{\Tr{George Alexander Kopf VII}}{%
      \pstree{\Tr{Barry Santos}}{\Tr{James Kyle} \Tr{Ann Ada}}
      \pstree{\Tr{Terri Maloney}}{\Tr{Uwe Kopf} \Tr{Vera Kan}}}
\end{LTXexample}

%The center of the root node of a tree is positioned above the midpoint between
%the centers of the two outer successors. If you want the root to be positioned
%drectly above one of the successors, put the command
%  \Mac  \treecenter
%right \emph{after} that successor. For example:
%\begin{LTXexample}
%  \def\myfan#1{\Tfan[fillstyle=solid,fillcolor=#1]\treecenter}%
%    \pstree{\Tcircle{$x_2$}}{%
%      \pstree{\Tcircle{$x_1$}}{%
%        \pstree{\Tcircle{$x_0$}}{\myfan{black}}
%        \myfan{gray}}
%      \myfan{white}}
%\end{LTXexample}

%Here is another interesting example:
%\begin{example}
%  \def\psedge{\ncangle[angleA=0,angleB=90]}
%  \pstree[treesep=10pt]{\Tcircle[name=after]{$x_0$}}{%
%    \Tfan[fillstyle=solid,fillcolor=black]
%    \treecenter
%    \pstree{\Tcircle{$x_1$}}{\Tfan[fillstyle=solid,fillcolor=darkgray]}
%    \pstree{\Tcircle{$x_2$}}{\Tfan[fillstyle=solid,fillcolor=gray]}
%    \pstree{\Tcircle{$x_3$}}{\Tfan[fillstyle=solid,fillcolor=lightgray]}
%    \pstree{\Tcircle{$x_4$}}{\Tfan[fillstyle=solid,fillcolor=white]}}
%\end{example}


\section{Edges}

Right after you use a tree node command, \Lcs{pssucc} is equal to the name of the
node, and \Lcs{pspred} is equal to the name of the node's predecessor. Therefore,
you can draw a line between the node and its predecessor by inserting, for
example,

\begin{lstlisting}[style=syntax]
\ncline{\pspred}{\pssucc}
\end{lstlisting}

To save you the trouble of doing this for every node, each tree node executes
\begin{lstlisting}[style=syntax]
  \psedge{\pspred}{\pssucc}
\end{lstlisting}
The default definition of \Lcs{psedge} is \Lcs{ncline}, but you can redefine it as
you please with \Lcs{def} or \LaTeX's \Lcs{renewcommand}.

For example, here I use \Lcs{ncdiag}, with \Lkeyword{armA}=0, to get all the node
connections to emanate from the same point in the predecessor. \LaTeX{} users can instead type:
\begin{lstlisting}[style=syntax]
\renewcommand{\psedge}{\ncdiag[armA=0,angleB=180,armB=1cm]}
\end{lstlisting}


\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \def\psedge{\ncdiag[armA=0,angleB=180,armB=1cm]}
  \pstree[treemode=R,levelsep=3.5cm,framesep=2pt]{\Tc{6pt}}{%
      \small \Tcircle{K} \Tcircle{L} \Tcircle{M} \Tcircle{N}}
\end{LTXexample}

Here is an example with \Lcs{ncdiagg}. Note the use of a negative \Lkeyword{armA} value
so that the corners of the edges are vertically aligned, even though the nodes
have different sizes:

\begin{LTXexample}
$
\def\psedge#1#2{\ncdiagg[angleA=180,armA=1cm,nodesep=4pt]{#2}{#1}}
% Or: \renewcommand{\psedge}[2]{ ... }
\pstree[treemode=R, levelsep=5cm]{\Tc{3pt}}{%
  \Tr{z_1\leq y}  \Tr{z_1<y\leq z_2}   \Tr{z_2<y\leq x}   \Tr{x<y}
}
$
\end{LTXexample}

Another way to define \Lcs{psedge}\Largb{} is with the \Lkeyword{edge}
parameter. Be sure to enclose the value in braces "{}" if it contains commas
or other parameter delimiters. This gets messy if your command is long, and
you can't use arguments like in the preceding example, but for simple changes
it is useful. For example, if I want to switch between a few node connections
frequently, I might define a command for each node connection, and then use
the \Lkeyword{edge} parameter.

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \def\dedge{\ncline[linestyle=dashed]}
  \pstree[treemode=U,radius=2pt]{\Tc{3pt}}{%
    \TC*[edge=\dedge]
    \pstree{\Tc{3pt}}{\TC*[edge=\dedge] \TC*}
    \TC*}
\end{LTXexample}

You can also set \Lkeyset{edge=none} to suppress the node connection.

If you want to draw a node connection between two nodes that are not direct
predecessor and successor, you have to give the nodes a name that you can
refer to, using the \Lkeyword{name} parameter. For example, here I connect two nodes
on the same level:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \pstree[nodesep=3pt,radius=2pt]{\Toval{nature}}{%
    \pstree{\Tc[name=top]{3pt}}{\TC* \TC*}
    \pstree{\Tc[name=bot]{3pt}}{\TC* \TC*}}
   \ncline[linestyle=dashed]{top}{bot}
\end{LTXexample}

We conclude with the more examples.

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \def\psedge{\nccurve[angleB=180, nodesepB=3pt]}
  \pstree[treemode=R, treesep=1.5, levelsep=3.5]%
    {\Toval{root}}{\Tr{X} \Tr{Y} \Tr{Z}}
\end{LTXexample}

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \pstree[nodesepB=3pt, arrows=->, xbbl=15pt,
    xbbr=15pt, levelsep=2.5cm]{\Tdia{root}}{%
    $
    \TR[edge={\ncbar[angle=180]}]{x}
    \TR{y}
    \TR[edge=\ncbar]{z}
    $}
\end{LTXexample}

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \psset{armB=1cm, levelsep=3cm, treesep=1cm,
    angleB=-90, angleA=90, arrows=<-, nodesepA=3pt}
  \def\psedge#1#2{\ncangle{#2}{#1}}
  \pstree[radius=2pt]{\Ttri{root}}{\TC* \TC* \TC* \TC*}
\end{LTXexample}






\section{Edge and node labels}

Right after a node, an edge has typically been drawn, and you can attach
labels using \Lcs{ncput}, \Lcs{tlput}, etc.
With \Lcs{tlput}, \Lcs{trput}, \Lcs{taput}, and \Lcs{tbput}, you can align the labels
vertically or horizontally, just like the nodes. This can look nice, at least
if the slopes of the node connections are not too different.

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \pstree[radius=2pt]{\Tp}{%
    \psset{tpos=.6}
    \TC* \tlput{k}
    \pstree{\Tc{3pt} \tlput[labelsep=3pt]{r}}{%
      \TC* \tlput{j}
      \TC* \trput{i}}
    \TC* \trput{m}}
\end{LTXexample}

Within trees, the \Lkeyword{tpos} parameter measures this distance from the
predecessor to the successor, whatever the orientation of the true.
(Outside of trees it measures the distance from the top to bottom or left to
right nodes.)

PSTricks also sets \Lkeyset{shortput=tab} within trees. This is a special
\Lkeyword{shortput} option that should not be used outside of trees. It implements
the following abbreviations, which depend of the orientation of the true:

\begin{center}
\begin{tabular}{ccc}
 & \multicolumn{2}{c}{Short for:}\\
  \emph{Char.} & \emph{Vert.} & \emph{Horiz.}\\[2pt]
  \textasciicircum & \Lcs{tlput} & \Lcs{taput} \\
  \textunderscore & \Lcs{trput} & \Lcs{tbput}
\end{tabular}
\end{center}
(The scheme is reversed if \Lkeyset{treeflip=true}.)

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \psset{tpos=.6}
  \pstree[treemode=R, thistreesep=1cm,
    thislevelsep=3cm,radius=2pt]{\Tc{3pt}}{%
    \pstree[treemode=U, xbbr=20pt]{\Tc{3pt}^{above}}{%
      \TC*^{left}
      \TC*_{right}}
    \TC*^{above}
    \TC*_{below}}
\end{LTXexample}

You can change the character abbreviations with


\begin{BDef}
\Lcs{MakeShortTab}\Largb{<char1>}\Largb{<char2>}
\end{BDef}

The \verb+\n*put+ commands can also give good results:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
  \psset{npos=.6,nrot=:U}
  \pstree[treemode=R, thistreesep=1cm,
    thislevelsep=3cm]{\Tc{3pt}}{%
    \Tc{3pt}\naput{above}
    \Tc*{2pt}\naput{above}
    \Tc*{2pt}\nbput{below}}
\end{LTXexample}

You can put labels on the nodes using \Lcs{nput}. However, \Lcs{pstree} won't take
these labels into account when calculating the bounding boxes.

There is a special node label option for trees that does keep track of the
bounding boxes:
\begin{BDef}
\Lnotation{\texttildelow}\OptArg*{*}\OptArgs\Largb{stuff}
\end{BDef}
Call this a ``tree node label''.

Put a tree node label right after the node to which it applies, before any
node connection labels (but node connection labels, including the short forms,
can follow a tree node label). The label is positioned directly below the node
in vertical trees, and similarly in other trees. For example:


\begin{LTXexample}
  \pstree[radius=2pt]{\Tc{3pt}\nput{45}{\pssucc}{root}}{%
    \TC*~{$h$} \TC*~{$i$} \TC*~{$j$} \TC*~{$k$}}
\end{LTXexample}

Note that there is no ``long form'' for this tree node label. However, you can
change the single character used to delimit the label with
\begin{BDef}
\Lcs{MakeShortTnput}\Largb{<char1>}
\end{BDef}
If you find it confusing to use a single character, you can also use a command
sequence. E.g.,

\begin{lstlisting}[style=syntax]
\MakeShortTnput{\tnput}
\end{lstlisting}

You can have multiple labels, but each successive label is positioned relative
to the bounding box that includes the previous labels. Thus, the order in
which the labels are placed makes a difference, and not all combinations will
produce satisfactory results.

You will probably find that the tree node label works well for terminal nodes,
without your intervention. However, you can control the tree node labels be
setting several parameters.

To position the label on any side of the node ("l"eft, "r"ight, "a"bove or
"b"elow), set:  \Lkeyword{tnpos}=\Lkeyval{l}/\Lkeyval{r}/\Lkeyval{a}/\Lkeyval{b}

\begin{LTXexample}[pos=l,width=0.4\linewidth]
\psframebox{%
  \pstree{\Tc{3pt}~[tnpos=a,tndepth=0pt,radius=4pt]{root}}{%
    \TC*~[tnpos=l]{$h$}
    \TC*~[tnpos=r]{$i$}}}
\end{LTXexample}

When you leave the argument empty, which is the default, PSTricks chooses the
label position is automatically.

To change the distance between the node and the label, set \Lkeyword{tnsep} to a dimension
When you leave the argument empty, which is the default, PSTricks uses the
value of \Lkeyword{labelsep}. When the value is negative, the distance is measured
from the center of the node.

When labels are positioned below a node, the label is given a minimum height
of \Lkeyword{tnheight}.
Thus, if you add labels to several nodes that are horizontally aligned, and if
either these nodes have the same depth or \Lkeyword{tnsep} is negative, and if the
height of each of the labels is no more than \Lkeyword{tnheight}, then the labels
will also be aligned by their baselines. The default is \nxLcs{ht}\Lcs{strutbox}, which
in most \TeX{} formats is the height of a typical line of text in the current
font. Note that the value of \Lkeyword{tnheight} is not evaluated until it is used.

The positioning is similar for labels that go below a node. The label is given
a minimum \emph{depth} of \Lkeyword{tndepth}.
For labels positioned above or below, the horizontal reference point of the
label, i.e., the point in the label directly above or below the center of the
node, is set by the \Lkeyword{href} parameter.

When labels are positioned on the left or right, the right or left edge of the
label is positioned distance \Lkeyword{tnsep} from the node. The vertical point that
is aligned with the center of the node is set by \Lkeyword{tnyref}.
When you leave this empty, \Lkeyword{vref} is used instead. Recall that \Lkeyword{vref}
gives the vertical \emph{distance} from the baseline. Otherwise, the
\Lkeyword{tnyref} parameter works like the \Lkeyword{yref} parameter, giving the fraction of
the distance from the bottom to the top of the label.

\section{Details}

PSTricks does a pretty good job of positioning the nodes and creating a box
whose size is close to the true bounding box of the tree. However, PSTricks
does not take into account the node connections or labels when calculating the
bounding boxes, except the tree node labels.

If, for this or other reasons, you want to fine tune the bounding box of the
nodes, you can set the following parameters to a dimension:

\begin{tabular}{@{}l l @{}}
\emph{name} & \emph{default}\\\hline
\Lkeyword{bbl}   &  0pt\\
\Lkeyword{bbr}&  0pt\\
\Lkeyword{bbh}&  0pt\\
\Lkeyword{bbd}&  0pt\\
\Lkeyword{xbbl}&  0pt\\
\Lkeyword{xbbr}&  0pt\\
\Lkeyword{xbbh}&  0pt\\
\Lkeyword{xbbd}&  0pt
\end{tabular}

The "`x"' versions increase the bounding box by <dim>, and the others set the
bounding box to the dimension. There is one parameter for each direction from the
center of the node, \textbf{l}eft, \textbf{r}ight, \textbf{h}eight, and
\textbf{d}epth.

These parameters affect trees and nodes, and subtrees that switch directions,
but not subtrees that go in the same direction as their parent tree (such
subtrees have a profile rather than a bounding box, and should be adjusted by
changing the bounding boxes of the constituent nodes).

Save any fiddling with the bounding box until you are otherwise finished with
the tree.

You can see the bounding boxes by setting the \Lkeyword{showbbox}=\true/\false
parameter to \true. To see the bounding boxes of all the nodes in a tree, you
have to set this parameter before the tree.

In the following example, the labels stick out of the bounding box:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
\psset{tpos=.6,showbbox=true}
\pstree[treemode=U]{\Tc{5pt}}{%
    \TR{foo}^{left}
    \TR{bar}_{right}}
\end{LTXexample}

Here is how we fix it:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
\psset{tpos=.6,showbbox=true}
\pstree[treemode=U,xbbl=8pt,xbbr=14pt]{\Tc{5pt}}{%
    \TR{foo}^{left}
    \TR{bar}_{right}}
\end{LTXexample}

Now we can frame the tree:

\begin{LTXexample}[pos=l,width=0.4\linewidth]
\psframebox[fillstyle=solid,fillcolor=lightgray,framesep=14pt,
  linearc=14pt,cornersize=absolute,linewidth=1.5pt]{%
  \psset{tpos=.6,border=1pt,nodesepB=3pt}
  \pstree[treemode=U,xbbl=8pt,xbbr=14pt]{%
    \Tc[fillcolor=white,fillstyle=solid]{5pt}}{%
    \TR*{foo}^{left}
    \TR*{bar}_{right}}}
\end{LTXexample}

We would have gotten the same result by changing the bounding box of the two
terminal nodes.

\iffalse

To skip levels, use

\begin{BDef}
\LcsStar{skiplevel}\OptArgs\Largb{nodes or subtrees}\\[4pt]
\LcsStar{skiplevels}\OptArgs\Largb{int} \\
\qquad<nodes or subtrees> \\
\Lcs{endskiplevels}
\end{BDef}


These are kind of like subtrees, but with no root node.

\begin{LTXexample}
  \pstree[treemode=R,levelsep=1.8,radius=2pt]{\Tc{3pt}}{%
    \skiplevel{\Tfan}
    \pstree{\Tc{3pt}}{%
      \TC*
      \skiplevels{2}
        \pstree{\Tc{3pt}}{\TC* \TC*}
        \TC*
      \endskiplevels
      \pstree{\Tc{3pt}}{\TC* \TC*}}}
\end{LTXexample}

The profile at the missing levels is the same as at the first non-missing
level. You can adjust this with the bounding box parameters. You get greatest
control if you use nested \Lcs{skiplevel} commands instead of \Lcs{skiplevels}.

\begin{LTXexample}
\large\psset{radius=6pt, dotsize=4pt}
\pstree[thislevelsep=0,edge=none,levelsep=2.5cm]{\Tn}{%
    \pstree{\TR{Player 1}}{\pstree{\TR{Player 2}}{\TR{Player 3}}}
    \psset{edge=\ncline}
    \pstree{\pstree[treemode=R]{\TC}{\Tdot ~{(0,0,0)} ^{N}}}{%
         \pstree{\TC[name=A] ^{L}}{%
           \Tdot ~{(-10,10.-10)} ^{l}
           \pstree{\TC[name=C] _{r}}{%
             \Tdot ~{(3,8,-4)} ^{c}
             \Tdot ~{(-8,3,4)} _{d}}}
         \pstree{\TC[name=B] _{R}}{%
           \Tdot ~{(10,-10.0)} ^{l}
           \pstree{\TC[name=D]_{r}}{%
             \Tdot ~{(4,8,-3)} ^{c}
             \Tdot ~{(0,-5,0)} _{d}}}}}
\ncbox[linearc=.3,boxsize=.3,linestyle=dashed,nodesep=.4]{A}{B}
\ncarcbox[linearc=.3,boxsize=.3,linestyle=dashed,arcangle=25,nodesep=.4]{D}{C}
\end{LTXexample}

\fi

\section{The scope of parameter changes}

\Lkeyword{edge} is the only parameter which, when set in a tree node's parameter
argument, affects the drawing of the node connection (e.g., if you want to
change the \Lkeyword{nodesep}, your edge has to include the parameter change, or you
have to set it before the node).

As noted at the beginning of this section, parameter changes made with
\Lcs{pstree} affect all subtrees. However, there are variants of some of these
parameters for making local changes, i.e, changes that affects only the
current level: \Lkeyword{thistreesep}, \Lkeyword{thistreenodesize}, 
\Lkeyword{thistreefit}=\Lkeyval{tight}/\Lkeyval{loose}, and \Lkeyword{thislevelsep}.

For example:
\begin{LTXexample}[pos=l,width=0.4\linewidth]
\pstree[thislevelsep=.5cm,thistreesep=2cm,
  radius=2pt]{\Tc*{3pt}}{%
  \pstree{\TC*}{\TC* \TC*}
  \pstree{\TC*}{\TC* \TC*}}
\end{LTXexample}

There are some things you may want set uniformly across a level in the tree,
such as the \Lkeyword{levelsep}. At level <n>, the command \nxLcs{pstreehook<roman(n)>}
(e.\,g., \Lcs{pstreehookii}) is executed, if it is defined (the root node of the
whole tree is level 0, the successor tree objects and the node connections
from the root node to these successors is level 1, etc.). In the following
example, the \Lkeyword{levelsep} is changed for level 2, without having to set the
\Lkeyword{thislevelsep} parameter for each of the three subtrees that make of 
level 2:

\begin{LTXexample}
\[
\def\pstreehookiii{\psset{thislevelsep=3cm}}
\pstree[treemode=R,levelsep=1cm,radius=2pt]{\Tc{4pt}}{%
  \pstree{\TC*}{%
    \pstree{\TC*}{\Tr{X_1} \Tr{X_2}}
    \pstree{\TC*}{\Tr{Y_1} \Tr{Y_2}}}
  \pstree{\TC*}{%
    \pstree{\TC*}{\Tr{K_1} \Tr{K_2}}
    \pstree{\TC*}{\Tr{J_1} \Tr{J_2}}}}
\]
\end{LTXexample}


\clearpage


\part{Theory}


\begin{abstract}
This is a description of a recursive alignment algorithm that is useful for drawing trees 
and tree-like graphs. It is a generalization of the algorithm in~\cite{reingold:1981}.
The purpose of the algorithm is to recursively construct a description of a {\em tree} 
in a high-level graphics language with the capabilities of PostScript. Thus, the algorithm 
is a preprocessor, and the graphics interpreter is a postprocessor. This division makes the 
algorithm simpler and more modular. The  postprocessing could be implemented internally, 
if a low-level graphics description is required.

Thanks to: Ed Reingold
\end{abstract}


\section{Introduction}

A tree is a collection of nodes, organized into levels, with each node's center assigned a 
coordinate position. The center of a node is where edges should point to. Trees have 
ragged left and right profiles, because the widths of the levels vary. In {\em horizontal mode}, 
the algorithm joins trees side by side, aligned by their top levels and fitted together 
tightly. In {\em vertical mode}, the algorithm stacks trees so that the nodes at the bottom 
level of the each tree are centered above the nodes at the top level of the next tree.

The algorithm is implemented in \LPack{pst-tvz}, which is part of the PSTricks package. 
PSTricks is a collection of PostScript extensions to \TeX. The examples in this paper 
use the PSTricks implementation. The syntax of the input file is:

\begin{lstlisting}[style=syntax]
  \psTree
    ~tree objects~ \\
    ~tree objects~ \\
    ...
    ~tree objects~
  \endpsTree
\end{lstlisting}

Each row except for the last ends in \verb=\\=. Each row is processed in horizontal mode, and then the 
rows are stacked in vertical mode. See Example~\ref{one}.

\begin{LTXexample}[width=5cm,pos=l,caption=Example 1,label=one]
  \psTree[radius=2pt,nodesep=3pt]
    \TC* \\
    \psTree
      \TC* \\
      \TC* \TC* \\
      \TC*
    \endpsTree
    \psTree
      \TC* \TC* \\
      \TC* \\
      \TC* \TC* \TC*
    \endpsTree
  \endpsTree
\end{LTXexample}

\section{The graphics description}\label{graphics}

The graphics language should have whatever features one needs to draw the nodes, edges 
and labels, plus the ability to define procedures and variables for later reference. 
Furthermore, the graphics state should keep track of a current point, which can be 
manipulated as follows:
\begin{compactenum}
  \item Operators \Lps{gsave} and \Lps{grestore}, respectively, push the current point onto a stack and 
  pop the top current point from that stack.
  \item The operator $x$ $y$ \Lps{RMOVETO} shifts the current point $x$ units to the right and $y$ units down.
\end{compactenum}
Note the convention that the $y$-direction is {\em down}.

The tree graphics description should place (the center of) the top-left node at the current 
point, and should not change the current point.


The graphics description consists of these operators plus nodes, node labels, 
edges and edge labels. Here is what these objects do:
\begin{compactdesc}\label{Labels}
\item[Node]
  Draws the node, without changing the current point, and defines a procedure, identified 
  by the node's name, that can answer queries about where to draw edges. For example, in 
  PSTricks the nodes can report the coordinate of the center of the node, and the coordinate 
  of the boundary of the node in any direction from the center.

\item[Node label]
  Draws a label at a node, by querying the node to find out where to position the label.

\item[Edge]
  Draws a line between two nodes, querying the nodes to find out where to connect the lines, 
  and then defines a procedure for finding the coordinate and slope at any point on the line.

\item[Edge label]
  Puts a label on an edge, using the procedure for finding the coordinate and slope of a 
  point on the last edge that was drawn.
\end{compactdesc}

\begin{LTXexample}[caption=Example 2,label=2]
  \def\sm{\rm\scriptsize} \footnotesize\sf
  \psTree[radius=8pt,treesep=2.5cm,levelsep=2.5cm]
    \psTree
      \TCircle{A}\nput{r}{\pssucc}{\sm $(0,0)$}
      \TCircle{B}\nput{r}{\pssucc}{\sm $(100,0)$}
      \\
      \TCircle{C}\nput{r}{\pssucc}{\sm $(50,-100)$}
    \endpsTree
    \psTree
      \TCircle{D}~[tnpos=r]{\sm $(200,0)$}
      \\
      \TCircle{E}^{$l$}\nput{r}{\pssucc}{\sm $(150,-100)$}
      \TCircle{F}~[tnpos=r]{\sm $(250,-100)$}_{$r$}
    \endpsTree
    \\
    \TCircle{G}~[tnpos=r]{\sm $(200,-200)$}
  \endpsTree
\end{LTXexample}



Suppose we want to draw the graph in Example~\ref{2}. We start by constructing the code for the subgraph 
containing nodes A, B and C. The first row (nodes A and B) is:
\begin{lstlisting}[style=syntax]
  gsave
    ~Node A~
    100 0 rmoveto
    ~Node B~
  grestore
\end{lstlisting}
and the second row is:
\begin{lstlisting}[style=syntax]
  gsave
    ~Node C~
    ~Line from Node A to Node C~
    ~Line from Node B to Node C~
  grestore
\end{lstlisting}
Then we calculate that the top-left node (node C) of the second row is positioned at $(50,100)$ from the 
top-left node (node A) of the top row. The subgraph is thus:
\begin{lstlisting}[style=syntax]
  gsave
    gsave
      ~Node A~
      100 0 rmoveto
      ~Node B~
    grestore
    50 100 rmoveto
    gsave
      ~Node C~
      ~Edge from Node A to Node C~
      ~Edge from Node B to Node C~
    grestore
  grestore
\end{lstlisting}

Similary, the subgraph for nodes D, E and F is:
\begin{lstlisting}[style=syntax]
  gsave
    gsave
      ~Node D~
    grestore
    -50 100 rmoveto
    gsave
      ~Node E~
      ~Edge from Node A to Node C~
      ~Edge label~
      ~Node F~
      ~Edge from Node B to Node C~
      ~Edge label~
    grestore
  grestore
\end{lstlisting}

To join these two subgraphs, we calculate that the distance from the top-left node of $\{A,B,C\}$ to the top-left node of $\{D,E,F\}$ is $(200,0)$. Thus, the subgraph $\{A,B,C,D,E,F\}$ is
\begin{lstlisting}[style=syntax]
  gsave
    ~Subgraph A,B,C~
    200 0 rmoveto
    ~Subgraph D,E,F~
  grestore
\end{lstlisting}

The code for the the bottom row (node G) is:
\begin{lstlisting}[style=syntax]
  gsave
    ~Node G~
    ~Edge from Node C to Node G~
    ~Edge from Node E to Node G~
    ~Edge from Node F to Node G~
  grestore
\end{lstlisting}

This node is positioned distance $(150,200)$ from the top-left node of subgraph $\{A,B,C,D,E,F\}$, and so the code for the whole graph is
\begin{lstlisting}[style=syntax]
  gsave
    gsave
      ~Subgraph A,B,C~
      rmoveto(200,0)
      ~Subgraph D,E,F~
    grestore
    150 200 rmoveto
    gsave
      ~Node G~
      ~Edge from Node C to Node G~
      ~Edge from Node E to Node G~
      ~Edge from Node F to Node G~
    grestore
  grestore
\end{lstlisting}

\section{Language requirements}

I assume that the preprocessing language has operators \verb=BEGINGROUP= and \verb=ENDGROUP= that keep 
changes to variables local to the group, and \verb=GLOBAL= which make the next change global.

There must be enough memory to hold the entire description of the tree in memory, because 
the algorithm constructs the description recursively rather than linearly.

I use the following data types:
\begin{quote}
\begin{tabular}{ll}
 integer & INT\\
 boolean & BOOL\\
 string &  STRING\\
 dimension & DIM\\
 list of strings & LOS\\
 list of dimensions & LOD
\end{tabular}
\end{quote}
Dimensions might be integers or reals, depending on the implementation. The algorithm only uses integer arithmetic.

\section{Accounting}

As seen in Section~\ref{graphics}, joining subtrees is mainly a problem of finding the 
distance between them. If we simply joined them by inserting a fixed amount of space 
between their bounding boxes (the way \TeX\ builds boxes from boxes) then we would 
only need to know each subtree's bounding box. Instead, for horizontal mode we need 
to keep track of the different sizes of the levels (the profiles). For alignment in 
vertical mode, we also need to know the positions of the extreme nodes in the top and 
bottom levels. For automatic drawing of edges, we need to keep track of the names of 
the nodes at the bottom level (which the top nodes of the next level draw edges to). 
We keep track of a few more items that are used by some of the special features 
described in Section~\ref{bells}.

Here is the list of the tree data. (The distance between nodes refers to the distance between the 
centers of the nodes.) There is some redundancy, because it can be faster to keep track of 
information in the form it is needed rather than extracting it from other information.



\begin{compactdesc}%{\tt rightprofile}
  \item[\texttt{treecode}] The graphics description of the tree. (DIM)

  \item[\texttt{width}] The distance from the top-left node to the top-right node. (DIM)

  \item[\texttt{leftprofile}] The horizontal distance from the left edge of the bounding box of each level to the top-left node. (LOD)

  \item[\texttt{rightprofile}] The horizontal distance from the top-right node to the right edge of the bounding box of each level. (LOD)

  \item[\texttt{leftbase}] The horizontal distance from the bottom-left node to the top-left node. (DIM)

  \item[\texttt{rightbase}] The horizontal distance from the top-right node to the bottom-right node. (DIM)

  \item[\texttt{center}] The distance from the top-left node to the center of the top level 
  (for alignment in vertical mode), or \verb=NULL= if the center should be the midpoint 
  between the top-left and the top-right nodes. (DIM)

  \item[\texttt{centerbase}] The distance from the top-left node to the center of the bottom level 
  (for alignment in vertical mode), or \verb=NULL= if the center should be the midpoint between the 
  bottom-left and bottom-right nodes. (DIM)

  \item[\texttt{height}] The vertical distance from the top of the bounding box to the top  level. (DIM)

  \item[\texttt{depth}] The vertical distance from the top level to the bottom of the bounding box. (DIM)

  \item[\texttt{leftsize}] The horizontal distance from the left side of the bounding box to the top-left node. (DIM)

  \item[\texttt{rightsize}] The horizontal distance from the top-right node to the right side of the bounding box. (DIM)

  \item[\texttt{rootnodes}] A list of the names of the top-level nodes. (LOS)

  \item[\texttt{basenodes}] A list of the names of the bottom-level nodes. (LOS)

  \item[\texttt{cumlevelsep}] The distance between the first and last levels. (DIM)

  \item[\texttt{numlevels}] The number of levels in the tree. (INT)

  \item[\texttt{levelsizes}] The list of the height and depth of the bounding box of each level, plus, 
  for every level except the last, the vertical distance to the next level.
\end{compactdesc}

See Figure~\ref{data}.

\begin{figure}
  \psset{unit=.85}
  \unitlength\psunit
  \pspicture(-3,-6)(10.5,1)
  \def\N(#1,#2)(#3,#4){%
    \rput(#3,#4){\framebox(#1,#2){\hbox{\qdisk(0,0){2pt}}}}}
  \N(3,1.5)(0,0)
  \N(4,2)(5.5,0)
  \N(3,2)(-1.75,-4)
  \N(2,1)(2.75,-4)
  \N(3,1.5)(7.25,-4)
  \psline[linestyle=dashed,linewidth=.4pt](0,0)(0,-6)
  \psline[linestyle=dashed,linewidth=.4pt](5.5,0)(5.5,-6)
  \pcline{|->}(-1.5,-1.2)(0,-1.2)
  \ncput*{L1}
  \pcline{|->}(-3.25,-2.6)(0,-2.6)
  \ncput*{L2}
  \pcline{->|}(5.5,-1.35)(7.5,-1.35)
  \ncput*{R1}
  \pcline{->|}(5.5,-2.8)(8.75,-2.8)
  \ncput*{R2}
  \pcline{|->}(-1.75,-5.3)(0,-5.3)
  \nbput[npos=.3]{\tt leftbase}
  \pcline{->|}(5.5,-5.2)(7.25,-5.2)
  \nbput[npos=.7]{\tt rightbase}
  \pcline[tbarsize=15pt]{|->|}(9.4,0)(9.4,-4)
  \ncput*{\tt cumlevelsep}
  \endpspicture

  \vskip .4cm

  \verb|leftprofile  = { L1 , L2 }|\\
  \verb|rightprofile = { R1 , R2 }|

  \vskip .8cm

  \pspicture(-5,-5)(10.5,1.75)
  \def\N(#1,#2)(#3,#4){%
    \rput(#3,#4){\framebox(#1,#2){\hbox{\qdisk(0,0){2pt}}}}}
  \N(3,1.5)(0,0)
  \N(4,2)(5.5,0)
  \N(3,2)(-1.75,-4)
  \N(2,1)(2.75,-4)
  \N(3,1.5)(7.25,-4)
  \psframe[linestyle=dashed,linewidth=.4pt](-3.25,-5)(8.75,1)
  \psset{tbarsize=15pt}
  \pcline{|->}(-3.25,1.5)(0,1.5)
  \ncput*{\tt leftsize}
  \pcline{|->}(0,1.5)(5.5,1.5)
  \ncput*{\tt width}
  \pcline{|->|}(5.5,1.5)(8.75,1.5)
  \ncput*{\tt rightsize}
  \pcline{|->}(9.4,1)(9.4,0)
  \naput{\tt height}
  \pcline{|->|}(9.4,0)(9.4,-5)
  \naput{\tt depth}
  \pcline{|->}(-3.75,1)(-3.75,0)
  \nbput{\tt H1}
  \pcline{|->|}(-3.75,0)(-3.75,-1)
  \nbput{\tt D1}
  \pcline{|->}(-3.75,-3)(-3.75,-4)
  \nbput{\tt H2}
  \pcline{|->}(-3.75,-4)(-3.75,-5)
  \nbput{\tt D2}
  \pcline{|->|}(-5,0)(-5,-4)
  \ncput*{\tt levelsep1}
  \endpspicture

\vskip .4cm

\verb|levelsizes = { H1 , D1 , levelsep1 , H2 , D2 }|

\vskip .6cm

\caption{Tree data}\label{data}
\end{figure}

\section{Horizontal mode}

  In horizontal mode, the trees are aligned by their toplevels  (i.\,e., a tree's baseline is the 
  center of its top level). We add trees to the row one-by-one, updating the description of the row each time.

  A row, while under construction, is itself a tree, and each time we add a tree we update 
  the data for the row. As we construct the graphics description for the row, the current 
  point is left at the top-left node of the last tree. We keep track of the width of the 
  last tree (\verb=Lastwidth=). Each time we add a tree to the row, we face the canonical problem 
  of determining how much space to leave between the top-right node of the row and the 
  top-left node of the next tree.

  To distinguish the tree data variables of the row from those of the next tree to be added 
  to the row, we begin the variable names for the row with capital letters. E.\,g., \verb|Leftprofile| 
  is the leftprofile of the row, and \verb|leftprofile| is the leftprofile of the next tree.

  When adding the first tree object, we have to simply initialize the row's variables:
\begin{lstlisting}[style=syntax]
  Treecode     = treecode
  Width        = width
  Lastwidth    = width
  Leftprofile  = leftprofile
  Rightprofile = rightprofile
  Leftbase     = leftbase
  Rightbase    = rightbase
  Center       = center
  Centerbase   = centerbase
  Height       = height
  Depth        = depth
  Leftsize     = leftsize
  Rightsize    = rightsize
  Rootnodes    = rootnodes
  Basenodes    = basenodes
  Cumlevelsep  = cumlevelsep
  Numlevels    = numlevels
  Levelsizes   = levelsizes
\end{lstlisting}

 For subsequent tree object's, we first find the distance between the top-right node of the 
 current row and the top-left node of the next object, and we assign the result to \verb|sep|. 
 We want the minimum distance between the objects, level-by-level, to be \verb|treesep| (a parameter):
\begin{lstlisting}[style=syntax]
  sep = MAX { Rightprofile ++ leftprofile } + treesep
\end{lstlisting}
where \verb|++| makes a list by adding two lists item-by-item, up to the length of the shortest list.


Now we can add the new tree's code to the row's code:
\begin{lstlisting}[style=syntax]
  Treecode = CONCAT
             {
                   Treecode 
                   sep + Lastwidth  0 rmoveto
                   treecode
             }
\end{lstlisting}

Then we update the row description. First we set \verb|Width| to the distance from the top-left node of the row to 
the top-left node of the next tree (\verb=Width+sep=) and we set \verb|sep| to the distance from the top-right 
node of the previous tree to the top-right node of the next tree (\verb=sep+width=), because these quantities 
are used in the calculations of the other row variables. At the end, we set \verb|Width| to the actual 
width of the row (\verb|Width+width|).
\begin{lstlisting}[style=syntax]
  Width        = Width + sep
  sep          = sep + width
  Lastwidth    = width
  Leftprofile  = BIMAX { Leftprofile , leftprofile - Width }
  Rightprofile = BIMAX { Rightprofile - sep , rightprofile }
  IF    Numlevels < numlevels
  THEN  Leftbase = Leftbase - Width
  FI
  Rightbase    = IF    Numlevels > numlevels
                 THEN  Rightbase - sep - width
                 ELSE  rightbase
                 FI
  Height       = MAX { Height , height }
  Depth        = MAX { Depth , depth }
  Leftsize     = MAX { Leftsize , leftsize - Width }
  Rightsize    = MAX { Rightsize - sep , rightsize }
  Rootnodes    = CONCAT { Rootnodes , rootnodes }
  IF    center = NULL
  ELSE  Center = center + Width
  FI
  IF    Numlevels < numlevels OR ( Numlevels = numlevels
              AND NOT centerbase = NULL )
  THEN  Centerbase = centerbase + Width
  FI
  IF    Numlevels < numlevels
  THEN  Basenodes = basenodes
  ELSE  IF    Numlevels = numlevels
        THEN  Basenodes = CONCAT { Basenodes , basenodes }
        FI
  FI
  Numlevels    = MAX { Numlevels , numlevels }
  Levelsizes   = BIMAX { Levelsizes, levelsizes }
  Width        = Width + width
\end{lstlisting}

The updating that depends on \verb|Numlevels| and \verb|numlevels| can be summarized:
\begin{lstlisting}[style=syntax]
  IF    Numlevels < numlevels
  THEN  Leftbase = leftbase - Width
        Centerbase = centerbase + Width
        Rightbase = rightbase
        Basenodes = basenodes
        Cumlevelsep = cumlevelsep
  ELSE  IF    Numlevels = numlevels
        THEN  Basenodes = CONCAT { Basenodes , basenodes }
              Rightbase = rightbase
              IF    centerbase = NULL
              ELSE  Centerbase = centerbase + Width
              FI
        ELSE  Rightbase = Rightbase - sep
        FI
  FI
\end{lstlisting}

Nodes are treated in the same way. A node is a trivial tree. It is completely described by its nodeleftsize (distance from the center to the left side of the bounding box), noderightsize, nodeheight, nodedepth and name. Here is the value of all the tree object variables in terms of the leftsize, rightsize, height, depth and name:
\begin{lstlisting}[style=syntax]
  treecode     = {~node~}
  width        = 0
  leftprofile  = {nodeleftsize}
  rightprofile = {noderightsize}
  leftbase     = 0
  rightbase    = 0
  center       = NULL
  centerbase   = NULL
  height       = nodeheight
  depth        = nodedepth
  leftsize     = nodeleftsize
  rightsize    = noderightsize
  rootnodes    = {name}
  basenodes    = {name}
  cumlevelsep  = 0
  numlevels    = 1
  levelsizes   = {height,depth}
\end{lstlisting}

\section{Vertical mode}

 Here is the description of vertical mode. We also add rows one at a time, updating the description 
 of the tree each time. Each row is just a tree object, and a partially completed tree is just a 
 tree object. Therefore, the problem of joining rows is just the canonical problem of stacking 
 two tree objects.

  The description variables of the row begin with capital letters, and so we revert to 
  uncapitalized names for the description variables of the tree.

  When adding the first row, we simply have to initialize the tree's variables, 
  setting \verb|treecode=Treecode|, etc.

  To add the subsequent rows, we first have to find the horizontal displacement of the 
  top-left node of the next row from the top-left node of the tree. We chose this 
  displacement so that the \verb|centerbase" of the tree is aligned with the \verb|Center| 
  of the row, as shown in Figure~\ref{center}.

\begin{figure}
\begin{pspicture}(-2.3,-4)(7,1)
  \psset{radius=3pt,labelsep=.7cm}
  \def\biglbrace{$\left\{\vrule height .75cm depth .75cm width 0pt \right.$}
  \rput[l](-1.9,0){\llap{Tree} \biglbrace}
  \rput[l](-1.9,-3){\llap{Row} \biglbrace}
  \Cnode*(0,0){A}
  \uput[u](0,0){\rnode{AA}{Top-left node}}
  \ncline[nodesep=3pt]{->}{AA}{A}
  \Cnode*(6,0){B}
  \uput[u](6,0){\rnode{C}{Center of base}}
  \ncline[nodesep=3pt]{->}{C}{B}
  \ncline{<->}{A}{B}
  \ncput*{\tt centerbase}
  \Cnode*(6,-3){D}
  \uput[d](6,-3){\rnode{DD}{Center of top}}
  \ncline[nodesep=3pt]{->}{DD}{D}
  \Cnode*(2,-3){E}
  \uput[d](2,-3){\rnode{EE}{Top-left node}}
  \ncline[nodesep=3pt]{->}{EE}{E}
  \ncline{<->}{D}{E}
  \ncput*{\tt Center}
  \pnode(2,-1.5){F}
  \ncline[linestyle=dashed,dash=2pt 2pt]{E}{F}
  \pnode(0,-1.5){G}
  \ncline[linestyle=dashed,dash=2pt 2pt]{A}{G}
  \ncline{|*-|*}{G}{F}
  \ncput*{\tt sep}
\end{pspicture}
\caption{Aligning rows in vertical mode.}\label{center}
\end{figure}

  First, calulate \verb=centerbase= and \verb=Center= if these are \verb=NULL=:
\begin{lstlisting}[style=syntax]
  IF    centerbase = NULL
  THEN  centerbase = ( width + rightbase - leftbase ) / 2
  FI
  IF    Center = NULL
  THEN  Center = width / 2
  FI
\end{lstlisting}
Then set \verb|sep| to the horizontal distance between the top-left nodes of the tree and row:
\begin{lstlisting}[style=syntax]
  sep = centerbase - Centerbase
\end{lstlisting}

Next we calculate the vertical displacement. Each time we add a row, the current point ends up 
at the top-left node of the last row. We save the \verb=Cumlevelsep= of the last row as \verb=lastcumlevelsep=. 
The distance from the bottom level of the tree and the top level of the next row is the canonical distance 
between levels, \Lkeyword{levelsep}, which is a parameter. Hence, the total displacement is
\begin{lstlisting}[style=syntax]
  lastcumlevelsep + levelsep
\end{lstlisting}

Thus, we add the new row's code to the tree's code with:
\begin{lstlisting}[style=syntax]
  treecode = CONCAT {
                    treecode 
                    sep lastcumlevelsep+levelsep rmoveto
                    Treecode
             }
\end{lstlisting}

Now we have to update the description. At first, \verb|cumlevelsep| is set to the distance from the top level of the tree 
to the top level of the next row (\verb=cumlevelsep + levelsep=) and \verb=rightsep= is set to the horizontal distance 
from the top-right node of the tree to the top-right node of the next row (\verb=sep + Width - width=), 
because these are used when updating the other variables. At the end, \verb=cumlevelsep= is set to the actual 
\verb=cumlevelsep= (\verb=cumlevelsep + Cumlevelsep=).

\begin{lstlisting}[style=syntax]
  cumlevelsep     = cumlevelsep + levelsep
  lastcumlevelsep = Cumlevelsep
  rightsep        = sep + Width - width
  leftprofile     = CONCAT { leftprofile , Leftprofile - sep }
  rightprofile    = CONCAT {
                           rightprofile ,
                           Rightprofile + rightsep )
                    }
  leftbase        = Leftbase - sep
  rightbase       = Rightbase + rightsep
  centerbase      = IF    Centerbase=NULL
                    THEN  NULL
                    ELSE  Centerbase - sep
                    FI
  height          = MAX { height , Height - cumlevelsep }
  depth           = MAX { depth , Depth + cumlevelsep }
  leftsize        = MAX { leftsize , Leftsize - sep }
  rightsize       = MAX { rightsize , Rightsize + rightsep }
  rootnodes       = rootnodes
  numlevels       = numlevels + Numlevels
  levelsizes      = CONCAT { levelsizes , levelsep , Levelsizes }
  cumlevelsep     = cumlevelsep + Cumlevelsep
\end{lstlisting}

\section{Bells and whistles\label{bells}}

e also need to keep track of the list of nodes in the tree object, and the coordinates of the nodes. 
We can measure the coordinates relative to the top-left node. Then when we join two tree objects, 
we find the top-left node of the new object, join the lists of nodes, and update the coordinates 
with respect to the top-left node. This is simplified by the fact that once a tree object has been 
formed, the relative position of the nodes within that object does not change when the object is 
nested inside another tree object.

I have so far described the algorithm assuming that the objects in a row are joined from left to 
right, and then the rows are stacked from top to bottom, and I will continue to use this convention 
throughout. However, the algorithm is the same when the tree objects grow in different directions; 
all that differs in \LPack{pst-tvz} is how one joins tree objects. For example, after calculating the 
distance between the top-left nodes of two tree objects, do we position the second object below, 
to the right, above or to the left of the first object?

\LPack{pst-tvz} uses a key=value system for controlling the algorithm. Keys are called {\em parameters}. 
Here are the parameters that control the direction in which the tree is constructed:
\begin{compactdesc}%{treenodesize}
  \item[\Lkeyword{treemode}] The \Lkeyword{treemode} is the direction in which trees grow (in which rows are stacked). 
  The value is stored as an integer:
\begin{lstlisting}[style=syntax]
  down  -> 0
  right -> 1
  up    -> 2
  left  -> 3
\end{lstlisting}
In vertical trees (\Lkeyword{treemode} is even), the rows are horizontal. In horizontal trees 
(\Lkeyword{treemode} is odd), the rows are vertical.
  \item[\Lkeyword{treeflip}] \Lkeyword{treeflip} is a boolean that sets the direction in which rows are 
  constructed. When \false, the horizontal rows of vertical trees are constructed from left to right 
  (in the order in which objects appear in the input file), and the vertical rows of horizontal trees 
  are constructed from top to bottom. When \true, the rows of vertical trees are constructed from
   right to left, and the rows of horizontal trees are constructed from bottom to top.
\end{compactdesc}

For example:
\begin{LTXexample}[width=5cm,pos=l]
  \psTree[treemode=R,treeflip=true,nodesep=3pt]
    \Tc{3pt} \\
    \Tr{First} \Tr{Second} \Tr{Third}
  \endpsTree
\end{LTXexample}

There are several methods for setting this distance.

 If the "treesep*" parameter has been set, then
\begin{lstlisting}[style=syntax]
  sep = treesep*
\end{lstlisting}
That is, the spacing between the centers of the nodes (and hence between edges) is fixed.

Otherwise, if the \Lkeyword{treefit} parameter equals \Lkeyval{tight},

If instead \Lkeyset{treefit=loose}, the distance between the tree objects' bounding boxes be \Lkeyword{treesep}. I.\,e.,
\begin{lstlisting}[style=syntax]
  sep = MAX { Rightprofile } + MAX { leftprofile } + treesep
\end{lstlisting}

In summary:
\begin{lstlisting}[style=syntax]
  sep = IF    treesep* = NULL
        THEN  IF    treefit = tight
              THEN  MAX { Rightprofile ++ leftprofile } + treesep'
              ELSE  MAX { Rightprofile } + MAX { leftprofile } + treesep
              FI
        ELSE  treesep*
        FI
\end{lstlisting}

If both objects have more than one level, then increase \verb=sep= by \Lkeyword{xtreesep}:
\begin{lstlisting}[style=syntax]
  IF    Numlevels > 1
  THEN  IF    numlevels > 1
        THEN  ADVANCE sep BY xtreesep
        FI
  FI
\end{lstlisting}
Positive values of \Lkeyword{xtreesep} can be used to highlight the structure of the trees.

Finally, if the user inserts
\begin{lstlisting}[style=syntax]
  \addtreesep{~dim~}
\end{lstlisting}
before a tree object, then ~dim~ is saved in the \verb=addtreesep= variable, and we add this to \verb=sep=:
\begin{lstlisting}[style=syntax]
  IF    addtreesep = NULL
  ELSE  ADVANCE sep BY addtreesep
  FI
\end{lstlisting}

\begin{lstlisting}[style=syntax]
  Treecode = CONCAT {
                 Treecode ,
                 IFODD  Treemode
                 THEN   IF    Treeflip=TRUE
                        THEN  0 sep rmoveto
                        ELSE  0 -sep rmoveto
                        FI
                 ELSE   IF    Treeflip=TRUE
                        THEN  -sep 0 rmoveto
                        ELSE  sep 0 rmoveto
                        FI
                 FI
                 ,
                 treecode
             }
\end{lstlisting}

A node calculates its leftsize, rightsize, height, depth and name, and then invokes 
\verb=\node@makecanonical@tree=, which does the assignment given above.

The assignment actually depends on the orientation of the row, because the node calculates its 
dimensions for an upright orientation. That is, the assignment given above is correct if the row 
is part of a horizontal tree that grows down and if the row adds objects from left to right.

Here is the general assignment of leftprofile, rightprofile, height and depth: 
\begin{lstlisting}[style=syntax]
  height       = IFCASE Treemode
                        nodeheight
                 OR     leftsize
                 OR     nodedepth
                 OR     rightsize
                 FI
  depth        = IFCASE Treemode
                        nodedepth
                 OR     rightsize
                 OR     nodeheight
                 OR     leftsize
                 FI
  leftsize     = IFODD Treemode
                 THEN  IF   Treeflip=TRUE
                       THEN nodedepth
                       ELSE nodeheight
                       FI
                 ELSE  IF   Treeflip=TRUE
                       THEN rightsize
                       ELSE leftsize
                       FI
                 FI
  rightsize    = IFODD Treemode
                 THEN  IF   Treeflip=TRUE
                       THEN nodeheight
                       ELSE nodedepth
                       FI
                 ELSE  IF   Treeflip=TRUE
                       THEN leftsize
                       ELSE rightsize
                       FI
                 FI
  leftprofile  = { leftsize, }
  rightprofile = { rightsize, }
\end{lstlisting}

However, if the \Lkeyword{treenodesize} is set, then the profile are set using this value as half the ``width'' of the node. That is:
\begin{lstlisting}[style=syntax]
  IF    treenodesize = NULL
  THEN  leftprofile  = { leftsize, }
        rightprofile = { rightsize, }
  ELSE  leftprofile  = { treenodesize, }
        rightprofile = leftprofile
  FI
\end{lstlisting}

Tree objects whose orientation is different from the row are given special treatment. If the object 
has the same direction, but a different flip, then we simply swap the left and right profiles, and related items:
\begin{lstlisting}[style=syntax]
  IF    Treemode = treemode
  THEN  IF    Treeflip = treeflip
        ELSE  temp         = leftprofile
              leftprofile  = rightprofile
              rightprofile = temp
              temp         = leftbase
              leftbase     = rightbase
              rightbase    = temp
              temp         = leftsize
              leftsize     = rightsize
              rightsize    = temp
              center       = IF    center = NULL
                             THEN  NULL
                             ELSE  width - center
                             FI
              centerbase   = IF    centerbase = NULL
                             THEN  NULL
                             ELSE  width - centerbase
                             FI
        FI
  FI
\end{lstlisting}

If the tree objects has a different direction, then we treat the object like a node, centered at the center of its top level
\begin{lstlisting}[style=syntax]
  IF    Treemode = treemode
  ELSE  tree@makecanonical@node
        node@makecanonical@tree
  FI
\end{lstlisting}

Here is the definition of \verb=tree@makecanonical@node=:

\begin{lstlisting}[style=syntax]
  IF    center = NULL
  THEN  center = width / 2
  FI
  IF    center = 0
  ELSE  SETBOX box =
        IFODD treemode
        THEN  VBOX TO 0 BEGINBOX VSS
        ELSE  HBOX TO 0 BEGINBOX HSS
        FI
              BOX box
              KERN IF Treeflip = TRUE THEN - FI center
        ENDBOX
  FI
  IF    treeflip = TRUE
  THEN  tempa = rightsize + width - center
        tempb = leftsize + center
  ELSE  tempa = leftsize + center
        tempb = rightsize + width - center
  FI
  IFODD treemode
  THEN  nodeheight = tempa
        nodedepth  = tempb
  ELSE  leftsize   = tempa
        rightsize  = tempb
  FI
  IFCASE treemode
         nodeheight = height
         nodedepth  = depth
  OR     leftsize   = height
         rightsize  = depth
  OR     nodeheight = depth
         nodedepth  = height
  OR     leftsize   = depth
         rightsize  = height
  FI
\end{lstlisting}

We increase "sep" by \Lkeyword{treeshift}:
\begin{lstlisting}[style=syntax]
  ADVANCE sep BY treeshift
\end{lstlisting}

Now we insert \Lkeyword{levelsep} between the trees, move the row by \verb=sep=, and add an extra space of 
\verb=Cumlevelsep= so that the total size of the the tree is the same as \verb=cumlevelsep=.  With vertical 
trees we do this in a \Lcs{vtop} and in horizontal trees we do this in an \Lcs{hbox}. I.\,e.,
\begin{lstlisting}[style=syntax]
  IFODD treemode
  THEN  HBOX
        {
              UNHBOX box
              IF    treeflip = TRUE
              THEN  KERN - levelsep
                    LOWER sep BOX hbox
                    KERN - Cumlevelsep
              ELSE  KERN levelsep
                    RAISE sep BOX hbox
                    KERN Cumlevelsep
              FI
        }
  ELSE  VTOP
        {
              UNVBOX box
              IF    treeflip = TRUE
              THEN  KERN - levelsep
                    MOVELEFT sep BOX hbox
                    KERN - Cumlevelsep
              ELSE  KERN levelsep
                    MOVERIGHT sep BOX hbox
                    KERN Cumlevelsep
              FI
        }
  FI
\end{lstlisting}


\section{The PSTricks implementation}

In \LPack{pst-tvz}, we can let \TeX\ keep track of nodes and node coordinates internally. We store each 
tree object in a \TeX\ box with zero size, such that the current point is at the center of the top 
left node. We create a new tree object in a \TeX\ box, inserting space between the component objects.

With \TeX, we construct the row for both vertical and horizontal trees using an \Lcs{hbox}. In an \Lcs{hbox}, 
we can insert horizontal space to separate tree objects in a horizontal row, and we can lower or raise 
objects below or above to baseline to separate tree objects in a vertical row. For horizontal rows 
(vertical trees), the current insertion point is thus at the top-left node of the last object, and 
so we also need to know the width of this object. This value is stored in \verb=wsep= after adding a tree 
object, and then \verb=wsep= is set to the total distance between the top-left node of the last object and 
the top-left node of the current object. In vertical rows (horizontal trees). the current insertion 
point is at the top-left node of the row, and so we also need to know the width of the current row, 
but this is already stored in \verb=Width=. We set \verb=wsep= to the total distance between the top-left node 
of the row and the top-left node of the new object.

\begin{lstlisting}[style=syntax]
  IFODD treemode
  THEN  wsep = Width + sep
  ELSE  wsep = wsep + sep
  FI
\end{lstlisting}

We add the space and the tree object
\begin{lstlisting}[style=syntax]
  IFODD treemode
  THEN  LOWER wsep
  ELSE  KERN wsep
  FI
  BOX box
\end{lstlisting}
and update the row description:

\section{Examples}

Here is how this information is used to position the successors. First, all terminal nodes 
are treated as single-level trees. Thus, the canonical successor is a subtree (that has the 
same orientation as the parent tree). The successors are positioned so that their centers 
line up horizontally. How the distance between successors is calculate depends on the 
values of two parameters:

\begin{compactitem}
 \item When "treefit=tight", the subtrees are positioned so that the minimum distance 
 between levels is "treesep". This is calculated by adding the right profile of the 
 current group of successors (this profile is with respect to the center of the 
 right-most successor) to the left profile of the new successor item by item, finding 
 the maximum of the resulting list, and then adding "treesep".

\item When \Lkeyset{treefit=loose}, the subtrees are positioned so that the distance between their bounding 
boxes is \Lkeyword{treesep}.

\item When also \Lkeyword{treenodesize} is non-negative, the top level of each subtrees is given a width of <dim>, 
{\em  for the purpose of fitting the subtrees together}.
\end{compactitem}

After the row of successors is constructed, and its profiles, height and depth are calculated, the root object is 
positioned above the row of successors so that the center of the root object is centered between the centers of 
the first and last successors (although this can be modified). The distance between the root object and the row 
of successors is \Lkeyword{levelsep}. The profiles, height and depth of the resulting tree are calculated 
from the dimensions of the root object and the dimensions of the row of successors.

The \Lkeyword{treemode} parameter determines the direction in which the tree grows, and the \Lkeyword{treeflip} parameter determines 
the direction in which successors are added. The values of these parameters together are called the tree's 
orientation. The terminology used above is for trees with the default orientation: \Lkeyset{treemode=D} and \nxLkeyword{treeflip=false} 
(the tree grows down and successors are added from left to right). 
However, the logical structure of a tree does not depend on its orientation, and so we can use the 
same terminology and accounting system for all trees. Here is the correspondence between 

\begin{compactitem}
\item For a vertical tree that grows up, successors are also added from left to right, and so the profiles 
are as described above (although ``top'' levels are physically at the bottom of the tree). The ``height'' and 
``depth'' of tree is the distance from the center to the physical bottom and top, respectively, of the bounding box.
\item For a horizontal tree, successors are added from top to bottom. The ``left'' profiles are the physical 
top profiles, and the ``right'' profiles are the physical bottom profiles. The ``height'' and ``depth'' 
of a horizontal tree that grows to the right are the distances from the center of the tree to the left 
and right sides, respectively, of its bounding box. The opposite holds if the tree grows to the left.
\item If \nxLkeyword{treeflip=true}, then successors are added in the opposite direction, and so the left and 
right profiles are switched. ``Right'' always refers to the direction in which new successors are added.
\end{compactitem}

A root node can actually be a subtree, and a subtree can have a different orientation from its parent. 
Here is how we deal with these special cases:

The canonical root object is a node. Trees are converted to this canonical root object by calculating 
their bounding box, and thereby determining their height, width, left and right sizes.

The canonical successor is a subtree that has the same orientation as the parent tree. Nodes and trees 
that grow in other directions are converted to this canonical succcessor by treating them as single level 
trees. That is, the left profile is just the left size of the node or tree (with respect to the orientation 
of the parent tree), and the right profile is the right size of the node or tree.

In addition to being a root or successor of a tree, a tree object can be unnested, or ``outer''. 
The canonical outer object is a node, and it is made into a box whose dimensions are the size of the node. 
Trees are converted to this canonical outer. Hence, when a tree is outer, we only need to remember store its 
bounding box, and can forget about its profile.

A subtree that grows in the same direction (which is weaker than having the same orientation) is called a {\em proper} 
subtree. All other trees---outer, root, or subtrees that change directions---are not proper.

The three places a tree object can be found---root, successor or outer---are called {\em modes}. By an unfortunate 
historical accident, the directions trees grow---down, up, right and left---are also called modes. However, 
this should not cause confusion in the code.

The programming implementation of this algorithm is modular. Tree objects, which are either nodes or (sub)trees, 
save their dimensions in designated registers and commands. Then they invoke

\begin{lstlisting}[style=syntax]
  \ps<object_type>@makecanonical
\end{lstlisting}
<object\_type> is \verb=node= or \verb=tree=. This translates the object's dimensions into dimensions 
of a canonical object for the current mode. Then the object invokes

\begin{lstlisting}[style=syntax]
  \ptr@build
\end{lstlisting}

which positions the box (\Lcs{ptr@box}) containing the object, also depending on the current mode.

Here are the registers and commands that a tree object must set before invoking \nxLcs{ps<object\_type>@makecanonical} and 
\nxLcs{ps<object\_type>@build}:

\begin{compactdesc}
\item[Nodes] These are all dimension registers, and should not be set globally.\label{nodesizes}
\begin{center}
\begin{tabular}{ll}
 \Ldim{pst@dima} & left size\\
 \Ldim{pst@dimb} & right size\\
 \Ldim{pst@dimc} & height\\
 \Ldim{pst@dimd} & depth
\end{tabular}
\end{center}

\item[Trees]
\Lcs{PTR@height} and \Lcs{PTR@depth} are count registers, measuring sp units. The others are lists. These are 
set globally, so that a tree can use these commands and registers while it is being constructed. However, 
changes are actually kept local with respect to the structure of trees because he values in effect when 
the tree is started are restored at the end of the tree.
\begin{center}
\begin{tabular}{ll}
 \Ldim{PTR@height}        & height\\
 \Ldim{PTR@depth}         & depth\\
 \Lcs{PTR@leftprofile}   & left profile\\
 \Lcs{PTR@rightprofile}  & right profile\\
 \Lcs{PTR@levelsizes}    & level size
\end{tabular}{ll}
\end{center}
\end{compactdesc}

The \nxLcs{ps<object\_type>@makecanonical} commands translate the values stored in the commands and 
registers listed above, and assign the results to the following commands and registers, for use 
by \nxLcs{ps<object\_type>@build}:
\begin{compactdesc}
\item[Outer mode]
 Outer objects use \Ldim{pst@dima}, \Ldim{pst@dimb}, \Ldim{pst@dimc} and \Ldim{pst@dimd}, like nodes. These dimensions 
 refer to the physical dimensions. I.e., they do not depend on the orientation of the object.

\item[Root mode]
These are all commands.
\begin{tabular}{ll}
 \Ldim{psroot@leftsize} & left size\\
 \Ldim{psroot@rightsize} & right size\\
 \Ldim{psroot@height} & height\\
 \Ldim{psroot@depth} & depth
\end{tabular}

\item[Successor objects]
\Ldim{ptr@height} and \Ldim{ptr@depth} are counters, and the remaining are lists.
\begin{center}
\begin{tabular}{ll}
 \Ldim{ptr@height}        & height\\
 \Ldim{ptr@depth}         & depth\\
 \Lcs{ptr@leftprofile}   & left profile\\
 \Lcs{ptr@rightprofile}  & right profile\\
 \Lcs{ptr@levelsizes}    & level size
\end{tabular}{ll}
\end{center}

\end{compactdesc}

Except for \Ldim{pst@dima}, etc., used for the dimensions of nodes, root and outer objects, all values are stored 
as integers, giving the distance in sp units. Much of the accounting is done using counters and sp units, 
because this is more efficient and because counters are not quite as scarce as dimension registers.

When a subtree is made canonical, we need to know the orientation of both the subtree and the parent tree. 
We use \Lcs{psk@Treemode} and \Lcs{if@Treeflip} to keep track of the orientation of the parent tree. These are set 
by the parent tree when it begins to process the row of successors. This information is not needed by root objects.

When the value of the \Lkeyword{levelsep} parameter is preceded by \verb=*=, the size of the levels is taken into account 
when setting the distance between levels. This information is only known after the tree has been constructed, 
because levels extend beyond the recursive structure of the trees. That is, the distance between levels of one 
subtree will depend on the distance be levels of other (disjoint) subtrees. Therefore, we write this information 
to an auxilary file, to be read the next time the main input file is processed. Each level in a tree must have 
a unique identifier, so that a subtree can find the distance between levels by looking up the value for its 
tree and level. The count register \Lcs{ptr@ID} is used to indentify trees, and the count register \Lcs{ptr@levelID} 
is used to identify the levels in a tree.

The logical compenents of a tree do not coincide with the physical components when subtree appears as a root 
tree, or as a successor to a parent with a distinct orientation. E.\,g., there is no point in trying to synchronize 
the distance between levels of two subtrees that grow in different directions. Hence, in each of these cases, 
the \Lcs{ptr@ID} is advanced so that for the purpose of determining the distance between levels, the subtree has a 
different indentifier from its parents. For a subtree that appears as a successor, the identifier should be 
changed when \Lcs{psk@treemode} does not equal \Lcs{psk@Treemode}. So that this test works in outer and root mode, the 
\Lcs{psk@treemode} is set to $-1$ in these modes (the \Lkeyword{treemode} is saved as 0, 1, 2 or 3).

Because the value of \Lcs{ptr@ID} can change globally within a tree, a tree's identifier is saved as \Lcs{ptr@id} 
immediately after \Lcs{ptr@ID} is incremented. \Lcs{ptr@id} is an ordinary command.

In addition to not relying on \TeX\ boxes to do all the accounting, we cannot rely on \TeX\ grouping to do 
keep values of certain commands and registers local. This is because the successors, which are in their own 
\TeX\ group, must communicate information to the parent tree as it constructs the row of successors (e.\,g., 
by modifying the parent tree's \Lcs{PTR@height}). We get around this by saving and restoring the values of 
certain commands and paramters just before and after processing the row of sucessors.

There are some special features whose implementation is incorporated into the \verb=makecanonical= and \verb=build= 
commands, for efficiency:
\begin{compactdesc}
\item[Adjust bounding boxes]
  Bounding boxes are only adjusted for nodes or for a tree that is an outer or root object or that changes 
  directions. Such trees are first made into nodes, via \Lcs{ptr@makecanonical@outer}, and it is at the end of 
  this command that bounding box adjustment is invoked. The bounding box adjustment is invoked by nodes just 
  before \Lcs{psnode@makecanonical}.

\item[Show bounding boxes]
 The show bounding box commands are invoked as follows:
 \begin{compactitem}
   \item For nodes, just after the bounding box adjustment.
   \item For trees that invoke \Lcs{ptr@makecanonical@outer}, just after the bounding box adjustment.
   \item For subtrees that have the same \Lkeyword{treemode} as their parent, at the beginning 
    of the \Lcs{ptr@makecanonical@succ} command.
 \end{compactitem}

\item[Skip levels]
 The commands for finding the amount of space to be skipped and the profiles of the skipped levels are invoked 
 at the beginning of the tree macros, or in \Lcs{psnode@makecanonical@succ}. The adjustment of the box and profiles 
 takes place in \Lcs{ptr@build@succ}.
\end{compactdesc}



\clearpage
\section{List of all optional arguments for \texttt{pst-thick}}

\xkvview{family=pst-tvz,columns={key,type,default}}

\nocite{*}
\bgroup
\RaggedRight
\bibliographystyle{plain}
\bibliography{pst-tvz-doc}
\egroup

\printindex

\end{document}