[Tex/LaTex] How to draw a tree diagram with 90º degrees branches

foresttikz-qtreetikz-treestrees

I'm trying to replicate the following tree in LaTeX:
enter image description here

I'm having problems, because (1) I never drew a tree diagram with 90º degrees branches, like this, and (2) I never drew a tree diagram together with numbered examples.
I tried using tikz-qtree and forest, but I didn't get anywhere.
So I tried using tabbing environment to solve the numbered exemples problem, but now I don't know what package to use to draw the lines.
This is my current (and horribly written) code:

\documentclass[]{article}
\begin{document}
\begin{tabbing}
\hspace{0em} $(20)$ \= \hspace{2em} $n$ \= \hspace{1em} $s/(n)[n]$ \= 
\hspace{1em} $n/[s]$ \= \hspace{1em} $n$ \= \hspace{1em} $s/(n)$ \= 
\hspace{1em} $s/(n)//(s/(n))$ \= \hspace{1em} $s/(n)//(s/(n))[n]$ \= 
\hspace{1em} $n$ \= \hspace{1em} $n/(n)$\\~\\
\hspace{0em} $(21)$\= \hspace{37.5em} $n$\\~\\
\hspace{0em} $(22)$\= \hspace{17.5em} $s/(n)$\\~\\
\hspace{0em} $(23)$\= \hspace{30em} $s/(n)$\\~\\
\hspace{0em} $(24)$\= \hspace{20,5em} $s$\\~\\
\hspace{0em} $(25)$\= \hspace{14em} $n$\\~\\
\hspace{0em} $(26)$\= \hspace{6em} $s$\\
\end{tabbing}
\end{document}

Resulting in:
enter image description here
Can anyone give me some help on how to resolve this nightmare I'm having to replicate the tree diagram of the first image?

Best Answer

Here's a version using forest.

\documentclass{article}
\usepackage[edges]{forest}

\forestset{
declare count register=labelcount,
enumerate/.style={labelcount=#1,
for tree={content/.pgfmath=labelcount, labelcount-=1},
}
}

\begin{document}

\begin{forest}for tree={grow=north,forked edge,math content}
[
[s,no edge
    [n
        [s
            [s/(n)
                [n,tier=21
                    [n/(n), tier=top]
                    [n,tier=top]] 
                [{s/(n)//(s/(n))[n]},tier=top ]
                [s/(n)
                    [s/(n)//(s/(n))),tier=top ]
                    [s/(n),tier=top ]
                ]
            ]
            [n,tier=top]
         ]
        [{n/[s]},tier=top ]
    ]
    [{s/(n)[n]},tier=top ]
    [n,tier=top ]
]
[,for tree=no edge,delay={enumerate={26},for tree={content={(#1)}}}
    [[[[[,tier=21 [,tier=top ]]]]]]]
]
\end{forest}

\end{document}

output of code

Explanation of the code

  • We use the edges library from forest to make easy square branches.
  • We use grow=north to make the tree upside down.
  • The use of the tier key allows all the terminal nodes of the tree to be aligned at the top of the tree. It is also used to separate line (21) from (22) since in the tree they are at the same level of embedding, but in numbering they are not. By aligning the nodes in the tree with the nodes in the numbering we achieve the desired result.
  • The numbering is done with what is essentially a phantom root node that dominates both the tree and the numbers.
  • The numbers are calculated semi-automatically by giving the number for the root node using the enumerate=... key and counting down from there. Again, the tier key is used keep the alignment correct.
Related Question