为二叉树创建节点

为二叉树创建节点

我想创建一个二进制文件,用于可视化常见的 C 二叉树函数。我曾使用过分割矩形,但没有成功。目前我有以下 MWE:

\documentclass[12pt]{article}
\usepackage[a4paper,margin=1in,showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{
  chains,
  shapes.multipart,
}
\begin{document}
\begin{figure}[!ht]
\centering
\begin{tikzpicture}[
  line width=1pt,
  font=\small\ttfamily,
  list/.style={
     rectangle split,
     rectangle split parts=3,
     draw,
     minimum width=1.8cm,
  },
  start chain,
  chain default direction=down
]
   \node[list,on chain] (A) {43 \nodepart{two} Name}; 
   \draw (A.three north) -- (A.three south);
   \node[list,on chain] (B) {54 \nodepart{two} Jesse}; 
\end{tikzpicture}
\end{figure}
\end{document}

左侧节点有一条从北到南的分割线,但我希望该线只出现在矩形的第三部分,如右侧所示。我该怎么做?

在此处输入图片描述

答案1

您可以用append after command它来实现这一点。

\documentclass[12pt]{article}
\usepackage[a4paper,margin=1in,showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{
  chains,
  shapes.multipart,
}
\begin{document}
\begin{figure}[!ht]
\centering
\begin{tikzpicture}[
  line width=1pt,
  font=\small\ttfamily,
  list/.style={
     rectangle split,
     rectangle split parts=3,
     draw,
     minimum width=1.8cm,
     append after command={(\tikzlastnode.south)
     edge (\tikzlastnode.two split)}
  },
  start chain=going below,
  %chain default direction=down
]
   \node[list,on chain] (A) {43 \nodepart{two} Name}; 
   \node[list,on chain] (B) {54 \nodepart{two} Jesse}; 
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

答案2

我会尝试手动做事,因为这样更容易定制一切。

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{expl3}
\usepackage{tikz}

\usetikzlibrary{scopes}

\begin{document}


\ExplSyntaxOn


\dim_new:N \g_doc_node_width_dim
\dim_new:N \g_doc_node_height_dim

\dim_gset:Nn \g_doc_node_width_dim {3cm}
\dim_gset:Nn \g_doc_node_height_dim {1cm}

\tikzset{
  mynode/.style={
    outer~sep=0pt,
    inner~sep=0pt,
    draw=black,
    rectangle,
    minimum~height=\g_doc_node_height_dim,
    execute~at~begin~node={\ttfamily\small\centering},
    execute~at~end~node={\par}
  }
}

% x, y, text, width
\cs_set:Npn \doc_draw_node:NNnn #1#2#3#4 {
  \node[mynode,anchor=north~west,minimum~width=#4,text~width=#4] at (\dim_use:N #1, \dim_use:N #2) {#3};
}

\cs_generate_variant:Nn \doc_draw_node:NNnn {NNxx}

% x, y, fields separated by comma, bbox_name
\cs_set:Npn \doc_draw_tree_node:nnnn #1#2#3#4 {
  \dim_gset:Nn \g_tmpa_dim {#1} % x
  \dim_gset:Nn \g_tmpb_dim {#2} % y
  \clist_gset:Nn \g_tmpa_clist {#3}
  
  \begin{scope}[local~bounding~box={#4}]
    \doc_draw_node:NNxx \g_tmpa_dim \g_tmpb_dim {\clist_item:Nn \g_tmpa_clist {1}} 
      {\g_doc_node_width_dim}
    \dim_gsub:Nn \g_tmpb_dim {\g_doc_node_height_dim}
    \doc_draw_node:NNxx \g_tmpa_dim \g_tmpb_dim {\clist_item:Nn \g_tmpa_clist {2}} 
      {\g_doc_node_width_dim}
    \dim_gsub:Nn \g_tmpb_dim {\g_doc_node_height_dim}
    \doc_draw_node:NNxx \g_tmpa_dim \g_tmpb_dim {\clist_item:Nn \g_tmpa_clist {3}} 
      {0.5\g_doc_node_width_dim}
    \dim_gadd:Nn \g_tmpa_dim {0.5\g_doc_node_width_dim}
    \doc_draw_node:NNxx \g_tmpa_dim \g_tmpb_dim {\clist_item:Nn \g_tmpa_clist {4}} 
      {0.5\g_doc_node_width_dim}
  \end{scope}
}

\cs_set_eq:NN \treenode \doc_draw_tree_node:nnnn

\ExplSyntaxOff

\begin{figure}[!ht]
\centering
\begin{tikzpicture}
\treenode{-2cm}{0cm}{43, Name, 0x00, 0x01}{node1}
\treenode{2cm}{0cm}{50, Jessie, 0x02, 0x04}{node2}
\node[anchor=north, draw=black] (node3) at (1cm, -4cm) {test node};
\draw (node1.south)--(node3);
\draw (node2.south)--(node3);
\end{tikzpicture}
\end{figure}
\end{document}

相关内容