0

I need to paint a different path on the tree to figure two different situations that happen on the tree. In particular, I'm able to figure the travers tree with a forest, as the figure below, and this will figure my cases from root -> leaf.

enter image description here

But I need also to figure the cases from the leaf -> root with different colors and different arrows that point from child to parent and I don't know where to start.

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

\begin{document}
\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}    
\end{figure}
\end{document}

Thanks for the help.

2
  • Is this based on code from another question? If so, can you post the link (& ideally attribute it)?
    – cfr
    Dec 8, 2020 at 5:28
  • What's a travers tree? Do you mean you need arrows the other way on the same tree? Or are you talking about a different diagram?
    – cfr
    Dec 8, 2020 at 5:29

1 Answer 1

1

A simple solution is manual adding color of edges:

\documentclass[letterpaper,12pt]{article}
\usepackage{blindtext}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{figure}[ht]
    \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,edge=red
               [7 \nodepart{two} 8
                    [7]
                    [8]
               ]
               [9\nodepart{two} 1 \nodepart{three} 7, color=red,edge=red
                    [9]
                    [1\nodepart{two} 7, color=red,edge=red
                        [10, color=green,edge=green]
                        [7]
                    ]
               ]
            ]
        ]
    \end{forest}
\end{figure}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .