1

I'm trying to color the nodes of a binary tree. The main idea is that i want to create a binary tree that the nodes of its subtrees are colored. I don't have a preferred color for the nodes, i just want with someway to color them.

[![What i want to achieve][1]]

I have also used this code to make the binary tree but with out the colors:

 \begin{forest}

for tree={
    grow=south,
    circle, draw, minimum size=2ex, inner sep=1.5pt,
    s sep=1mm
        }
[
    [
        [ [ [ [] [] ] [[] [] ] ] [ [[] [] ] [[] [] ] ] ]
        [
            [ [[] [] ] [ [] [] ] ] [ [[] [] ] [[] []] ]
        ]
    ]
    [
        [
            [ [[] [] ] [[] [] ] ] [ [[] [] ] [[] [] ] ]
        ] [ [ [[] []  ] [[] [] ] ] [ [[] [] ] [[] [] ] ] ]
    ]
]

\end{forest}

Is there a way to use forest package to color the nodes? [1]: https://i.stack.imgur.com/XxeCH.png

1 Answer 1

1

You can use for tree on any subtree.

enter image description here

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={
    grow=south,
    circle, draw, minimum size=2ex, inner sep=1.5pt,
    s sep=1mm
}
[
    [, for tree={fill=red}
        [ [ [ [] [] ] [[] [] ] ] [ [[] [] ] [[] [] ] ] ]
        [
            [ [[] [] ] [ [] [] ] ] [ [[] [] ] [[] []] ]
        ]
    ]
    [, for tree={fill=blue}
        [
            [ [[] [] ] [[] [] ] ] [ [[] [] ] [[] [] ] ]
        ] [ [ [[] []  ] [[] [] ] ] [ [[] [] ] [[] [] ] ] ]
    ]
]
\end{forest}

\end{document}
1
  • Thank you very much! :)
    – Leonidas
    Nov 21, 2022 at 16:29

You must log in to answer this question.

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