在 Latex 中绘制树

在 Latex 中绘制树

我正在尝试画一个使用Tikz。我的代码如下:

\begin{figure}
    \centering
    \begin{tikzpicture}[node distance=1.5cm]
    \tikzstyle{process} = [rectangle, minimum width=0.7cm, minimum height=0.7cm,text centered, draw=black, fill=white!100]
    
    \tikzstyle{arrow} = [thick,->,>=stealth]
    
    \node (proA) [process] {{A}};
    \node (proB) [process, right of = proA] {{B}};
    
    \node (pro1) [process, below of = proA] {{1}};
    
    \node (pro2) [process, below of = pro1] {{$S_2$}};
    \node (pro3) [process, right of = pro2] {{$S_3$}};
    
    \node (pro6) [process, below of = pro2] {{6}};
    \node (pro7) [process, right of = pro6] {{7}};
    \node (pro8) [process, right of = pro7] {{8}};
    \node (pro9) [process, right of = pro8] {{9}};
    
    \draw [arrow] (pro1) -- (pro2);
    \draw [arrow] (pro1) -- (pro3);
    
    \draw [arrow] (pro2) -- (pro6);
    \draw [arrow] (pro2) -- (pro7);
    \draw [arrow] (pro3) -- (pro8);
    \draw [arrow] (pro3) -- (pro9);
    
    \draw [arrow] (proA) -- (pro1);
    \draw [arrow] (proB) -- (pro1);
    \end{tikzpicture}
    \label{fig:CTU_partitioning}
\end{figure}

但是,当我需要类似于左侧的输出时,它会产生类似于图像右侧的输出。

在此处输入图片描述

我该如何解决这个问题?可能更多级别节点在树中。请建议一个聪明的绘画方法乳胶中的这种类型的图形。

答案1

通过使用forest包(针对节点下方的树1)和 Ti上面节点的 Z 图片(首先,但是还有更复杂的方法用森林绘制完整的图像):

在此处输入图片描述

\documentclass[border=3.141592]{standalone}
\usepackage{forest}
\usetikzlibrary{positioning}

\begin{document}
\begin{forest}
  for tree={
%   nodes
    draw, minimum size=2em, inner sep=1pt,
%   tree
    parent anchor=south,
    child anchor=north,
    l sep=7mm,
    s sep=7mm
            }
[1, name=s
    [S\textsubscript{2}
        [6]
        [7]
    ]
    [S\textsubscript{3}
        [8]
        [9]    
    ]
]
\node (a)   [draw, minimum size=2em,
             above left=7mm and 7mm of s] {A};
\node (b)   [draw, minimum size=2em,
             above right=7mm and 7mm of s] {B};
\draw (a.south) -- (s.north) -- (b.south);
\end{forest}
\end{document}

相关内容