我有一个堆,我想显示每个节点旁边的节点数。我认为计数器可能有用,或者使用某种方法,\foreach
但我不确定如何实现。
我目前拥有的:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{shapes.misc}
\tikzset{
heap/.style={
every node/.style={circle,draw},
level 1/.style={sibling distance=65mm, level distance=10mm},
level 2/.style={sibling distance=35mm},
level 3/.style={sibling distance=20mm},
level 4/.style={sibling distance=8mm}
},
cutEdgeToLeftChild/.style={strike out,draw,pos=0.5,line width=1pt, minimum height=5mm,rotate=80},
cutEdgeToRightChild/.style={strike out,draw,pos=0.5,line width=1pt, minimum height=5mm,rotate=150}
}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture} [heap]
\node (a) {}
child {node (b) {}
child {node (c) {}
child {node (d) {}
child {node {}}
child {node (e) {}}
}
child {node {}}
}
child {node {}
child {node {}}
child {node {}}
}
}
child {node {}
child {node {}
child {node {}}
child {node {}}
}
child {node {}
child {node {}}
child {node {}}
}
}
;
\node[draw=none] at (-6,0) {\(k\text{-}heap\)};
\node[draw=none] at (6,-5) {\(D_k = \{8, 4, 2, 2, 1\}\)};
\path (a) -- (b) node[cutEdgeToLeftChild] {};
\path (b) -- (c) node[cutEdgeToLeftChild] {};
\path (c) -- (d) node[cutEdgeToLeftChild] {};
\path (d) -- (e) node[cutEdgeToRightChild] {};
\end{tikzpicture}
\end{figure}
\end{document}
更新:我需要从上到下、从左到右进行编号(因为堆会被填满)。
答案1
欢迎!这就像做这样的事。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\newcounter{heapno}
\tikzset{autonumber/.code={\stepcounter{heapno}%
\tikzset{alias=heap-\number\value{heapno}}},
heap/.style={
nodes={circle,draw,autonumber},
level 1/.style={sibling distance=65mm, level distance=10mm},
level 2/.style={sibling distance=35mm},
level 3/.style={sibling distance=20mm},
level 4/.style={sibling distance=8mm}
},
cutEdgeToLeftChild/.style={strike out,draw,pos=0.5,line width=1pt, minimum height=5mm,rotate=80},
cutEdgeToRightChild/.style={strike out,draw,pos=0.5,line width=1pt, minimum height=5mm,rotate=150}
}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{scope}[heap]
\node (a) {}
child {node (b) {}
child {node (c) {}
child {node (d) {}
child {node {}}
child {node (e) {}}
}
child {node {}}
}
child {node {}
child {node {}}
child {node {}}
}
}
child {node {}
child {node {}
child {node {}}
child {node {}}
}
child {node {}
child {node {}}
child {node {}}
}
}
;
\end{scope}
\path foreach \X in {1,...,\number\value{heapno}}
{(heap-\X) node[below=1ex,font=\tiny]{\X}};
\node[draw=none] at (-6,0) {\(k\text{-}heap\)};
\node[draw=none] at (6,-5) {\(D_k = \{8, 4, 2, 2, 1\}\)};
\path (a) -- (b) node[cutEdgeToLeftChild] {};
\path (b) -- (c) node[cutEdgeToLeftChild] {};
\path (c) -- (d) node[cutEdgeToLeftChild] {};
\path (d) -- (e) node[cutEdgeToRightChild] {};
\end{tikzpicture}
\end{figure}
\end{document}
但是,我相信如果你改用 ,情况会更好forest
。这列出了枚举方案(请参阅手册第 52 页)。
\documentclass{article}
\usepackage{amsmath}
\usepackage{forest}
\forestset{enumerate/.style={tempcounta=1,
for #1={
label/.pgfmath=tempcounta,
tempcounta+=1
}}}
\begin{document}
\foreach \X in {tree,tree reversed,tree children-first,tree children-first
reversed,tree breadth-first,tree breadth-first reversed}
{\subsection*{\X}
\begin{forest}
delay={enumerate=\X},
for tree={circle,inner sep=0pt,minimum size=1.5em,draw,s sep+=1em}
[
[
[
[
[]
[]
]
[]
]
[
[]
[]
]
]
[
[
[]
[]
]
[
[]
[]
]
]
]
\end{forest}}
\end{document}
根据您的描述,您可能需要
\documentclass{article}
\usepackage{amsmath}
\usepackage{forest}
\forestset{enumerate/.style={tempcounta=1,
for #1={
label/.pgfmath=tempcounta,
tempcounta+=1
}},
strike/.style={edge label={pic[midway,sloped]{strike}}}}
\tikzset{pics/strike/.style={code={\draw[thick] (90:0.4) -- (-90:0.4);}},}
\begin{document}
\begin{forest}
delay={enumerate=tree breadth-first},
/tikz/every label/.append style={font=\small\sffamily},
for tree={circle,inner sep=0pt,minimum size=1.5em,draw,s sep+=1em,l sep+=1em}
[
[,strike
[,strike
[,strike
[]
[,strike]
]
[]
]
[
[]
[]
]
]
[
[
[]
[]
]
[
[]
[]
]
]
]
\path
(current bounding box.north west) node[font=\itshape,below right] {k-heap}
(current bounding box.south east) node[font=\itshape,below left]
{\(D_k = \{8,4, 2, 2, 1\}\)};
\end{forest}
\end{document}