答案1
您可以使用子节点作为父节点。连接节点时可以添加标签。以下是入门指南:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds,fit}
\begin{document}
\tikzset{every node/.style={fill=yellow,draw=black,circle},
myedgestyle/.style={edge from parent/.style={thick,draw,->}}
}
\begin{tikzpicture}[level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2cm}]
\node (parent) {1}
child[myedgestyle] {node (child1) {2}
child {node (child2) {4} edge from parent node[left,draw=none,fill=none]{c}}
child {node (child3) {5} edge from parent node[right,draw=none,fill=none]{d}}
edge from parent node[left,draw=none,fill=none]{a}}
child[myedgestyle] {node (child4) {3}
child {node (child5) {6} edge from parent node[left,draw=none,fill=none]{e}}
child {node (child6) {7} edge from parent node[right,draw=none,fill=none]{f}}
edge from parent node[right,draw=none,fill=none]{b}};
% For the cyan-coloured circles and ellipses behind the nodes and children
\begin{pgfonlayer}{background}
\filldraw[cyan] (parent.center) circle (5mm);
\filldraw[cyan] (child1.center) circle (5mm);
\filldraw[cyan] (child4.center) circle (5mm);
\node[shape=ellipse,cyan,inner sep=0pt,fit={(child2) (child3)}] {};
\node[shape=ellipse,cyan,inner sep=0pt,fit={(child5) (child6)}] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
必须为每个子节点提供该edge from parent node
选项。我找不到在 中预定义它的方法\tikzset
。对于父节点,edge for parent node
必须写入后已经为其子项定义了边标签。
我还没有找到实现背景蓝色的方法。这可能与 tikzlibrary 有关backgrounds
。
编辑:所以使用这链接,我们可以调用fit
库来获取子节点的椭圆。backgrounds
库确保我们在创建节点后获得形状。