2

I have the following code in Overleaf (Please excuse the \hspace{} filler):

\documentclass[margin=3mm, varwidth]{standalone}
\usepackage{tikz-qtree}
\tikzset{every tree node/.style={minimum width=2em,draw,circle},
         blank/.style={draw=none},
         edge from parent/.style=
         {draw,edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
         level distance=1.5cm}

\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \Tree
            [.\hspace{0.01cm}     
                [.\hspace{0.01cm} 
                    [.\hspace{0.01cm} ]
                    [.\hspace{0.01cm} ]
                ]
                [.\hspace{0.01cm} 
                    [.\hspace{0.01cm} 
                        [.\hspace{0.01cm} ]
                        [.\hspace{0.01cm} ]
                    ]
                    [.\hspace{0.01cm} ]
                ]
            ]
    \end{tikzpicture}
    \caption{A simple tree.}
    \label{fig:tree}
\end{figure}
\end{document}

Which provides the following tree where nodes are circles without text (and is structurally correct):

correct structure

However, I would like to fill the circles of individual (chosen) nodes with different colours. Is this possible with qtree, or do I need to move to an alternative like forest?

1
  • 1
    please add a document class so your example can be compiled.
    – cfr
    Commented Aug 15 at 3:55

1 Answer 1

4

Like this?

enter image description here

For content of vertices use \node[fill=<desired color>] {} where in {} you can insert some text:

\documentclass[margin=3mm, varwidth]{standalone}
\usepackage{tikz-qtree}
\tikzset{every tree node/.style={minimum width=2em,draw,circle},
         blank/.style={draw=none},
         edge from parent/.style=
         {draw,edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
         level distance=1.5cm}

\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \Tree
            [.\hspace{0.01cm}  
                [.\node[fill=green] {};    
                    [.\hspace{0.01cm} ]
                    [.\hspace{0.01cm} ]
                ]
                [.\node[fill=red] {};  
                    [.\hspace{0.01cm} 
                        [.\hspace{0.01cm} ]
                        [.\hspace{0.01cm} ]
                    ]
                    [.\hspace{0.01cm} ]
                ]
            ]
    \end{tikzpicture}
    \caption{A simple tree.}
    \label{fig:tree}
\end{figure}
\end{document}

Addendum:
The same result you but with a bit simpler code you can get by use of the `forest package:

\documentclass[margin=3mm, varwidth]{standalone}
\usepackage{forest}

\begin{document}
\begin{figure}
    \centering
    \begin{forest}
for tree = {circle, draw, minimum size=5mm, inner sep=0pt,
            l sep=7mm, 
            s sep=3mm
            }
[
    [ ,fill=green
        [   ]
        [   ]
    ]
    [ ,fill=red
        [
            []
            []
        ]
        []
    ]
]
    \end{forest}
    \caption{A simple tree.}
    \label{fig:tree}
\end{figure}
\end{document}
1

You must log in to answer this question.

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