I am trying to create figures that illustrate how binary trees may be represented using array indices. Therefore I would like each node in my tree to have two labels: One inside the note, representing its value, and one above the node, representing its index in the array. I have already created a satisfactory figure, but I feel there should be a better way to create the figure.

This is my figure:

This is the code that produced the figure:

\GraphInit[vstyle = Normal]

\tikzset{VertexStyle/.style = {draw, thick, circle, fill=yellow!80}}
    \Vertex[LabelOut=true, Lpos=90, Ldist=.12cm, L=\texttt{1}]{a}
    \SOWE[LabelOut=true, Lpos=90, Ldist=.12cm,  L=\texttt{2}](4){b}
        \SOWE[LabelOut=true, Lpos=90, Ldist=.12cm,  L=\texttt{4}](3){d}
        \SOEA[LabelOut=true, Lpos=90, Ldist=.12cm,  L=\texttt{5}](3){e}
    \SOEA[LabelOut=true, Lpos=90, Ldist=.12cm,  L=\texttt{3}](4){c}
        \SOWE[LabelOut=true, Lpos=90, Ldist=.12cm,  L=\texttt{6}](2){f}
        \SOEA[LabelOut=true, Lpos=90, Ldist=.12cm,  L=\texttt{7}](2){g}

In order to create the labels, I drew each node (or vertex) twice: Once for the top label, and again for the bottom label. Is there another way using tkz-graph? I realize there may be better ways to draw binary trees, but this will also be useful for more general graphs.

Many thanks for all help.

  • Welcome to TeX.SE! Are you also open to other ways to draw such trees, like forest? – marmot Oct 12 '18 at 20:57
  • \tikzset{mynode/.style={above,yshift=0.3cm}} and inside the tikzpicture: \node[above,yshift=0.3cm] at (a) {1}... This can be included in your Vertex (or a new style) I think in order to have two parameters and the second will be the number 1 of my example but have no the time to test it now... Tested just that the \node works. – koleygr Oct 12 '18 at 21:03
  • @marmot Thank you! For this problem I absolutely could consider other ways of drawing trees. However, later in these notes I will need to draw more complex graphs where I would like to achieve the same result – i.e. two distinct labels for each vertex. – Erik André Oct 12 '18 at 21:07
  • @koleygr Taking e.g. the very first Vertex of my figure, is your suggestion to use \Vertex{4} and then in the next line \node[above, yshift=0.4cm] at (4){1};? Or did I misunderstand? – Erik André Oct 12 '18 at 21:12
  • @ErikAndréJakobsen... I meant that node works as expected and could be used in one command that will create the Vertex and the node the same time... but had not the time to test it and the accepted answer seems fine to me. – koleygr Oct 12 '18 at 21:40

Here is a proposal based on forest. If you really have more complex examples later on, I strongly recommend to switch to forest.

for tree={s sep=1cm,l sep=1.2cm,
draw, thick, circle, fill=yellow!80,

enter image description here

Notice that forest offers almost endless possibilities to draw the tree programmatically. So on the long run I really think you'd benefit from switching there (but of course I may be wrong).

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.