我正在画一棵树,下面是我的成果。它工作正常,但我希望 8 是 5 的左子节点(不直接画在它下面),10 是 8 的右子节点(不直接画在它下面)。我该怎么做?
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[sibling distance=8em,
every node/.style = {shape=circle,
draw, align=center}]]
\node {1}
child { node {3}
child { node {9}}
child { node {7}}}
child { node {5}
child { node {8}
child { node {10}
child { node {12}}
child { node {15}
child { node {20}}
child {node {18}
}}}}};
\end{tikzpicture}
答案1
欢迎来到 TeX.SE!!
你要这个:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
level 1/.style ={sibling distance=12em},
level 2/.style ={sibling distance=6em},
every node/.style = {shape=circle,
draw, align=center}]
]
\node {1}
child { node {3}
child { node {9}}
child { node {7}}}
child { node {5}
child { node {8}
child {edge from parent[draw=none]} % Added
child { node {10}
child { node {12}}
child { node {15}
child { node {20}}
child {node {18}
}}}}
child {edge from parent[draw=none]} % Added
};
\end{tikzpicture}
\end{document}
由于左对齐为 8,它与@Zarko 的答案不同。
答案2
第一个版本: 嗯,看到@manooooh 的回答 (+1) 后,我意识到我误读了这个问题,当然得到的树也是错误的(正确答案请参见@manooooh 的回答)
第二个版本
这棵树可以用forest
包简单绘制。希望这次设计的树是正确的:
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree = {draw,
shape=circle,
inner sep=0pt,
minimum size=1.6em,
l sep=6mm,
s sep=9mm
}
[1
[3
[9]
[7]
]
[5
[8
[,phantom]
[10
[12]
[15
[20]
[18]
]
]
]
[,phantom]
]
]
\end{forest}
\end{document}