我正在尝试对齐各种堆以形成斐波那契堆。我正在使用包tikz
,并使用begin{scope}
后面的指令begin{tikzpicture}
来对齐各种堆。我设法对齐了其中 3 个,但出于某种原因,当我尝试对齐其中 4 个时,却无法对齐。以下是斐波那契堆的代码(Overleaf 说我以 结尾\begin{tikzpicture}
,end{scope}
但我不这么认为):
\begin{center}
\begin{tikzpicture}
[
level distance=10mm,
level 1/.style={sibling distance=2mm},
thick, every node/.style = {draw, circle,inner sep=0pt,minimum size=1.7em},
min/.style={circle, draw=blue!60, fill=blue!20, very thick, minimum size=7mm},
]
\node[min]{5}
\begin{scope}
[
xshift=3.5cm,
level 1/.style={sibling distance=24mm}
]
\node[root]{11}
child{node{70}};
\begin{scope}
[
xshift=4.5cm,
level distance=10mm,
level 1/.style={sibling distance=23mm},
level 2/.style={sibling distance=14mm},
level 3/.style={sibling distance=11mm},
level 4/.style={sibling distance=8mm},
thick, every node/.style = {draw, circle,inner sep=0pt,minimum size=1.7em},
root/.style={circle, draw=blue!60, fill=blue!5, very thick, minimum size=7mm},
min/.style={circle, draw=blue!60, fill=blue!20, very thick, minimum size=7mm},
marked/.style={circle, draw=red!60, fill=red!5, very thick, minimum size=7mm}
]
\node[root]{7}
child{node[marked]{15}
child{node[marked]{30}
child{node{61}}}
child{node{20}}}
child{node{9}
child{node{11}}}
child{node{13}};
\begin{scope}
[
xshift=3.5cm,
level 1/.style={sibling distance=24mm},
level 2/.style={sibling distance=12mm}
]
\node[root]{8}
child{node{12}};
\end{scope}
\end{scope}
\end{scope}
\end{tikzpicture}
\end{center}
我希望它看起来像这样(但顶部有更多节点与其他节点对齐):
感谢您的帮助!
答案1
有了 这可能会更容易forest
。
\documentclass{article}
\usepackage{forest}
\tikzset{root/.style={circle, draw=blue!60, fill=blue!5, very thick, minimum size=7mm},
min/.style={circle, draw=blue!60, fill=blue!20, very thick, minimum size=7mm},
marked/.style={circle, draw=red!60, fill=red!5, very thick, minimum size=7mm}
}
\begin{document}
\begin{forest}
for tree={
if level=1{no edge}{},
if level=0{}{draw},
if n children=0{fit=band}{},
circle, inner sep=0pt, minimum size=1.7em,
tier/.pgfmath=level(), s sep=.5cm
}
[
[11, min
[70]
]
[7, root
[15, marked
[30, marked[61][5]][20]]
[9[11]][13]
]
[8, min
[12]
]
]
\end{forest}
\end{document}