绘制非普通二叉树

绘制非普通二叉树

一位作者给我发来了他的文章的草图,该文章专门用于描述记忆中的知识结构,这些结构由划分为子域的二叉树描述。作者使用自由形式的线连接树节点,以强调这些不是简单的二叉树。下面我展示了作者发给我的草图。这只能在纯 tikz-pgf 中完成吗,还是有更简单的方法?我将不胜感激您的提示。

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

更新:我尝试为第一个草图编写一些代码(请注意,这是我在 TikZ-PGF 中的第一个代码,我可能需要在这里使用样式)。

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[baseline=-7mm,edge from parent path= {(\tikzparentnode.south) .. controls (0,1.2) and (0,.2) .. (\tikzchildnode.north)}]
\node [circle,draw,label=left:$z_1$] {} 
    child { node {} }
    child { node {} };
\node [circle,below=7mm] {$\Sigma(z_1)$};
\end{tikzpicture}
$\oplus $
\begin{tikzpicture}[baseline=-7mm,edge from parent path= {(\tikzparentnode.south) .. controls (0,1.2) and (0,.2) .. (\tikzchildnode.north)}]
\node [circle,draw,label=right:$z_2$] {} 
    child { node {} }
    child { node {} };
\node [circle,below=7mm] {$\Sigma(z_1)$};
\end{tikzpicture}
\hspace{3mm}=
\begin{tikzpicture}[baseline=-13mm,edge from parent path= {(\tikzparentnode.south) .. controls (0,1.2) and (0,.2) .. (\tikzchildnode.north)}]
\node [circle,draw,label={[label distance=2mm]right:$z_1\oplus z_2$}] {} 
    child { node [circle,draw] {} 
        child { node {} } 
        child { node {} } }
    child { node [circle,draw] {} 
        child { node {} } 
        child { node {} } };
\node [circle,below=20mm] {$\Sigma(z_1)\oplus\Sigma(z_2)$};
\end{tikzpicture}
\caption{Sketch 1}
\end{figure}

\end{document}

不幸的是,我无法像原始草图 1 中所示的那样分离两个“域”。这部分对我来说很难的绘图已用红色圆圈标记。

在此处输入图片描述

答案1

在此处输入图片描述

我定义了一个pic对象它接受三个参数:标签、元素的宽度修改(宽度增量)以及从元素定义位置到标签的距离。元素pic包含两个节点,稍后可以引用。它们的名称由连接而成(参见代码)。右图由三个调用组成;两个较低层的定义位置帽子是顶部的两个内部节点

第二个图形使用了类似但稍微复杂一些的东西(有三个不同的pic元素)。在下面的代码中,第二个图形被注释掉了。

在此处输入图片描述

代码

\documentclass[11pt, margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, shapes.arrows}
\begin{document}

\tikzset{%
  pics/cap/.style n args={3}{% label, delta width, label distance
    code={%
      \node[draw, circle, inner sep=.7ex, outer sep=0] (-N) {};
      \path ($(-N) +(-4.5ex, -7ex) -(#2ex, 0)$)
      node[inner sep=.7ex, outer sep=.5ex] (-SW) {};
      \path ($(-N) +(4.5ex, -7ex) +(#2ex, 0)$)
      node[inner sep=.7ex, outer sep=.5ex] (-SE) {};
      \path (-N.220) edge[out=190, in=90] (-SW)
      (-N.{-40}) edge[out=-10, in=90] (-SE);
      \path ($(-N) -(0, 7ex) -(0, #3ex)$) node {#1};
    }
  },
  pics/rBranch/.style n args={3}{% label, delta width, delta height
    code={%
      \node[inner sep=.7ex, outer sep=0] (-N) {};
      \path (-N.center) ++(0, -2ex)
      node[inner sep=0, outer sep=0] (-C) {};
      \path ($(-C) +(4.5ex, -8ex) +(#2ex, 0) -(0, #3ex)$)
      node[draw, circle, inner sep=.7ex, outer sep=0,
      label={[label distance=0ex, scale=.8] 60:#1}] (-SE) {};
      \path (-N.270) edge[out=270, in=90] (-C);
      \path (-C) edge[out=-50, in=90] (-SE.90);
    }
  },
  pics/lBranch/.style n args={4}{% llabel, rlabel, delta width, delta height
    code={%
      \node[inner sep=.7ex, outer sep=0] (-N) {};
      \path (-N.center) ++(0, -2ex)
      node[inner sep=0, outer sep=0] (-C) {};
      \path ($(-C) +(-4.5ex, -5ex) +(#3ex, 0)$) 
      node[inner sep=0, outer sep=0] (tmp) {};
      % \path (tmp) pic (lrB) {rBranch={#2}{0}{0}};
      \path ($(tmp) +(4ex, -3ex)$)
      node[draw, circle, inner sep=.7ex, outer sep=0,
      label={[label distance=0ex, scale=.8] 60:#2}] (tmpSE) {};
      \path ($(tmp) +(-3ex, -5ex) -(0, #4ex)$)
      node[draw, circle, inner sep=.7ex, outer sep=0,
      label={[label distance=0ex, scale=.8] 0:#1}] (-SW) {};
      \path (-N.270) edge[out=270, in=90] (-C.center);
      \path (-C.center) edge[out=220, in=90] (tmp);
      \path (tmp) edge[out=265, in=80] (-SW.80);
      \path (tmp) edge[out=-10, in=110] (tmpSE.110);
    }
  }
}

\begin{tikzpicture}[x={(4ex, 0)}, y={(0, 4ex)}]
  % left side  
  \path (0, 0) pic (z1) {cap={$\Sigma(z_1)$}{0}{0}};
  \path (2, -1) node {$\oplus$};
  \path (4, 0) pic (z2) {cap={$\Sigma(z_2)$}{0}{0}};
  \path (6.5, -1) node {$=$};

  % right side
  \path (10, 1) pic (z12) {cap={$\Sigma(z_1)\oplus\Sigma(z_1)$}{1}{9}};
  \path (z12-SW) pic {cap={}{0}{0}};
  \path (z12-SE) pic {cap={}{0}{0}};
\end{tikzpicture}

% \begin{tikzpicture}[x={(4ex, 0)}, y={(0, 4ex)}]
%   % left side
%   \path (2, 5) pic[yscale=2] (bell) {cap={}{7}{0}};
%   \path (bell-N) pic (rB) {rBranch={$\gamma$}{2}{0}};
%   \path (bell-N) pic (lB) {lBranch={$\alpha$}{$\beta$}{0}{0}};
  
%   \path (0, 0) pic (z1) {cap={$\Sigma(z_1)$}{0}{0}};
%   \path (4, 0) pic (z2) {cap={$\Sigma(z_2)$}{0}{0}};

%   \path (7.25, 0) node[draw, single arrow] {\phantom{aaaa}};
%   % right side
%   \path (13, 4) pic[yscale=2.5] (bell) {cap={}{9}{0}};
%   \path (bell-N) pic (lB) {lBranch={$\alpha$}{$\beta$}{0}{0}};
%   \path (bell-N) pic (rB) {rBranch={$\gamma$}{3}{0}};

%   \path (lB-SW) pic (z1) {cap={$\Sigma(z_1)$}{0}{1}};
%   \path (rB-SE) pic (z2) {cap={$\Sigma(z_2)$}{0}{1}};
% \end{tikzpicture}
\end{document}

相关内容