绘制哈希表

绘制哈希表

想要绘制此哈希表?使用 tikzpicture 可以吗?在此处输入图片描述

我试过这里找到的代码:绘制哈希表 设计者:苏巴姆·索尼它给出了一些不错的结果,看起来就是我正在寻找的。
对于列表,不需要双重链接,只要简单相关即可,如果可能的话。
提前谢谢您。

\begin{tikzpicture}[scale=.2, >=stealth, start chain]
    \node[hashtable,on chain, inner sep=8pt] (H) {};
    \node[listnode,on chain,above right=-1.4cm and 0.7cm of H] (A) {$12$};
    \node[listnode,on chain] (B) {$99$};
    \node[listnode,on chain] (C) {$37$};
    \node[on chain,inner sep=2pt] (D) {$\emptyset$};
    \draw[*->] let \p1 = (H.center), \p2 = (H.two) in (\x1,\y2) -- (A.west);
    \draw[*->] let \p1 = (A.two), \p2 = (A.center) in (\x1,\y2) -- (B.west);
    \draw[*->] let \p1 = (B.two), \p2 = (B.center) in (\x1,\y2) -- (C.west);
    \draw[*->] let \p1 = (C.two), \p2 = (C.center) in (\x1,\y2) -- (D);
\end{tikzpicture}

找到的代码给出了类似这样的内容: 在此处输入图片描述

答案1

作为起点,但现在是完整的图像:

\documentclass[margin=3.141592]{standalone}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                backgrounds,
                chains,
                positioning,
                shapes.geometric, shapes.multipart
                }

\begin{document}
    \begin{tikzpicture}[%scale=.2,
node distance = 7mm and 4mm,
  start chain = going right, 
   arr/.style = {semithick, -Stealth},
   dot/.style = {circle, fill, inner sep=1.2pt,
                 label=left:#1},
every label/.append style = {font=\footnotesize, fill=white, align=center,
                             fill opacity=0.5, text opacity=1, 
                             inner sep=1pt},
     E/.style = {ellipse, draw, fill=#1},
  mpnh/.style = {rectangle split, rectangle split horizontal, 
                 rectangle split parts=3, draw, fill=gray!20,
                 inner sep=2pt,
                 on chain},
  mpnv/.style = {rectangle split, rectangle split parts=10,
     rectangle split part fill={gray!30,gray!10,gray!30,gray!30,gray!30,
                                gray!10,gray!30,gray!10,gray!10,gray!30},
     draw, minimum height=2ex},
   sym/.style = {yshift=-1mm},
   syp/.style = {yshift=+1mm},
                        ]
\node[mpnv, label=H] (H) 
    {\nodepart{one}     $\diagup$
     \nodepart{two}     \vphantom{$\diagup$} 
     \nodepart{three}   $\diagup$
     \nodepart{four}    $\diagup$
     \nodepart{five}    $\diagup$
     \nodepart{six}     \vphantom{$\diagup$} 
     \nodepart{seven}   $\diagup$
     \nodepart{eight}   \vphantom{$\diagup$} 
     \nodepart{nine}    \vphantom{$\diagup$} 
     \nodepart{ten}     $\diagup$
    };
%
\node[mpnh, right=of H.two east] (A1) 
   {\nodepart{one}  $\diagup$
    \nodepart{two}  $k_1$
    \nodepart{three}    \hphantom{$\diagup$}  
   };
\node[mpnh] (A2)
   {\nodepart{one}      \hphantom{$\diagup$}
    \nodepart{two}      $k_4$
    \nodepart{three}    $\diagup$
   };
%
\node[mpnh, right=of H.six east] (B1)
   {\nodepart{one}      $\diagup$
    \nodepart{two}      $k_5$
    \nodepart{three}    \hphantom{$\diagup$}
   };
\node[mpnh] (B2)
   {\nodepart{one}      \hphantom{$\diagup$}
    \nodepart{two}      $k_2$
    \nodepart{three}    \hphantom{$\diagup$}
   };
\node[mpnh] (B3)
   {\nodepart{one}      \hphantom{$\diagup$}
    \nodepart{two}      $k_7$
    \nodepart{three}    $\diagup$ 
   };
%
\node[mpnh, right=of H.eight east] (C1)
   {\nodepart{one}      $\diagup$
    \nodepart{two}      $k_3$
    \nodepart{three}    $\diagup$
   };
%
\node[mpnh, right=of H.nine east] (D1)
   {\nodepart{one}  $\diagup$
    \nodepart{two}  $k_8$
    \nodepart{three}    \hphantom{$\diagup$}
   };
\node[mpnh] (D2)
   {\nodepart{one}      \hphantom{$\diagup$}
    \nodepart{two}      $k_6$
    \nodepart{three}    $\diagup$
   };
%% arrows (right)
\draw[arr]  (H |- H.two east)   edge (A1)
            (H |- H.six east)   edge (B1)
            (H |- H.eight east) edge (C1)
            (H |- H.nine east)   to (D1)
            ;
\draw[arr, transform canvas={yshift=1mm}]  
            (A1.three north |- A1.east)  edge (A2)
            (B1.three north |- B1.east)  edge (B2)
            (B2.three north |- B2.east)  edge (B3)
            (D1.three north |- D2)   to   (D2)
            ;
\draw[arr, transform canvas={yshift=-1mm}]
            (A2.one north |- A2)  edge  (A1)
            (B2.one north |- B2)  edge  (B1)
            (B3.one north |- B3)  edge  (B2)
            (D2.one north |- D2)   to   (D1)
            ;
%% dots, ellipses
\pgfmathsetseed{3}
Explicitly sets the seed for
\foreach \i in {1,2,...,8}
    \node (k\i) [dot=$k_{\i}$] at (-33mm +40*rand,0.5*rand) {};


\scoped[on background layer]
{
\draw[fill=gray!30]  (-4,0.4) ellipse (3 and 2);
\path   (-4,1) node[label={$U$\\ (universe of keys)}] {};

\draw[fill=white]   (-4,0) ellipse (2.4 and 1);
\path   (-6,0) node[label=right:$K$\\ (actual\\ keys)] {};

\draw[arr]  (k1)    edge ([syp] H.two west)
            (k4)    edge ([sym] H.two west)

            (k2)    edge ([syp] H.six west)
            (k5)    edge (H.six west)
            (k7)    edge ([sym] H.six west)
            
            (k3)    edge (H.eight west)
            
            (k8)    edge ([syp] H.nine west)
            (k6)    edge ([sym] H.nine west)
            ;
}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容