我正在尝试绘制一棵树,但无法让它跳过一层。这是我的代码。
\documentclass[border=1cm]{standalone}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
[every tree node/.style={draw,circle},
level distance=1.25cm,sibling distance=1cm,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node {};
\edge node[auto=right] {1};
[.\node{};
\edge node[auto=right] {1};
[.\node{};
\edge node[auto=right] {1};
[.\node{apple};]
\edge node[auto=left] {0};
[.\node {pear}; ]
]
\edge node[auto=left] {0};
[.\node{};
\edge node[auto=right] {1};
[.\node{bike};]
\edge node[auto=left] {0};
[.\node {car}; ]
]
]
\edge node[auto=left] {I WANT THIS EDGE TO GO STRAIGHT TO};
[
\edge node[auto=right] {};
[.\node{HERE};
\edge node[auto=right] {1};
[.\node{walk};]
\edge node[auto=left] {0};
[.\node {run}; ] ] ]
]
\end{tikzpicture}
\end{document}
这是代码生成的图像。
答案1
你可以:
- 删除边缘(使用选项
draw=none
), - 标记你想要在其间建立边的两个节点(我称它们为
foo
和),并且qux
- 手动绘制边缘。
结果:
代码:
\documentclass[border=1cm]{standalone}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
[every tree node/.style={draw,circle},
level distance=1.25cm,sibling distance=1cm,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node (foo) {};
\edge node[auto=right] {1};
[.\node{};
\edge node[auto=right] {1};
[.\node{};
\edge node[auto=right] {1};
[.\node{apple};]
\edge node[auto=left] {0};
[.\node {pear}; ]
]
\edge node[auto=left] {0};
[.\node{};
\edge node[auto=right] {1};
[.\node{bike};]
\edge node[auto=left] {0};
[.\node {car}; ]
]
]
\edge[draw=none] node[auto=left] {};
[
\edge[draw=none] node[auto=right] {};
[.\node (qux) {};
\edge node[auto=right] {1};
[.\node{walk};]
\edge node[auto=left] {0};
[.\node {run}; ] ] ]
];
\draw (qux) -- (foo) node[midway,auto=right] {0};
\end{tikzpicture}
\end{document}
答案2
没有办法完全按照您的需要绘制树枝;tikz-qtree
它不是为绘制那样的树而设计的。但是您可以使用它来实现类似的结果,frontier/.style
它允许您将终端节点设置为与根节点的固定距离。为了做到这一点,您需要[ ]
从终端节点中删除(这样它就tikz-qtree
知道它们是终端节点。)我还添加了一些代码,使带有文本的圆圈节点大小统一。
\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
[every internal node/.style={draw,circle,inner sep=1.5pt},every leaf node/.style={draw, circle, minimum size=1.cm,inner sep=0pt},
level distance=1.25cm,sibling distance=1cm,frontier/.style={distance from root=3.5cm},
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node{};
\edge node[auto=right] {1};
[.\node{};
\edge node[auto=right] {1};
[.\node{};
\edge node[auto=right] {1};
\node{apple};
\edge node[auto=left] {0};
\node {pear};
]
\edge node[auto=left] {0};
[.\node{};
\edge node[auto=right] {1};
\node{bike};
\edge node[auto=left] {0};
\node {car};
]
]
\edge node[auto=left] {0};
[.\node{};
\edge node[auto=right] {1};
\node{walk};
\edge node[auto=left] {0};
\node {run}; ] ]
]
\end{tikzpicture}
\end{document}
答案3
使用forest
,您可以简单地用来tier=<tier name>
对齐您想要的节点。
例如:
\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
circle,
draw,
inner sep=1pt,
minimum size=5pt,
if n=1{
edge label={node [above left, midway] {1}}
}{
if n'=1{
edge label={node [above right, midway] {0}}
}{}
}
}
[
[
[, tier=align me
[apple]
[pear]
]
[
[bike]
[car]
]
]
[, tier=align me
[walk]
[run]
]
]
\end{forest}
\end{document}