I'd like to draw trees like the following:
desired tree display http://quicklatex.com/cache3/ql_c0f6b5397c19caf09ce70d6e47841eb7_l3.png
That is, I have edges which are crossing. The tree should be drawn like a regular tree (without crossings) except at the last level where the leaves can be in any horizontal order. The number of nodes etc. is arbitrary, the image is just an example.
The only thing I know about the tree is (a) its structure, that is, nodes and their children, and (b) an ordering number (=x position) for each leaf which determines where the leaves should be displayed horizontally to form the crossings. In the example above, I would know a1
is at position 0, b1
at 1, a2
at 2, and b2
at 3. I can use these numbers when I draw a specific leaf.
The code must represent the structure of the tree, like that:
\documentclass[a4paper]{scrartcl}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
\tikzset{frontier/.style={distance from root=70pt}}
\Tree [.Z [.\node{A};
\node{a1};
\node{a2}; ]
[.\node{B};
\node{b1};
\node{b2}; ] ]
\end{tikzpicture}
\end{document}
which produces:
non-crossing tree display http://quicklatex.com/cache3/ql_4217cf1d35018cf2016b55ac580d6085_l3.png
Ideally, now I'd like to reorder the leaves based on their ordering numbers that I know:
\Tree [.Z [.\node{A};
\node at (0,0) {a1};
\node at (2,0) {a2}; ]
[.\node{B};
\node at (1,0) {b1};
\node at (3,0) {b2}; ] ]
The problem is that this becomes:
wrong tree http://quicklatex.com/cache3/ql_8b543330e2e251476b91100f13dcca4a_l3.png
Question: How can the repositioning of the leaves be achieved without loosing the automatic tree layout functionality of tikz-qtree
?
Some background: These are derivation trees generated recursively by so-called multiple context-free grammars which can model languages like a^i b^j a^i b^j. I appended numbers to the terminals to make it more explicit where they end up when reordered.