7

I want to make tree with each node has valency of three. I got the below code online somewhere. The first level has valency three however second and third level has valency four, and I also want to color the child different from parent and just want to use three colors. (e.g if parent is red then the branches will be green and blue)

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees,snakes}
\begin{document}
\pagestyle{empty}
\tikzstyle{level 1}=[level distance= 32mm,sibling angle=120]
\tikzstyle{level 2}=[level distance= 16mm,sibling angle=60]
\tikzstyle{level 3}=[level distance = 12mm,sibling angle=30]
\tikzstyle{every node}=[fill]
%\tikzstyle{edge from parent}=[segment length=1mm,
%                              segment angle=10,draw]
\begin{tikzpicture}[grow cyclic,shape=circle,very thick,level distance=13mm,
                    cap=round]
\node {} child [color=\A] foreach \A in {red,green,blue}
    { node {} child [color=\A!50!\B] foreach \B in {red, green,blue}
        { node {} child [color=\A!50!\B!50!\C] foreach \C in {red,green,blue}
            { node {} }
        }
    };
\end{tikzpicture}

enter image description here

I am new in using tikz and didn't feel comfortable in working with it so far.

  • Please add \end{document} and use \tikzset{level 1/.style={level distance= 32mm,sibling angle=120} and likewise for the other styles. And please explain what "valency" means. My current interpretation suggests it is not possible to achieve this with 3 colors only if a parent has more than 2 children. – user121799 Sep 5 '18 at 5:50
8

Very similar to Andrew's nice answer except that parents and children have different colors. EDIT: removed a superfluous \index, big thanks to Andrew! @nd EDIT: Simplification with \pgfkeysalso.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{trees,snakes}
\begin{document}
\pagestyle{empty}
\xdef\mycolor{black}
\xdef\ColorList{{"red","green!60!black","blue"}}
\tikzset{level 1/.style={level distance= 32mm,sibling angle=120},
level 2/.style={level distance= 16mm,sibling angle=60},
level 3/.style={level distance = 12mm,sibling angle=30},
every node/.append style={fill},
my color/.code={\pgfmathparse{\ColorList[mod(#1,3)]}
\pgfkeysalso{/tikz/color/.expanded=\pgfmathresult}}}
\begin{tikzpicture}[grow cyclic,shape=circle,very thick,level distance=13mm,
                    cap=round]
\node {} child [my color=\A] foreach \A in {0,1,2}
    { node {} child [my color=\A+\B+1] foreach \B in  {0,1}
        { node {} child [my color=\A+\B+\C+2] foreach \C in  {0,1}
            { node {} }
        }
    };
\end{tikzpicture}
\end{document}  

enter image description here

  • 3
    +1 Very cute! :) Part of me is unhappy that you are redefining the \index command but of course this doesn't matter since everything happens inside a group. – Andrew Sep 5 '18 at 6:34
  • 1
    @Andrew Good point. Will fix it. – user121799 Sep 5 '18 at 6:36
6

If you want each vertex to have valence 3 then you should loop over only two colours for the child nodes. This produces:

enter image description here

Here is the modified code:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees,snakes}
\begin{document}
\pagestyle{empty}
\tikzstyle{level 1}=[level distance= 32mm,sibling angle=120]
\tikzstyle{level 2}=[level distance= 16mm,sibling angle=60]
\tikzstyle{level 3}=[level distance = 12mm,sibling angle=30]
\tikzstyle{every node}=[fill]
%\tikzstyle{edge from parent}=[segment length=1mm,
%                              segment angle=10,draw]
\begin{tikzpicture}[grow cyclic,shape=circle,very thick,level distance=13mm,
                    cap=round]
\node {} child [color=\A] foreach \A in {red,green,blue}
    { node {} child [color=\A!50!\B] foreach \B in {red, green}
        { node {} child [color=\A!50!\B!50!\C] foreach \C in {red,green}
            { node {} }
        }
    };
\end{tikzpicture}

\end{document}
  • Thanks @Andrew. I did this earlier myself however my problem is also colors. The red parent should not have red child directly. – Touseef Sep 5 '18 at 6:31
  • @Touseef Sorry, didn't notice your colour requirement but marmot seems to have sorted it out. – Andrew Sep 5 '18 at 6:33
  • Yeah he did it. I am now trying to understand his magic code lines :-) – Touseef Sep 5 '18 at 6:35
  • @Touseef It's just a clever use of mod 3 arithmetic combined with the list of three colours defined in \ColorList. – Andrew Sep 5 '18 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.