Tikz 树:两个节点覆盖相同的子节点

Tikz 树:两个节点覆盖相同的子节点

我有一棵树,我想将节点 A 和 B 连接到同一叶节点分支。我尝试寻找解决方案,但仍然无法解决。请参阅附件,其中说明了我当前的情况和所需的输出。我想寻求您的帮助。非常感谢!

\documentclass[margin=2mm]{standalone}
\usepackage{adjustbox}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shapes,positioning,shadows,trees}
\tikzset{
    basic/.style  = {draw, text width=2cm, rectangle},
    root/.style   = {basic, rounded corners=2pt, thin, align=center},
    tnode/.style = {basic, thin, rounded corners=2pt, align=center, text width=1cm},
    t2node/.style = {basic, thin, rounded corners=2pt, align=center, text width=0.5cm},
    edge from parent/.style={draw=black, edge from parent fork right}
}
\begin{document}
\begin{adjustbox}{width=\linewidth}
\begin{forest} for tree={
    grow=east,
    growth parent anchor=east,
    parent anchor=east,
    child anchor=west,
    edge path={\noexpand\path[\forestoption{edge},-, >={latex}] 
         (!u.parent anchor) -- +(5pt,0pt) |- (.child anchor)
         \forestoption{edge label};}
}
[Example, root, s sep=5mm,  l sep=4mm,
    [B, t2node
            [C,tnode
                    [D,tnode]
                    [E,tnode]
            ]
            [F,tnode
                    [G,tnode]
                    [J,tnode]
                    [K,tnode]
            ]
            [L,tnode
                   [M,tnode]
                   [N,tnode]
            ]
    ]
    [A, t2node]
]
\end{forest}
\end{adjustbox}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

这是一种方法。如果出现 cfr,您将获得更好的答案。

\documentclass[margin=2mm]{standalone}
\usepackage{adjustbox}
\usepackage[edges]{forest}
\tikzset{
    basic/.style  = {draw, text width=2cm, rectangle},
    root/.style   = {basic, rounded corners=2pt, thin, align=center},
    tnode/.style = {basic, thin, rounded corners=2pt, align=center, text width=1cm},
    t2node/.style = {basic, thin, rounded corners=2pt, align=center, text width=0.5cm},
    edge from parent/.style={draw=black, edge from parent fork right}
}
\newsavebox\CheatBox
\sbox\CheatBox{\begin{forest} 
forked edges,
for tree={
    grow=east,
    growth parent anchor=east,
    parent anchor=east,
    child anchor=west}
[Example, root, s sep=5mm,  l sep=4mm,
    [B, t2node,alias=A]
    [A, t2node,alias=B]
]
\draw (A.east) -- ++ (1ex,0) |- (B.east) coordinate[pos=0.25] (aux)
(aux) --++ (1em,0);
\end{forest}}   
\begin{document}
\begin{forest} 
forked edges,
for tree={
    grow=east,
    growth parent anchor=east,
    parent anchor=east,
    child anchor=west,
}
[\usebox\CheatBox,alias=root
    [C,tnode
            [D,tnode]
            [E,tnode]
    ]
    [F,tnode
            [G,tnode]
            [J,tnode]
            [K,tnode]
    ]
    [L,tnode
           [M,tnode]
           [N,tnode]
    ]
]
\draw (root.east) -- ++ (-1ex,0);
\end{forest}
\end{document}

在此处输入图片描述

答案2

另一种解决方案是forest使用delay和:for nodewalkprocess

\documentclass[margin=3mm]{standalone}
\usepackage[edges]{forest}

\begin{document}
    \begin{forest}
  for tree={
    draw, thin, rounded corners=2pt, minimum width=1cm,
% tree
        grow = east,
child anchor = west,
parent anchor= east,
   fork sep+ = 2pt,
      l sep+ = 3pt,
      s sep- = 3pt,
% edge
    forked edge
        }% end of for tree
[Example
    [A]
    [, coordinate, phantom, % fake parent for "Sparse Signal Estimate"?
        [, coordinate,
%---------------------------------------------------------------%
         delay={for nodewalk/.process={OOw2 {name}{edge}{%
            {fake=u,siblings}{tikz+={\path [#2] % #2: use definition for edge
            (.parent anchor) -- (B.parent anchor |- .parent anchor)
                             -- ++ (\forestoption{fork sep},0)
                             |- (#1.child anchor);}% #1 use name of node
                                }% end of tikz+
                                                        }% end of w2 option/function
                                        }% end of process
                }% end of delay
%---------------------------------------------------------------%
            [C
               [E]
               [D]
            ]
            [F, calign with current edge,
                [G]
                [J]
                [K]
            ]
            [L
               [M]
               [N]
            ]
        ]%
    ]
    [B, name=B]
]
\end{forest}
\end{document}

在此处输入图片描述

相关内容