我正在尝试用 LaTeX 制作这棵树我发现这个回复很有帮助本网站但我很容易被所有的括号弄糊涂了,现在我有了这个
通过如下代码实现:
\usepackage{tikz-qtree}
\usetikzlibrary{arrows.meta,bending}
\tikzset{every tree node/.style={minimum width=2em,draw,circle},
blank/.style={draw=none},
edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
level distance=1.5cm}
\begin{tikzpicture}
\Tree
[.51
[.12 ]
[.87
\edge[blank]; \node[blank]{};
\edge[.52
\edge[]; {83}
\edge[blank]; \node[blank]{};
]
[.40
\edge[]; {20}
\edge[blank]; \node[blank]{};
]
]
]
\end{tikzpicture}
我知道有人可以轻松地为我修好所有这些支架,但我更希望有另一种更简单的方法,这样我也可以自己完成我想制作的其余树木。
答案1
我将简化树布局如下:
并在绘图中使用包forest
:
\documentclass[margin=3mm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
grow=south,
circle, draw, minimum size=3ex, inner sep=1pt,
s sep=7mm
}
[51
[12
[1]
[43
[36]
]
]
[87
[52
[83]
]
]
]
\end{forest}
\end{document}
附录: 考虑卡卡弗拉克评论:
\documentclass[margin=3mm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
grow=south,
circle, draw, minimum size=3ex, inner sep=1pt,
s sep=3mm
}
[51
[12
[1]
[43
[36]
[,no edge, draw=none]
]
]
[87
[52
[,no edge, draw=none]
[83]
]
[,no edge, draw=none]
]
]
\end{forest}
\end{document}
编辑:
在上面的 MWE 中,你可以用no edge, draw=none
更短的节点选项替换它phantom
(@ Arne,谢谢你提醒我这一点)。
答案2
这是一个使用的解决方案tikz
,例如考虑下面的树
为了创建这个代码我使用了
\documentclass[border = 5pt, tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
every node/.style = {minimum width = 2em, draw, circle},
]
\node {51}
child {node {12}}
child {node {87}};
\end{tikzpicture}
\end{document}
在这个表示中,很明显12
和都是87
的子级57
。如果你想让树增长,只需继续添加子级即可
\documentclass[border = 5pt, tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
every node/.style = {minimum width = 2em, draw, circle},
level/.style = {sibling distance = 30mm/#1}
]
\node {51}
child {node {12}
child {node {1}}
child {node {43}
child {node {36}}
child {edge from parent[draw = none]}
}
}
child {node {87}
child {node {52}
child {edge from parent[draw = none]}
child {node {83}}
}
child {edge from parent[draw = none]}
};
\end{tikzpicture}
\end{document}
答案3
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[%level distance=5mm,
level 1/.style={level distance=10mm,sibling distance=24mm},
level 2/.style={level distance=10mm,sibling distance=16mm},
level 3/.style={level distance=10mm,sibling distance=16mm},
font=\scriptsize,inner sep=2pt,every node/.style={draw,circle,minimum size=3ex}]
\node {51} % root
child {node {12} % first level
child {node{1}} % 2sd level
child {node{43}
child {node{36}} % 3rd level
child[missing]
}
edge from parent }
child {node {87} % first level
child {node {52} % 2sd level
child[missing] % 3rd level
child {node{83}}
}
child[missing]
} ;
\end{tikzpicture}
\end{document}