我怎样才能只圈出树的最后一层(叶子)(而不是所有层)?
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}
\tikzset{ every node/.style={circle, draw, fill=gray!10,, minimum size=0.75cm} }
\begin{tikzpicture}[>=stealth]
\graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
{
200 -> {
5 -> { 2, 2.5 },
40 -> { 5 -> {2, 2.5 }, 8->{2,4} }
}
};
\end{tikzpicture}
\end{document}
答案1
不要将样式应用于,而是创建仅适用于选定节点的every node
新样式。circnode
或者,您可以考虑使用forest
非常简单的地方。
\RequirePackage{luatex85}
\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}
\tikzset{ circnode/.style={circle, draw, fill=gray!10,, minimum size=0.75cm} }
\begin{tikzpicture}[>=stealth]
\graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
{
200 -> {
5 -> { 2 [circnode], 2.5 [circnode]},
40 -> { 5 -> {2[circnode], 2.5[circnode] }, 8->{2[circnode],4[circnode]} }
}
};
\end{tikzpicture}
\begin{forest}
for tree={l=0.5in,s sep=5mm,edge={-stealth}},
where n children=0{circnode}{}
[200
[5
[2][2.5]
]
[40
[5
[2][2.5]
]
[8
[2][4]
]
]
]
\end{forest}
\end{document}
答案2
在阅读 Torbjorn, T. 之前,我也偶然发现了这个解决方案。
\tikzstyle{leaves}=[circle, draw, fill=gray!10, minimum size=0.75cm]
\begin{tikzpicture}[>=stealth]
\graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
{
200 -> {
5 -> { 2[leaves], 2.5[leaves] },
40 -> { 5 -> {2[leaves], 2.5[leaves] }, 8-> {2[leaves],4[leaves]} }
}
};
\end{tikzpicture}