有人知道如何创建具有链接结构的二叉树吗?

有人知道如何创建具有链接结构的二叉树吗?

我已经访问了太多页面,但我还没有找到任何类似于 draw 或 draw 的页面

这就是我想画的

答案1

点赞节点图片

问题不太好(没有 MWE),OP 也不太主动与帮助者沟通。然而,这个图的复杂性吸引了我。我密集使用了几个锚点类似节点pic simcardsimsource,以及一个奇怪的(用户定义的)形状,如节点 pic snode。这里既没有使用,[name prefix]也没有使用, ... 。如果有更简单的方法(TikZ 或其他),我会感到惊讶。(-parent)(-children)

这种使用 s 的方式pic几乎和 s 一样灵活node。特别是,这种方式可以处理这里这里

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[>=latex]
\def\l{1.8}
\pgfmathsetmacro{\simwidth}{1+\l}

% a simcard ?
\tikzset{pics/simcard/.style args=
{nodename #1}{code={%
\path (.5-.5*\l,0) 
node[draw,fill=blue!10,rounded corners=3mm,minimum height=2cm,minimum width=\simwidth cm,outer sep=0,inner sep=0,transform shape] (#1) {};
\draw[orange] (0,1)--(0,-1) (0,0)--(-\l,0);
\draw[rounded corners=3mm] (1,1) rectangle (-\l,-1);
\fill 
(.5,0)      circle(3pt) coordinate (#1e)
(-\l/2,.5)  circle(3pt) coordinate (#1p)
(-\l/2,-.5) circle(3pt) coordinate (#1c)
(0,1) coordinate (#1N)
;
}}}

% a simsource ?
\tikzset{pics/simsource/.style args=
{nodename #1}{code={%
\path (0,0) 
node[draw,fill=blue!10,rounded corners=2mm,minimum height=9mm,minimum width=4.5cm,transform shape] (#1) {};
\fill 
(-1.5,0) circle(2pt) coordinate (#1s1) 
(-.5,0)  circle(2pt) coordinate (#1s2)
(.5,0)   circle(2pt) coordinate (#1s3)
(1.5,0)  circle(2pt) coordinate (#1s4)
;
\foreach \i in {1,...,4}
\draw (#1s\i) circle(5pt); 
}}}

% pic as node with weird (user-defined) shape and reusable anchor #1top  
\tikzset{pics/snode/.style args=
{nodename #1 nodecontent #2}{code={%
\path (0,0) node[transform shape,font=\itshape] (#1) {#2};
\path ([shift={(.2,-.05)}]#1.north) coordinate (#1top);
\draw[blue,rounded corners,fill=yellow] plot coordinates {(#1top)
([shift={(0,.1)}]#1.north east) (#1.south east) 
([shift={(-.4,-.2)}]#1.south) 
([shift={(-.1,.1)}]#1.south west) 
([shift={(.25,.1)}]#1.north west)}--cycle;
\path (0,0) node[transform shape,font=\itshape] (#1) {#2};
}}}

% the left block
\begin{scope}[local bounding box=Left block]
\path
(0,0) pic{simcard=nodename L};      
\draw[->,magenta] (Lp) to[bend left] +(0,1) node[above,black]{parent};
\draw[->,magenta] (Lc) to[bend right] +(0,-1) node[below,black]{children};
\draw[->,magenta] (Le) to[bend left] +(1,0) node[right,black]{element};
\end{scope}

\tikzset{declare function={a=1.2;b=3.5;}}
\begin{scope}[local bounding box=Right block,xshift=8cm]
\path
(-3*a,0) pic[scale=.5]{simcard=nodename A}      
(-a,0)   pic[scale=.5]{simcard=nodename B}
(a,0)    pic[scale=.5]{simcard=nodename C}
(3*a,0)  pic[scale=.5]{simcard=nodename D}      
(0,b)    pic[scale=.5]{simcard=nodename E}      
(0,.5*b) pic{simsource=nodename S}
(-2.1*a,-1.5) pic{snode=nodename Bal nodecontent Baltimore}
(-.1*a,-1.5)  pic{snode=nodename Chi nodecontent Chicago}
(2*a,-1.5)   pic{snode=nodename Pro nodecontent Providence}
(4.1*a,-1.5)  pic{snode=nodename Sea nodecontent Seattle}
(3.8*a,.7*b)  pic{snode=nodename New nodecontent New York}
;       
\end{scope}

\begin{scope}[magenta]
\draw[->] (Ss1) to[bend right] (AN);
\draw[->] (Ss2) to[bend right] (BN);
\draw[->] (Ss3) to[bend left]  (CN);
\draw[->] (Ss4) to[bend left]  (DN);

\draw[->] (Ap) to[out=120,in=180] (E.165);
\draw[->] (Bp) .. controls +(160:2) and +(180:2) .. (E.-165);
\draw[->,shorten >=-.5mm] (Cp) 
.. controls +(130:1) and +(200:.5) .. ++(1.5,.7)
.. controls +(40:2) and +(-30:1) .. (E.-30);
\draw[->] (Dp) .. controls +(60:2.5) and +(0:2) .. (E.-15);

\def\shiftpoint{(-75:1.8)}
\draw[->] (Ac) to[bend left] +\shiftpoint;
\draw[->] (Bc) to[bend left] +\shiftpoint;
\draw[->] (Cc) to[bend left] +\shiftpoint;
\draw[->] (Dc) to[bend left] +\shiftpoint;
\draw[->] (Ec) to[bend left] (S);

\draw[->] (Ep) to[bend right] +(0,1);
\draw[<-] (E.125) to[bend right] +(-1,.8);

\draw[->] (Ae) to[bend left] (Baltop);
\draw[->] (Be) to[bend left] (Chitop);
\draw[->] (Ce) to[bend left] (Protop);
\draw[->] (De) to[bend left] (Seatop);
\draw[->] (Ee) to[out=20,in=120] (Newtop);
\end{scope}

\path 
(Left block.south) node[below=3mm] (Lba) {\bfseries (a)}
(Right block.south|-Lba) node{\bfseries (b)};

\end{tikzpicture}
\end{document}

老的这是给你的建议——左边的方块。同样,你也可以画右边的方块。

我定义了一个pic命名的simcard(看起来像这样^^)。命名 apic在某种程度上是有限制的,但在这种情况下就足够了。里面pic有 3 个坐标:-element-parent-parent。因此,在命名该图片后,比如L,然后你可以将它们重新用作(L-element)L-parentL-parent

PS:您也可以将左边和右边画成独立的图形。

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{simcard/.pic={
\draw (0,1)--(0,-1) (0,0)--(-2,0);
\draw[rounded corners=3mm,very thick] (1,1) rectangle (-2,-1);
\fill 
(.5,0) circle(3pt) coordinate (-element)
(-1,.5) circle(3pt) coordinate (-parent)
(-1,-.5) circle(3pt) coordinate (-children)
;
}}

% the left block
\begin{scope}[local bounding box=blockA]
\path
(0,0) pic (L) {simcard};        
\draw[-stealth,magenta] (L-parent) to[bend left] +(0,1) node[above,black]{parent};
\draw[-stealth,magenta] (L-children) to[bend right] +(0,-1) node[below,black]{children};
\draw[-stealth,magenta] (L-element) to[bend left] +(1,0) node[right,black]{element};
\end{scope}

\path (blockA.south) node[below=3mm]{(a)};
\end{tikzpicture}
\end{document} 

相关内容