2

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:

\documentclass{article}
\usepackage{tkz-graph}
\GraphInit[vstyle = Normal]

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

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
3

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

\documentclass{article} 
\usepackage[edges]{forest}
\begin{document}
\tikzset{lbl/.style={label=above:#1}}
\begin{forest}
for tree={s sep=1cm,l sep=1.2cm,
draw, thick, circle, fill=yellow!80,
edge={thick}
}
  [4,lbl=1
   [2,lbl=3
    [2,lbl=4]
    [1,lbl=5]
   ]
   [2,lbl=3
    [1,lbl=6]
    [1,lbl=7]
   ]
  ]
\end{forest}
\end{document}

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.