我在使用 TikZ 时遇到了分支限界树中节点重叠的问题。
这是我的代码:
\documentclass[
12pt,
oneside,
a4paper,
english,
french,
spanish,
brazil
]{abntex2}
\usepackage{graphicx}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{calc, shapes,positioning}
\usepackage[lmargin=2.5cm,rmargin=2.5cm,tmargin=2.5cm,bmargin=2.5cm]{geometry}
\begin{document}
\begin{figure}[H]
\centering
\tikzset{thick,
tree node/.style = {align=center, inner sep=0pt, font = \footnotesize},
every label/.append style = {font=\footnotesize},
PL/.style = {draw, circle, minimum size = 10mm, inner sep=0pt,
top color=white, bottom color=blue!20,align=center},
ENL/.style = {% edge node left
font=\footnotesize, left=1pt},
ENR/.style = {% edge node right
font=\footnotesize, right=1pt},
grow = down,
sibling distance = 8cm,
level distance = 2cm
}
\fbox{
\begin{tikzpicture}[every node/.style={font=\scriptsize}]
\node [PL] {$1$}
child{node [PL] {$2$}
child{node [PL] {$3$}
child{node [PL] {$4$}}
child{node [PL] {$5$}}}
child{node [PL] {$6$}
child{node [PL] {$7$}}
child{node [PL] {$8$}}}
}
child{node [PL,] {$9$}
};
\end{tikzpicture}
}
\caption{Árvore de busca do exemplo numérico.}
\end{figure}
\end{document}
因此,你会得到这样的图片:
如您所见,节点 7 与节点 5 重叠,我该如何解决这个问题?
答案1
您可以使它sibling distance
依赖于级别,请参阅 pgfmanual v3.1.4 第 330 页的第二个示例。例如,您可以添加
level/.style={sibling distance={8cm/max(2,#1)}}
到的选项tikzpicture
:
\documentclass[
12pt,
oneside,
a4paper,
english,
french,
spanish,
brazil
]{abntex2}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{calc, shapes,positioning}
\usepackage[lmargin=2.5cm,rmargin=2.5cm,tmargin=2.5cm,bmargin=2.5cm]{geometry}
\begin{document}
\begin{figure}[H]
\centering
\tikzset{thick,
tree node/.style = {align=center, inner sep=0pt, font = \footnotesize},
every label/.append style = {font=\footnotesize},
PL/.style = {draw, circle, minimum size = 10mm, inner sep=0pt,
top color=white, bottom color=blue!20,align=center},
ENL/.style = {% edge node left
font=\footnotesize, left=1pt},
ENR/.style = {% edge node right
font=\footnotesize, right=1pt},
grow = down,
level distance = 2cm
}
\fbox{
\begin{tikzpicture}[every node/.style={font=\scriptsize},
level/.style={sibling distance={8cm/max(2,#1)}}]
\node [PL] {$1$}
child{node [PL] {$2$}
child{node [PL] {$3$}
child{node [PL] {$4$}}
child{node [PL] {$5$}}}
child{node [PL] {$6$}
child{node [PL] {$7$}}
child{node [PL] {$8$}}}
}
child{node [PL,] {$9$}
};
\end{tikzpicture}
}
\caption{\'Arvore de busca do exemplo num\'erico.}
\label{fig:tree}
\end{figure}
\end{document}
使用该max
函数可以确保只有级别大于 2 时距离才会减小。
答案2
如果您使用包绘制树,则可以避免您的问题forest
:
\documentclass[12pt,oneside,a4paper,
english,french,spanish,brazil]{abntex2}
\usepackage[margin=2.5cm]{geometry}
\usepackage{forest}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{figure}[ht]
\centering
\begin{forest}
for tree = {
% node style
draw, circle,
minimum size = 10mm,
inner sep=0pt,
top color=white, bottom color=blue!20,
font=\scriptsize,
math content,
% tree style
s sep=6mm,
l sep=12mm,
% frame around tree
tikz+={\tikzset{background rectangle/.style={draw, ultra thin},
show background rectangle}}
}
% tree
[1
[2
[3
[4]
[5]
]
[6
[7]
[8]
]
]
[9,fit=band]
]
\end{forest}
\caption{\'Arvore de busca do exemplo num\'erico.}
\label{fig:tree}
\end{figure}
\end{document}
Forest 是一个基于 pgf/TikZ 的包,用于绘制语言树(和其他类型)树。
答案3
这是一张简单的图片。你有一支笔可以画,你可以跟着笔头画,随心所欲。这是简单的 TikZ 绘图。
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[yscale=1.5,
n/.style={circle,draw,fill=yellow!50, minimum size=6mm}]
\path
(0,0) node[n] (1) {1}
+(1,-1) node[n] (9) {9}
++(-1,-1) node[n] (2) {2}
+(1,-1) node[n] (6) {6}
+(-1,-1) node[n] (3) {3}
(3)
+(-.5,-1) node[n] (4) {4}
+(.5,-1) node[n] (5) {5}
(6)
+(-.5,-1) node[n] (7) {7}
+(.5,-1) node[n] (8) {8};
\draw (1)--(2)--(3)--(4)
(1)--(9) (2)--(6)--(8) (3)--(5) (6)--(7);
\end{tikzpicture}
\end{document}