Come posso disegnare un diagramma ad albero in cui le foglie sono collegate a rami convergenti?

Aug 16 2020
\documentclass{article}
\usepackage{fullpage}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees}
\usetikzlibrary{arrows.meta}
\usepackage{mdframed}
\usepackage{boxedminipage}
\usepackage{forest}

\begin{document}

\begin{forest}
% Styling
for tree={
align=center,
parent anchor=south,
child anchor=north,
edge={thick, -{Stealth[]}},
l sep+=10pt,
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{
inner xsep=-15pt,
tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
[a
[b]
[c] ]
[b
[d]
[e] ]
]
\end{forest}

\end{document}

Come potrei collegare tutti i rami nel modo mostrato di seguito nello schizzo

Risposte

3 AndréC Aug 16 2020 at 16:29

Aggiornamento 2: frecce tra i nodi con specifica nodewalk

Tutte le frecce aggiuntive (non create in modo nativo dalla foresta) che collegano due nodi vengono posizionate nell'albero con la sintassi del percorso breve del nodo . Questo senza nominare i nodi come prima (un vero esercizio di stile) . Per consentire la visualizzazione di queste frecce, le ho colorate di rosso .

Cito il manuale:

Un nodewalk è un modo conciso di esprimere le relazioni tra i nodi. È semplicemente una stringa di passaggi, che sono rappresentati da singoli caratteri, dove: u sta per il nodo genitore (su); p per il fratello precedente; n per il prossimo fratello; s per il fratello (utile solo negli alberi binari); 1 , 2 ,. . . 9 per primo, secondo,. . . nono figlio; l, per l'ultimo figlio, ecc. Per la specifica completa, vedere la sezione 3.8.7 .

\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\useforestlibrary{linguistics}

\begin{document}
\begin{forest}
% Styling
for tree={
    align=center,
    parent anchor=south,
    child anchor=north,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
    edge path={\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{inner xsep=-15pt,tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
    [a
        [b]
        [c,s sep=10,
        [f,s sep=30,
            [,phantom]
            [z,edge={white}]
                {\draw[thick,red,-{Stealth[]}] (!rllN.south east) -- +(0,-10pt) -| (!c); %<-- arrow from g to z
                \draw[thick,red] (!uss.south west) -- +(0,-10pt) -| (!c) ;%<-- arrow from f to z
           }
        ]{\draw[thick,red,shorten >=3pt] (!us.south) -- +(0,-10pt) -| (!c) ;}%<-- arrow from b to f
        [,phantom]          
        ] 
    ]
    [b
        [d]
        [e,s sep=9
            [g]{\draw[thick,red,shorten >=3pt] (!us.south) -- +(0,-10pt) -| (!c) ;}%<-- arrow from d to g
            [,phantom]         
        ] 
    ]
]
\end{forest}

\end{document}

In risposta a un aggiornamento

Ho lavorato molto per ottenere questo risultato e quindi non sono sicuro di aver codificato in modo elegante. Se arriva un esperto di foreste, sto ascoltando quali miglioramenti possono essere apportati.

\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\useforestlibrary{linguistics}

\begin{document}
\begin{forest}
% Styling
for tree={
    align=center,
    parent anchor=south,
    child anchor=north,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
    edge path={\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{inner xsep=-15pt,tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
    [a
        [b,name=b]
        [c,s sep=10,
        [f,name=f,s sep=30,
            [,phantom]
            [z,name=z,edge={white}]
        ]
        [,phantom]          
        ] 
    ]
    [b
        [d,name=d]
        [e,s sep=9
            [g,name=g]
            [,phantom]         
        ] 
    ]
]
\draw[thick,shorten >=2pt] (b.south) -- +(0,-10pt) -| (f.north);
\draw[thick,shorten >=2pt] (d.south) -- +(0,-10pt) -| (g.north);
\draw[thick,shorten >=2pt] (g.south east) -- +(0,-10pt) -| (z.north);
\draw[thick,-{Stealth[]}] (f.south west) -- +(0,-10pt) -| (z.north);
\end{forest}

\end{document}

Vecchia risposta

Potrebbe esserci una soluzione più elegante, ma ecco la mia proposta.

\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\useforestlibrary{linguistics}
%\usepackage{fullpage}
%\usepackage[utf8]{inputenc}
%\usepackage{tikz}
%\usetikzlibrary{arrows,shapes,positioning,shadows,trees}
%\usepackage{mdframed}
%\usepackage{boxedminipage}


\begin{document}
\begin{forest}
% Styling
for tree={
    align=center,
    parent anchor=south,
    child anchor=north,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
    edge path={\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{inner xsep=-15pt,tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
    [a
        [b,name=b]
        [c,name=c
            [,phantom]
            [z,name=z]
        ] 
    ]
    [b
        [d,name=d]
        [e,name=e] 
    ]
]
\draw[thick,shorten >=2pt] (b.south) -- +(0,-10pt) -| (z.north);
\draw[thick,shorten >=2pt] (d.south) -- +(0,-10pt) -| (z.north);
\draw[thick,shorten >=2pt] (e.south) -- +(0,-10pt) -| (z.north);
\end{forest}

\end{document}