我不太清楚如何将两个非子节点连接在一起。如能提供任何帮助,我将不胜感激。
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{graph}{2.5in}
\centering
\begin{tikzpicture}[
level distance = 2.5cm,
level 1/.style = {sibling distance=4.0cm},
level 2/.style = {sibling distance=5.5cm},
level 3/.style = {sibling distance=5.0cm},
every node/.style = {circle,draw},
lbl/.style = {rectangle, draw=none, #1,% position
font=\footnotesize}
]
\node (Root) {Start}
child {node {Node 1}
child {node {Node 4}
child {node {Goal 1}
edge from parent node[lbl=left] {$3$}
}
edge from parent node[lbl=left] {$3$}
}
edge from parent node[lbl=left] {$3$}
}
child { node {Node 2}
child{ node {Goal 2}
edge from parent node[lbl=left] {$12$}}
edge from parent node[lbl=left] {$12$}
}
child { node {Node 3}
child { node {Node 6}
child {node {Node 8}
child {node {Goal 3}
edge from parent node[lbl=right] {$1$}}
edge from parent node[lbl=right] {$1$}}
edge from parent node[lbl=right] {$1$}}
edge from parent node[lbl=right] {$1$}
};
\end{tikzpicture}
\end{graph}
我想将目标 2 连接到节点 4。
答案1
节点会自动命名。这使得连接它们变得简单直接。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[
level distance = 2.5cm,
level 1/.style = {sibling distance=4.0cm},
level 2/.style = {sibling distance=5.5cm},
level 3/.style = {sibling distance=5.0cm},
every node/.style = {circle,draw},
lbl/.style = {rectangle, draw=none, #1,% position
font=\footnotesize}
]
%
\node (Root) {Start}
child {node {Node 1}
child {node {Node 4}
child {node {Goal 1}
edge from parent node[lbl=left] {$3$}
}
edge from parent node[lbl=left] {$3$}
}
edge from parent node[lbl=left] {$3$}
}
child { node {Node 2}
child{ node {Goal 2}
edge from parent node[lbl=left] {$12$}}
edge from parent node[lbl=left] {$12$}
}
child { node {Node 3}
child { node {Node 6}
child {node {Node 8}
child {node {Goal 3}
edge from parent node[lbl=right] {$1$}}
edge from parent node[lbl=right] {$1$}}
edge from parent node[lbl=right] {$1$}}
edge from parent node[lbl=right] {$1$}
};
\draw (Root-1-1) -- (Root-2-1) node[midway,above,lbl]{$7$};
\end{tikzpicture}
\end{document}