1

I want to use tikz-qtree to make a binary tree where left edges are red and labelled 0 while right edges are black and labelled 1. The following is the way I am currently doing it:

\documentclass{article}
\usepackage{tikz, tikz-qtree}
\begin{document}
\Tree[.a \edge[color=red] node[color=black, auto=right]{0};
             [.b \edge[color=red] node[color=black, auto=right]{0}; c \edge node[auto=left]{1}; d ]
         \edge node[auto=left]{1}; [.e \edge[draw=none]; {} \edge node[auto=left]{1}; f ] ]
\end{document}

and that code produces the following tree:

enter image description here

(This is supposed to be used for a larger tree, but the code for this is already ugly enough.) Can someone show me a better way? Tips to make the code more readable are also welcome.

For the sake of generality, I would also be interested in solutions that generalize to n-trees (where each node has n children), though I don't have plans to use that at the moment.

I am happy to see solutions that use other packages.

  • Please edit your post to make the snippet compilable and say whether you are open to solutions using other packages and what an n-tree is. – cfr Aug 11 '18 at 0:27
  • If you need to do this automatically, then forest is probably a better solution. – Alan Munn Aug 11 '18 at 22:52
  • @cfr hopefully the code compiles (I’m on my phone right now so I don’t know how to check); if so, I have done both. – alphacapture Aug 11 '18 at 22:52
  • For an n-tree, what would the colouring/numbering scheme be, though? Also, for a unary branch as in your e-f, what really determines if it's a left or right branch? – Alan Munn Aug 11 '18 at 23:02
  • 1
    I suggest you have a look at the 'forest' tag. That will give you an idea of what can be done. Annotating your current tree is trivial in Forest. You just need something like where n=1{edge+=red, edge label={node[midway, left] {0}}}{edge label={node[midway, right]{1}}} in the tree's preamble. – cfr Aug 12 '18 at 4:10

Your Answer

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

Browse other questions tagged or ask your own question.