我想画一棵如下图所示的二叉树。不幸的是,我完全不知道如何从上到下画两棵树以匹配它们的叶子。任何帮助都将不胜感激。
答案1
一种选择:
代码:
\documentclass{article}
\usepackage{forest}
\usetikzlibrary{positioning,arrows.meta}
\newsavebox\Downtree
\newsavebox\Uptree
\tikzset{
nleft/.style={text width=15pt,midway,left,font=\strut\scriptsize},
nright/.style={text width=15pt,midway,right,font=\strut\scriptsize},
}
\savebox\Downtree{\begin{forest}
for tree={
s sep=25pt,
l sep=20pt,
where n children=0{inner ysep=0pt}{draw,circle},
edge={->,>=latex}
}
[Y3
[Y2,edge label={node[nleft]{yes}}
[,edge label={node[nleft]{yes}}]
[,edge label={node[nright]{no}}]
]
[Y1,edge label={node[nright]{no}}
[,edge label={node[nleft]{yes}}]
[,edge label={node[nright]{no}}]
]
]
\end{forest}%
}
\savebox\Uptree{\begin{forest}
for tree={
grow'=north,
s sep=25pt,
l sep=20pt,
where n children=0{inner ysep=0pt}{draw,circle},
edge={->,>=latex}
}
[X3
[X4,edge label={node[nleft]{no}}
[,edge label={node[nleft]{no}}]
[,edge label={node[nright]{yes}}]
]
[X1,edge label={node[nright]{yes}}
[,edge label={node[nleft]{no}}]
[,edge label={node[nright]{yes}}]
]
]
\end{forest}%
}
\begin{document}
\begin{tikzpicture}[>={Latex[open]}]
\node[inner sep=0pt] (Down) {\usebox\Downtree};
\coordinate (aux);
\node[inner sep=0pt,below=0pt of Down] (Up) {\usebox\Uptree};
\begin{scope}[red,->]
\draw
([xshift=-15pt,yshift=-10pt]Down.north) to[bend right] (Down.south west);
\draw
([xshift=-15pt,yshift=10pt]Up.south) to[bend left] (Up.north west);
\end{scope}
\begin{scope}[blue,->]
\draw
([xshift=15pt,yshift=-10pt]Down.north) to[bend left] (Down.south east);
\draw
([xshift=15pt,yshift=10pt]Up.south) to[bend right] (Up.north east);
\end{scope}
\begin{scope}[green,<->]
\draw
([xshift=-2pt]Down.south)
.. controls ++(-10pt,2cm) and ++(10pt,2cm) ..
([xshift=2pt]Down.south);
\draw
([xshift=-2pt]Up.north)
.. controls ++(-10pt,-2cm) and ++(10pt,-2cm) ..
([xshift=2pt]Up.north);
\end{scope}
\end{tikzpicture}
\end{document}
我用的是forest
包来构建两棵独立的树;一棵向下生长,另一棵向上生长。首先将每棵树装箱,然后将箱子放在\node
s中,tikzpicture
以便轻松绘制红色和绿色箭头。
答案2
虽然您想绘制树木,但无需使用Tikz trees
、tikz-qtree
、forest
、 ... 来绘制它们。matrix
类似下面的代码或只需正确定位节点即可完成相同的操作。
并且下一个代码不会tikzpicture
在另一个代码中插入tikzpicture
,这有时是不推荐的。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, arrows.meta}
\begin{document}
\begin{tikzpicture}[>=latex]
\matrix (A) [matrix of nodes, nodes={circle,draw},
row 3/.style={nodes={draw=none, inner sep=0pt},nodes in empty cells},
row sep=.75cm, column sep=3mm]
{
&[4mm]&&&Y3&&&&[4mm]\\
&&Y2&&&&Y1&&\\
&&&&&&&&\\
&&X4&&&&X1&&\\
&&&&X3&&&&\\
};
\draw[->] (A-1-5)-- node[above left]{yes} (A-2-3);
\draw[->] (A-1-5)-- node[above right]{no} (A-2-7) ;
\draw[->] (A-2-3)-- node[above left]{yes} (A-3-2);
\draw[->] (A-2-3)-- node[above right]{no} (A-3-4) ;
\draw[->] (A-2-7)-- node[above left]{yes} (A-3-6);
\draw[->] (A-2-7)-- node[above right]{no} (A-3-8) ;
\draw[->] (A-5-5)-- node[below left]{no} (A-4-3);
\draw[->] (A-5-5)-- node[below right]{yes} (A-4-7) ;
\draw[->] (A-4-3)-- node[below left]{no} (A-3-2);
\draw[->] (A-4-3)-- node[below right]{yes} (A-3-4) ;
\draw[->] (A-4-7)-- node[below left]{no} (A-3-6);
\draw[->] (A-4-7)-- node[below right]{yes} (A-3-8) ;
\draw[red,->, >={Latex[open]},shorten <=3mm] (A-1-5.north) to[out=180,in=90] (A-3-1);
\draw[red,->, >={Latex[open]},shorten <=3mm] (A-5-5.south) to[out=180,in=-90] (A-3-1);
\draw[blue,->, >={Latex[open]},shorten <=3mm] (A-1-5.north) to[out=0,in=90] (A-3-9);
\draw[blue,->, >={Latex[open]},shorten <=3mm] (A-5-5.south) to[out=0,in=-90] (A-3-9);
\draw[green,<->, >={Latex[open]},shorten <=1mm, shorten >=1mm] (A-3-5) to[loop above,min distance=2cm] (A-3-5);
\draw[green,<->, >={Latex[open]},shorten <=1mm, shorten >=1mm] (A-3-5) to[loop below,min distance=2cm] (A-3-5);
\end{tikzpicture}
\end{document}