用以下图案给树涂色

用以下图案给树涂色

我知道最好报告一些代码来帮助社区了解我的问题是什么,但我需要重现下图,并在下面报告数组

在此处输入图片描述

我想用我的乳胶文档中的这个图重构我的实际结果

\documentclass[letterpaper,12pt]{article}
\usepackage{blindtext}
\usepackage{tikz}
\usepackage{forest}
\usepackage{xcolor}
\usepackage{float}
\usetikzlibrary{calc}
\newcounter{nodeidx}
\newcounter{example}
\setcounter{nodeidx}{1}

\usetikzlibrary{
    calc,
    arrows.meta,
    %shapes.multipart,
    %arrows
}

\newcommand{\nodes}[1]{%
    \foreach \num [count=\n starting from 0] in {#1}{% no need for an external counter
      \node[minimum size=6mm, draw, rectangle] (n\n) at (\n,0) {\num};
    }
}
\newcommand{\brckt}[4][1]{% [ lvl ] { from, to, text }
    \coordinate (left) at ($(n#2.south west)+(-2mm,-1mm)$);
    \coordinate (right) at ($(n#3.south east)+(2mm,-1mm)$);
    \draw (left) -- ($(left)+(0,-1mm*#1)$) --node[below,midway,font=\scriptsize] {#4} ($(right)+(0,-1mm*#1)$) -- (right);
}

\begin{document}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        %\pgftransparencygroup
        \nodes{2, 4, 3, 1, 6, 7, 8, 9, 1, 7}
        %\endpgftransparencygroup
        %\pgftransparencygroup
        \brckt[2]{0}{5}{Query(0, 5)}
        %\endpgftransparencygroup
        %\pgftransparencygroup
        \brckt{3}{9}{~~~~~~~Query(3, 9)}% used ~ to push the label
        %\endpgftransparencygroup
        \brckt[2]{8}{9}{Query(8, 9)}
    \end{tikzpicture}
    \caption{Original Array where perform the RMQ.}
    \label{fig:origianl_array}
  \end{figure}

\begin{figure}[H]
    \centering
    \begin{forest}
        for tree = {
            rectangle split,
            rectangle split horizontal,
            rectangle split parts=10,
            rectangle split ignore empty parts,
            draw,
            %
            parent anchor=south,
            child  anchor=north,
            calign=edge midpoint,
            edge = {-Stealth, semithick},
            l sep=6mm,
            where level=0{s sep=11mm}{s sep=3mm},
                    }
        [2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6
          \nodepart{six} 7 \nodepart{seven} 8 \nodepart{eight} 9 \nodepart{nine} 1 \nodepart{ten}  7, color={red}
            [2\nodepart{two} 2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6
               [2\nodepart{two} 4
                   [2] [4]]
               [3\nodepart{two} 1 \nodepart{three} 6
               [3] [1 \nodepart{two} 6
                       [1] [6]
               ]]
            ]
            [7 \nodepart{two} 8 \nodepart{three} 9 \nodepart{four} 1
              \nodepart{six} 7,fit=band, color={red}
               [7 \nodepart{two} 8
                    [7]
                    [8]
               ]
               [9\nodepart{two} 1 \nodepart{three} 7, color={red}
                    [9]
                    [1\nodepart{two} 7, color={red}
                        [10, color={green}]
                        [7]
                    ]
               ]
            ]
        ]
    \end{forest}
    \caption{Update the value in position 8 with value 10}
    \label{fig:update_rmq}  
\end{figure}
\end{document}

有了这个结果

在此处输入图片描述

答案1

  • 您的 MWE 具有误导性(复制一些不相关的树),为了帮助您,您需要从头开始编写代码。
  • 仅作为起点,因此代码相当简陋
  • 错过了我留给你的标签(你可以按照我重写的方式添加它们)
\documentclass[12pt, margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,
                backgrounds,
                fit}

\begin{document}
    \begin{forest}
for tree = {
% nodes
    circle, draw, fill=gray!30,
    minimum size=1.5em, inner sep=0pt, outer sep=0pt,
/tikz/every label/.style = {font=\tiny},
/tikz/FIT/.style = {draw, very thick, rounded corners, fill=gray!30,
                    inner sep=4pt, inner ysep=8pt, yshift=-4pt,
                    fit=#1},
% tree
/tikz/every edge/.style={draw=gray, -Straight Barb, 
                         -Straight Barb, shorten >=3pt, shorten <=3pt,
                         bend angle = 15},% style for bended arrows
    edge=semithick,
    l sep=4mm,
    s sep=12mm,
    where level=3{l sep=11mm}{},% at level 3 increase l sep
    where level=4{no edge, very thick, fill=white}{},
            }% level 4 is the lowest
[11, label=below:{1:[0,7]},  name=t0
    [13, label=below:{2:[0,3]},  name=t11
        [17, label=below:{4:[0,1]},  name=t21
            [18, thick, label=below:{8:[0,0]}
                [18, label=below:0]
            ]
            [17, draw=none, fill=gray,thick, label=below:{9:[1,1]},  name=t31
                [17, label=below:1, name=n1]
            ]
        ]
        [13, draw=none,fill=gray, label=below:{5:[2,3]}
            [13, very thick, label=below:{10:[2,2]}
                [13, draw=none, fill=gray, label=below:2]
            ]
            [19, very thick, label=below:{11:[3,3]}
                [19, label=below:3, name=n3]
            ]
        ]
    ]
    [11, very thick,edge=thick, label=below:{3:[4,7]}
        [11, thick,edge=thick,
            [15, thick,edge=thick,
                [15, label=below:4]
            ]
            [11, very thick,edge=thick,
                [13, label=below:5]
            ]
        ]
        [20, very thick,edge=thick,
            [20, very thick,edge=thick,
                [13, label=below:6]
            ]
            [99, very thick,edge=thick,,
                [13, label=below:7]
            ]
        ]
     ]
]
\scoped[on background layer]
\node[FIT=(n1) (n3)] {};
\draw
    (t0)  edge [bend right] (t11)
    (t11) edge [bend right] (t21)
    (t21) edge [bend  left] (t31);
    \end{forest}
\end{document}

编辑:

要添加弯曲箭头,您需要为要在其间绘制弯曲箭头的节点添加名称,并为这些箭头定义样式。(添加到上面的 MWE)。添加后,树形图为:

在此处输入图片描述

相关内容