我怎麼才能只圈出樹的最後一層(葉子)(而不是所有人)?
\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}