使用复杂的树实现在边上写标签

使用复杂的树实现在边上写标签

我绞尽脑汁想用文档来寻找答案,但遇到了一些困难,

我问了一个问题,要制作一个如下所示的复杂树,现在我正在研究基础实现以进行一些定制,特别是,我想在红色和绿色箭头上添加一些标签,因为我想指示返回树上函数的值,但我无法使用此功能定制解决方案。

在此处输入图片描述

另外,我想在右侧添加一些带有数组名称的标签,如下图所示

在此处输入图片描述

这是我的实际实现

\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, -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}{},
            where level=5{no edge, very thick, fill=white}{},
                    }% level 4 is the lowest
        [1, label=below:{1:[0,7]},  name=t0
            [1, label=below:{2:[0,3]},  name=t11
                [2, label=below:{[0,1]},  name=t21
                    [2, thick, label=below:{[0,0]}
                        [2, label=below:0, name=n0
                            [0, label=below:0] 
                        ]
                    ]
                    [4, label=below:{9:[1,1]}, name=t31
                        [4, label=below:1
                            [0, label=below:1] 
                        ] 
                    ]
                ]
                % to paint the node I can use draw=none,fill=gray, 
                [1, label=below:{5:[2,3]}
                    [3, label=below:{10:[2,2]}
                        [3,  label=below:2
                            [0, label=below:2] 
                        ]
                    ]
                    [1, label=below:{11:[4, 4]}
                        [1, label=below:3
                            [0, label=below:3] 
                        ]
                    ]
                ]
            ]
            [5, very thick,edge=thick, label=below:{3:[4,7]}, name=t62
                [6, thick,edge=thick, name=t61
                    [6, thick,edge=thick,
                        [6, label=below:4
                            [0, label=below:4] 
                        ]
                    ]
                    [7, very thick,edge=thick, name=t6
                        [7, label=below:5, name=n6
                            [0, label=below:5] 
                        ]
                    ]
                ]
                [8, very thick,edge=thick, name=t81
                    [8, very thick,edge=thick, 
                        [8, label=below:6, name=n7
                            [0, label=below:6] 
                        ]
                    ]
                    [10, very thick,edge=thick, name=t7
                        [10, label=below:7, name=n8
                            [0, label=below:7] 
                        ]
                    ]
                ]
             ]
        ]
        \scoped[on background layer]
        \node[FIT=(n7) (n8)] {};
        \draw[color=red]
            (t0) edge [bend right] (t62) 
            (t62) edge [bend right] (t81)
            (t81) edge [bend right] (t7);
        \draw[color=green]
            (t7) edge [bend right] (t81) 
            (t81) edge [bend right] (t62)
            (t62) edge [bend right] (t0);
    \end{forest}
\end{document}

答案1

可以使用quotes库(参见下面的 MWE)简单地标记弯曲箭头,例如:

    \draw[color=red]
        (t0)  edge [bend right, "a"] (t62)
        (t62) edge [bend right, "b"] (t81)
        (t81) edge [bend right, "c"] (t7);

您可以定义 Edge 引号样式为:

/tikz/every edge quotes/.style={auto=right,
                                font=\scriptsize, inner sep=1pt},% style for bended arrows quotes

如果您喜欢不同的风格,只需相应地更改上述定义。

不幸的是,您说的“我想在右侧添加一些带有数组名称的标签,如下图所示”的意思不太清楚。请澄清这一点。这个数组在哪里,这个数组里有什么?通常,您可以在某个新节点中添加它们,例如:

\node[right=1em] at (<coordinate>)
    {Lazy[]=\begin{tabular}{|*{6}{c|}}
             \hline
             0 & 0 & 0 & 0 & 4 & 4 \\
             \hline
             \end{tabular}};

编辑:

我临时在图表底层(4 和 5)的左侧添加标签“laty[ ] = ”。

梅威瑟:

\documentclass[12pt, margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc,
                fit,
                positioning,
                quotes}

\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=3pt, inner ysep=7pt, yshift=-3.5pt,
                    fit=#1},
% tree
/tikz/every edge/.style={draw, -Straight Barb,
                         shorten >=3pt, shorten <=3pt,
                         bend right = 15},% style for bended arrows
/tikz/every edge quotes/.style={auto=right,
                                font=\scriptsize, inner sep=1pt},% style for bended arrows quotes
    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}{},
    where level=5{no edge, very thick, fill=white}{},
            }% level 4 is the lowest
%%%% tree body
    [1, label=below:{1:[0,7]},  name=t0
        [1, label=below:{2:[0,3]},  name=t11
            [2, label=below:{[0,1]},  name=t21
                [2, thick, label=below:{[0,0]}
                    [0, label=below:0, name=n40
                        [0, label=below:0, name=n50]
                    ]
                ]
                [4, label=below:{9:[1,1]}, name=t31
                    [4, label=below:1
                        [0, label=below:1]
                    ]
                ]
            ]
            % to paint the node I can use draw=none,fill=gray,
            [1, label=below:{5:[2,3]}
                [3, label=below:{10:[2,2]}
                    [3,  label=below:2
                        [0, label=below:2]
                    ]
                ]
                [1, label=below:{11:[4, 4]}
                    [1, label=below:3
                        [0, label=below:3]
                    ]
                ]
            ]
        ]
        [5, very thick,edge=thick, label=below:{3:[4,7]}, name=t62
            [6, thick,edge=thick, name=t61
                [6, thick,edge=thick,
                    [6, label=below:4
                        [0, label=below:4]
                    ]
                ]
                [7, very thick,edge=thick, name=t6
                    [7, label=below:5, name=n6
                        [0, label=below:5]
                    ]
                ]
            ]
            [8, very thick,edge=thick, name=t81
                [8, very thick,edge=thick,
                    [8, label=below:6, name=n7
                        [0, label=below:6]
                    ]
                ]
                [10, very thick,edge=thick, name=t7
                    [10, label=below:7, name=n8
                        [0, label=below:7]
                    ]
                ]
            ]
         ]
    ]
    \node[left=4 mm of $(n40)!0.5!(n50)$] {Lazy[ ] = }; % <---   
    \scoped[on background layer]
    \node[FIT=(n7) (n8)] {};
    \draw[color=red]
        (t0)  edge ["a"] (t62)
        (t62) edge ["b"] (t81)
        (t81) edge ["c"] (t7);
    \draw[color=green]
        (t7)  edge ["1"] (t81)
        (t81) edge ["2"] (t62)
        (t62) edge ["3"] (t0);
\end{forest}
\end{document}

在此处输入图片描述

或者您想要这个(代码尚不存在):

在此处输入图片描述

答案2

        (t0) edge [bend right]node[below, ]{\scriptsize below} (t62) 

在此处输入图片描述

或者

        (t0) edge [bend right]node[fill=white, sloped ]{\scriptsize below} (t62) 

在此处输入图片描述

或者

(t0) edge [bend right]node[draw=none, fill=white,  sloped, pos=0.7 ]{\tiny below} (t62)

在此处输入图片描述

相关内容