我想画一棵与上一棵树相似的树(除了斜线节点),但我无法让线条上方的文本正确
我在
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{line} = [draw]
\begin{tikzpicture}
\node[circle,draw](a){$1,0$}
child{node[circle,draw, auto=left]{$0,6$}
child {node[circle,draw](c){$0,3$}
child {node[circle,draw](d){$0,1$}}
child {node[circle,draw](e){$0,2$}}
}
child {node[circle,draw](f){$0,3$}}
}
child{
node[circle,draw](g){$0,4$}};
\end{tikzpicture}
\end{document}
我已经尝试过了,\path [line] (a) -- node [text width=0.5cm,above] {0} (b);
但一切都乱七八糟
答案1
为了获得所需的行为,您可以使用库edge from parent
的元素tikz
trees
,该元素允许您链接到在其上放置节点的节点。
例如这将导致:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{line} = [draw]
\begin{tikzpicture}
\node[circle,draw](a){$1,0$}
child{%
node[circle,draw, auto=left]{$0,6$}
child {%
node[circle,draw](c){$0,3$}
child {%
node[circle,draw](d){$0,1$}
edge from parent
node [above left] {$0$}
}
child {%
node[circle,draw](e){$0,2$}
edge from parent
node [above right] {$1$}
}
edge from parent
node [above left] {$0$}
}
child {%
node[circle,draw](f){$0,3$}
edge from parent
node [above right] {$1$}
}
edge from parent
node[above left] {$0$}
}
child{%
node[circle,draw](g){$0,4$}
edge from parent
node [above right] {$1$}
};
\end{tikzpicture}
\end{document}
得出的结果是:
请小心将元素放在one 的edge from parent
所有 s 之后,否则你的树将被破坏。child
node