使用 Tikz 绘制这棵美丽的对称树

使用 Tikz 绘制这棵美丽的对称树

为了说明问题我需要制作这个图。

在此处输入图片描述

我能做到:

\documentclass[12pt,fleqn]{article}    
\usepackage[portuguese,brazil]{babel}    
\usepackage[latin1]{inputenc}    
\usepackage{tikz}    
\usepackage{xcolor}    
\begin{document}    
\begin{tikzpicture}[scale=.7]    
\draw (-1.5,-7)--(1,-7)--(1,7)--(-1.5,7)--(-1.5,-7);    
\draw (2,-7)--(3,-7)--(3,-0.5)--(2,-0.5)--(2,-7);    
\draw (4,-7)--(5,-7)--(5,-0.5)--(4,-0.5)--(4,-7);   
\draw (6,-7)--(8,-7)--(8,-0.5)--(6,-0.5)--(6,-7);    
\draw (2,0.5)--(3,0.5)--(3,7)--(2,7)--(2,0.5);    
\draw (4,0.5)--(5,0.5)--(5,7)--(4,7)--(4,0.5);    
\draw (6,0.5)--(8,0.5)--(8,7)--(6,7)--(6,0.5);    

\node at (-0.2,0) {\textbf{START}};    
\node at (2.5,-3.5) {\textbf{T}};    
\node at (2.5,3.5) {\textbf{H}};    
\node at (4.5,-1.5) {\textbf{1}};    
\node at (4.5,-2.5) {\textbf{2}};    
\node at (4.5,-3.5) {\textbf{3}};    
\node at (4.5,-4.5) {\textbf{4}};    
\node at (4.5,-5.5) {\textbf{5}};    
\node at (4.5,-6.5) {\textbf{6}};    
\node at (4.5,1.5) {\textbf{1}};    
\node at (4.5,2.5) {\textbf{2}};    
\node at (4.5,3.5) {\textbf{3}};    
\node at (4.5,4.5) {\textbf{4}};    
\node at (4.5,5.5) {\textbf{5}};    
\node at (4.5,6.5) {\textbf{6}};    
\node at (4.5,-1.5) {\textbf{1}};    
\node at (4.5,-2.5) {\textbf{2}};    
\node at (4.5,-3.5) {\textbf{3}};    
\node at (4.5,-4.5) {\textbf{4}};    
\node at (4.5,-5.5) {\textbf{5}};    
\node at (4.5,-6.5) {\textbf{6}};    
\node at (4.5,1.5) {\textbf{1}};    
\node at (4.5,2.5) {\textbf{2}};    
\node at (4.5,3.5) {\textbf{3}};    
\node at (4.5,4.5) {\textbf{4}};    
\node at (4.5,5.5) {\textbf{5}};    
\node at (4.5,6.5) {\textbf{6}};    
\node at (7,-1.5) {\textbf{(T,1)}};    
\node at (7,-2.5) {\textbf{(T,2)}};    
\node at (7,-3.5) {\textbf{(T,3)}};    
\node at (7,-4.5) {\textbf{(T,4)}};    
\node at (7,-5.5) {\textbf{(T,5)}};    
\node at (7,-6.5) {\textbf{(T,6)}};    
\node at (7,1.5) {\textbf{(H,1)}};    
\node at (7,2.5) {\textbf{(H,2)}};    
\node at (7,3.5) {\textbf{(H,3)}};    
\node at (7,4.5) {\textbf{(H,4)}};    
\node at (7,5.5) {\textbf{(H,5)}};    
\node at (7,6.5) {\textbf{(H,6)}};    
\end{tikzpicture}    
\end{document}

在此处输入图片描述

答案1

这是对原始代码的修改。我认为有更好的方法来实现这一点,使用不太明确的坐标,但这至少是一些启发。

在此处输入图片描述

\documentclass[12pt,fleqn]{article}    
\usepackage[portuguese,brazil]{babel}    
\usepackage[latin1]{inputenc}    
\usepackage{tikz}    
\usepackage{xcolor}    
\begin{document}    
\begin{tikzpicture}[scale=.7,every node/.append style={font=\bfseries\sffamily}]    
\fill[blue!10] (-1.5,-7) rectangle (1,7); 
\fill[blue!10] (2,-7) rectangle (3,-0.5); 
\fill[blue!10] (2,0.5) rectangle (3,7); 

\fill[red!10] (4,-7) rectangle (5,-0.5); 
\fill[red!10] (4,0.5) rectangle (5,7); 

\fill[yellow!30] (6,-7) rectangle (8,-0.5); 
\fill[yellow!30] (6,0.5) rectangle (8,7);  

\node (start) at (-0.2,0) {START};    
\node (T) at (2.5,-3.75) {T};    
\node (H) at (2.5,3.75) {H};    

\foreach [count=\i] \y in {1.25,2.25,...,6.25}
 {
  \node (t\i) at (4.5,-\y) {\i};
  \node (h\i) at (4.5,\y) {\i};
  \node (tp\i) at (7,-\y) {(T,\i)};
  \node (hp\i) at (7,\y) {(H,\i)};

  \draw (H.east) -- (h\i);
  \draw (T.east) -- (t\i);
 }

\draw (start.east) -- (H);
\draw (start.east) -- (T);

\end{tikzpicture}    
\end{document}

答案2

这是使用 power tree-drawing 包与 TikZ 库和forest结合构建的图表版本。Forest 的相对节点名称用于定位要包含在阴影框中的坐标,用于确保它们被正确包含在内,并用于在构建树后在树后面添加阴影。fitbackgroundsfitbackgrounds

尽管 Forest 需要一点时间来适应,但它是值得的,特别是因为除了 Forest 自己的设施之外,还可以充分利用 TikZ 的全部功能。

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{fit,backgrounds}
\begin{document}
\begin{forest}
  before packing={
    for tree={
      grow'=0,
      parent anchor=children,
      child anchor=parent,
      l sep'+=20pt,
    },
    where={level()>0}{
      s sep'-=10pt,
    }{}
  },
  where level=0{
    font=\sffamily\bfseries,
    tikz+={
      \begin{scope}[on background layer, every node/.append style={fill opacity=.25, inner ysep=0pt}]
        \node [fill=blue!50!cyan, fit=() ( |- !F.north) ( |- !L.south)] {};
      \end{scope}
    },
    for children={
      tikz+={
        \begin{scope}[on background layer, every node/.append style={fill opacity=.25, inner ysep=0pt}]
          \node [fill=blue!25!cyan, fit=() ( |- !F.north) ( |- !L.south)] {};
          \node [fill=blue!75!cyan, fit=(!1) (!6) (!1 |- !F.north) (!6 |- !L.south)] {};
          \node [fill=orange!50!yellow, fit=(!11) (!61) (!11 |- !F.north) (!61 |- !L.south)] {};
        \end{scope}
      },
    }
  }{},
  where n children=0{
    before typesetting nodes={
      font=\sffamily\bfseries,
      repeat=6{
        append={
          [, content/.option=n, font=\sffamily\bfseries,
            [, content/.wrap 2 pgfmath args={(##1,##2)}{content("!uu")}{n("!u")}, no edge, font=\sffamily\bfseries,
            ]
          ]
        }
      }
    }
  }{}
  [START
    [H
    ]
    [T
    ]
  ]
\end{forest}
\end{document}

森林解决方案

相关内容