删除圆圈并为树的元素添加颜色

删除圆圈并为树的元素添加颜色

问题:我正在寻找下面添加的图像。我想为每个节点添加不同的颜色。

这是我目前所做的:

\documentclass{article} 
\usepackage{forest}

\begin{document}
    \large
    \begin{forest}
        for tree={circle,draw, l sep=10pt}
        [36,black 
        [2]
        [18
        [2] 
        [9
        [3]
        [3]
        ]       
        ]
        ]
        \end{forest}
\end{document}

在此处输入图片描述

答案1

你的意思是像这样?只需tikz在节点文本后添加绘图参数(例如,draw=redfill=greentext=blue)。你可以通过添加 来删除给定节点的圆圈draw=none

\documentclass{article} 
\usepackage{forest}

\begin{document}
    \large
    \begin{forest}
        for tree={circle,draw, l sep=10pt}
        [36,draw=none 
        [2,fill=red]
        [18,draw=none
        [2,fill=yellow] 
        [9,draw=none
        [3,fill=green]
        [3,fill=cyan]
        ]       
        ]
        ]
        \end{forest}
\end{document}

输出

或者

\documentclass{article} 
\usepackage{forest}
\newcounter{nextcolor}
\xdef\LstColor{{"red","yellow","green","blue","orange","magenta","cyan"}}
\tikzset{fill next color/.style={%
/utils/exec=\pgfmathsetmacro{\mycolor}{\LstColor[\number\value{nextcolor}]}
\stepcounter{nextcolor},
fill=\mycolor}}
\begin{document}
    \large
    \begin{forest}
        for tree={l sep=10pt,
        if n children=0{circle,draw,fill next color}{}}
        [36,black 
        [2]
        [18
        [2] 
        [9
        [3]
        [3]
        ]       
        ]
        ]
    \end{forest}
\end{document}

相关内容