我正在尝试绘制一个结构如下的决策树:
这是我目前所拥有的:
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
[
grow = right,
sibling distance = 6em,
level distance = 10em,
edge from parent/.style = {draw, -latex},
every node/.style = {font=\footnotesize},
%sloped
]
\node{root}
child {node [env] {}}
child {node [dummy] {}
child {node [dummy] {child 1}
child{node [env] {grandchild 1}}
child{node [env] {grandchild 2}}
}
child {node [dummy] {child 2}
child {node [env] {grandchild 1}}
child {node [env] {grandchild 2}}
}}
\end{tikzpicture}
\end{document}
也就是说,两个边(来自子节点 1 和子节点 2)与孙节点 3 相连,而孙节点 2 缺失。有人知道如何修复这个问题吗?
答案1
- 使用
tikz
你需要重新定义sibling distance
- 使用森林,你只需要确定节点之间的距离
\documentclass[tikz,
border=3mm,
many,
preview
]{standalone}
\usepackage{forest}
\usetikzlibrary{shapes,
trees}
\begin{document}
\begin{tikzpicture}
[
grow = right,
level/.style = {sibling distance= 33mm/#1},
level distance = 6em,
edge from parent/.style = {draw, -latex},
every node/.style = {font=\footnotesize},
%
env/.style = {circle, draw},
dummy/.style = {draw, rounded corners,
top color=blue!10, bottom color=blue!60}
]
\node{root}
child {node [env] {}}
child {node [env] {}
child {node [env] {child 1}
child{node [dummy] {grandchild 1}}
child{node [dummy] {grandchild 2}}
}
child {node [env] {child 2}
child {node [dummy] {grandchild 1}}
child {node [dummy] {grandchild 2}}
}};
\end{tikzpicture}
\begin{forest}
for tree = {
font = \small,
grow' = 0,
l sep = 3em,
s sep = 2ex,
if level = 3{draw, rounded corners,
top color=blue!10, bottom color=blue!60,
anchor=west}%
{if level>=1{circle, draw}{s sep=12ex}},
calign=edge midpoint,
}
[root
[
[child 1
[grandchild 1]
[grandchild 2]
]
[child 2
[grandchild 3]
[grandchild 4]
]
]
[]
]
\end{forest}
\end{document}