如何为每个节点添加标签,该标签每次使用时都会递增

如何为每个节点添加标签,该标签每次使用时都会递增

我有一个堆,我想显示每个节点旁边的节点数。我认为计数器可能有用,或者使用某种方法,\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}

在此处输入图片描述

相关内容