我有一棵三层的树,想从第 1 层到第 3 层绘制边。我使用的是“从父节点开始的边”,但想使用类似“到祖父节点的边”之类的东西。到目前为止,我得到的结果呈 Y 形,这还算不错,但我更喜欢两条直线而不是“祖父节点”。
有什么办法可以得到“祖父母”选项吗?
\documentclass{article}
\usepackage{amssymb, amsmath, amstext}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1]
\tikzstyle{every node}=[circle,draw,inner sep=1pt]
\tikzstyle{level 1}=[sibling distance=60mm]
\tikzstyle{level 2}=[sibling distance=35mm,set style={{every node}+=[circle]}]
\tikzstyle{level 3}=[sibling distance=20mm,set style={{every node}+=[circle]}]
\node{}[grow'=south]
child {node{$\overline{G}$}
child{node[label={right:}]{$\overline{A}$}
child{node[label={right:}]{$\overline{E}$}
edge from parent
node[ right,draw=none,fill=none]{~$1$}
}
edge from parent
node[ right,draw=none,fill=none]{~$0.35$}
}
child{node[label={right:}]{$A$}
child{node[label={right:}]{$\overline{E}$}
edge from parent
node[ right,draw=none,fill=none]{~$0.7$}
}
child{node[label={right:}]{E}
edge from parent
node[ left,draw=none,fill=none]{$0.3$~~}
}
edge from parent
node[ left,draw=none,fill=none]{$0.65$~~}
}
edge from parent
node[above right,draw=none,fill=none]{$0.87$}
}
child {node[label={}]{$G$}
child{
child{node[label={below:}]{$\overline{E}$}
edge from parent
node[ right,draw=none,fill=none]{~$0.996$}
}
child{node[label={right:}]{$E$}
edge from parent
node[ left,draw=none,fill=none]{$0.004$~~}
}
edge from parent
%node[ left,draw=none,fill=none]{$0.65$~~}
}
edge from parent
node[above left,draw=none,fill=none]{$0.13$}
}
;
\end{tikzpicture}
\end{document}
答案1
仅供比较,这里是使用 的树forest
,它提供了tier
选项。
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
draw, circle, inner sep=1pt, l sep=7mm, s sep=1cm,
if n children=0{tier=leaf}{}
}
[
[$G$, edge label={node[midway, above left]{0.13}}
[$E$, edge label={node[midway, left]{0.004}}]
[$\overline{E}$, edge label={node[midway, right]{0.996}}]
]
[$\overline{G}$, edge label={node[midway, above right]{0.87}}
[$A$, edge label={node[midway, left]{0.65}}
[$E$, edge label={node[midway, left]{0.3}}]
[$\overline{E}$, edge label={node[midway, right]{0.7}}]
]
[$\overline{A}$, edge label={node[midway, right]{.35}}
[$\overline{E}$, edge label={node[midway, right]{1}}]
]
]
]
\end{forest}
\end{document}