我想使用 TiKZ 绘制二叉树(更确切地说是有根二叉 DAG,但只有一次出现具有两个父节点的节点)。我设法使用标准 TiKZ 图形布局获得了一棵树:
以下是相应的代码(使用 LuaTeX 进行编译):
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing,graphdrawing.trees}
\begin{document}
\begin{tikzpicture}
\begin{scope}%
[every node/.style={draw,circle}]
\graph [fresh nodes, math nodes] {
b -> {
c ->{
b -> {
"\Lambda", "\emptyset"
},
"\emptyset"
},
a -> {
b -> {
a -> {
"\Lambda", "\emptyset"
},
a -> {
M0/a -> {
"\Lambda",
c -> {
"\Lambda", "\emptyset"
}
},
"\emptyset"
}
},
c -> {
a -> {
(M0),
"\emptyset"
},
"\emptyset"
}
}
}
};
\end{scope}
\end{tikzpicture}
\end{document}
但是,如果将节点的“左”子节点放置在其父节点下(指向图的另一部分的节点除外),并将节点的“右”子节点放置在其右侧,则树将更加清晰易读。请注意,在我的尝试中,此行为是相反的……
是否有某种方法可以通过参数化图表来获得这个?
答案1
不是完全令人满意,但我可以依靠手动定位获得我想要的布局。这是结果。我仍然对更“聪明”的解决方案感兴趣。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing,graphdrawing.trees,positioning}
\begin{document}
\begin{tikzpicture}
\begin{scope}%
[every node/.style={draw,circle},inner sep=.1em]
\graph [Cartesian placement,fresh nodes, math nodes, grow right=.8] {
rb/b -> [dashed] ra/a [x=.8] -> [dashed] rc/c [x=3.2] -> [dashed] "\emptyset" [x=3.2];
(rb) -> cb/c ->[dashed] "\emptyset";
(cb) -> b -> [dashed] "\emptyset";
(b) -> l0/"\Lambda" [x=2] ;
(ra) -> ba/b [x=1.6,y=3] ->[dashed] aaac/a [x=2.4,y=3,fill=lightgray] ->[dashed] "\emptyset" [x=2.4,y=3];
(ba) -> a [x=1.6,y=3] ->[dashed] "\emptyset" [x=1.6,y=3];
(a) -> (l0);
(aaac) -> auc/a [x=3.2,y=4] ->[dashed] c [x=3.2,y=4] ->[dashed] "\emptyset" [x=3.2,y=4];
(auc) -> (l0) ;
(c) -> (l0);
(rc) -> aaac2/a [x=4.8,y=6,fill=lightgray] ->[dashed] "\emptyset" [x=4.8,y=6];
(aaac2) -> [out=-90,in=90] (auc);
};
\end{scope}
\end{tikzpicture}
\end{document}%