TransWikia.com

Paint different path on the tree with forest package

TeX - LaTeX Asked on August 19, 2021

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] (nn) 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}
            [2nodepart{two} 2 nodepart{two} 4 nodepart{three} 3 nodepart{four} 1 nodepart{five} 6
               [2nodepart{two} 4
                   [2] [4]]
               [3nodepart{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]
               ]
               [9nodepart{two} 1 nodepart{three} 7, color={red}
                    [9]
                    [1nodepart{two} 7, color={red}
                        [10, color={green}]
                        [7]
                    ]
               ]
            ]
        ]
    end{forest}    
end{figure}
end{document}

Thanks for the help.

One Answer

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,
            [2nodepart{two} 2 nodepart{two} 4 nodepart{three} 3 nodepart{four} 1 nodepart{five} 6
               [2nodepart{two} 4
                   [2] [4]]
               [3nodepart{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]
               ]
               [9nodepart{two} 1 nodepart{three} 7, color=red,edge=red
                    [9]
                    [1nodepart{two} 7, color=red,edge=red
                        [10, color=green,edge=green]
                        [7]
                    ]
               ]
            ]
        ]
    end{forest}
end{figure}
end{document}

enter image description here

Correct answer by Zarko on August 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP