Criando nós para uma árvore binária

Criando nós para uma árvore binária

Eu gostaria de criar um binário para visualização de funções comuns da árvore binária C. Eu usei retângulos divididos, mas sem sucesso. Atualmente tenho o seguinte 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}

O nó à esquerda tem uma linha dividida de norte a sul, mas eu gostaria dessa linha apenas na terceira parte do retângulo, conforme mostrado à direita. Como posso fazer isso?

insira a descrição da imagem aqui

Responder1

Você pode usar append after commandpara isso.

\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}

insira a descrição da imagem aqui

Responder2

Eu tentaria fazer as coisas manualmente, porque é mais fácil customizar tudo.

insira a descrição da imagem aqui

\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}

informação relacionada