我正在寻找如下图所示的树。但我不确定如何用虚线圈出树叶下方的标签。我目前得到的结果如下:
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}, ->]
\tikzstyle{every node}=[circle,draw]
\node (Root) {$\gamma_1$}
child {
node {$\gamma_2$}
child { node[fill=black!20,label=below:$\beta1$] {$\gamma_4$} }
child { node[fill=black!20,label=below:$\beta2$] {$\gamma_5$} }
child { node[fill=black!20,label=below:$\beta3$] {$\gamma_6$} }
}
child {
node[fill=red!20] {$\gamma_3$}
child { node[fill=black!20, label=below:$\beta4$] {$\gamma_7$} }
child { node[fill=black!20,label=below:$\beta5$] {$\gamma_8$} }
child { node [fill=black!20,label=below:$\beta6$]{$\gamma_9$} }
}
;
\end{tikzpicture}
谢谢您的帮助。
答案1
这是一种方法。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc} % needed <<<<<<<<<<<<<<<<<
\begin{document}
\begin{tikzpicture}[
level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}, ->]
\tikzstyle{every node}=[circle,draw]
\node (Root) {$\gamma_1$}
child {
node {$\gamma_2$}
child { node[fill=black!20,label=below:$\beta_1$, name=gamma4] {$\gamma_4$} } %changed <<<<<<<<<
child { node[fill=black!20,label=below:$\beta_2$] {$\gamma_5$} }
child { node[fill=black!20,label=below:$\beta_3$, name=gamma6] {$\gamma_6$} }%changed <<<<<<<<<
}
child {
node[fill=red!20] {$\gamma_3$}
child { node[fill=black!20, label=below:$\beta_4$, name=gamma7] {$\gamma_7$} }%changed <<<<<<<<<
child { node[fill=black!20,label=below:$\beta_5$] {$\gamma_8$} }
child { node [fill=black!20,label=below:$\beta_6$, name=gamma9]{$\gamma_9$} }%changed <<<<<<<<<
}
;
\draw[thick,dashed, rounded corners] ($(gamma4.north west)+(-1ex,-0.75)$) rectangle ($(gamma6.south east)+(1ex,-0.80)$);
\draw[thick,dashed, rounded corners] ($(gamma7.north west)+(-1ex,-0.75)$) rectangle ($(gamma9.south east)+(1ex,-0.80)$);
\end{tikzpicture}
\end{document}
更简单的是奎伯比尔贝尔`s 评论:命名标签。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[
level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}, ->]
\tikzstyle{every node}=[circle,draw]
\node (Root) {$\gamma_1$}
child {
node {$\gamma_2$}
child { node[fill=black!20,label={[name=beta1]below:$\beta_1$}] {$\gamma_4$} } %changed <<<<<<<<<
child { node[fill=black!20,label=below:$\beta_2$] {$\gamma_5$} }
child { node[fill=black!20,label={[name=beta3]below:$\beta_3$}] {$\gamma_6$} }%changed <<<<<<<<<
}
child {
node[fill=red!20] {$\gamma_3$}
child { node[fill=black!20, label={[name=beta4]below:$\beta_4$}] {$\gamma_7$} }%changed <<<<<<<<<
child { node[fill=black!20,label=below:$\beta_5$] {$\gamma_8$} }
child { node [fill=black!20,label={[name=beta6]below:$\beta_6$}]{$\gamma_9$} }%changed <<<<<<<<<
}
;
\draw[dashed, rounded corners] (beta1.north west) rectangle (beta3.south east);
\draw[dashed, rounded corners] (beta4.north west) rectangle (beta6.south east);
\end{tikzpicture}
\end{document}
答案2
只是为了比较,这里有一个使用 的选项forest
。
\documentclass{article}
\usepackage{forest}
\tikzset{leaves/.style={draw, dashed, thick, rounded corners, inner sep=2pt, fit = {#1}}}
\begin{document}
\begin{forest}
for tree={
l sep=1cm,
if n children=0{no edge}{draw, circle, minimum size=7mm, edge={->}},
if level=0{s sep+=5mm}{},
if level=2{fill=gray!30, l sep-=7mm}{}
}
[$\gamma_1$
[$\gamma_2$
[$\gamma_4$[$\beta_1$, name=A]][$\gamma_5$[$\beta_2$]][$\gamma_6$[$\beta_3$, name=B]]]
[$\gamma_3$, fill=red!30
[$\gamma_7$[$\beta_4$, name=C]][$\gamma_8$[$\beta_5$]][$\gamma_9$[$\beta_6$, name=D]]]
]
\node[leaves=(A)(B)]{};
\node[leaves=(C)(D)]{};
\end{forest}
\end{document}