4

I want to draw complete binary tree with some portion highlighted. I am able to draw the complete binary tree, but not able to highlight the specified portion.

I want to draw the digram given below:

enter image description here

Till now I'm able to do this much:

\documentclass[border=2pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={l+=0.07cm} % increase level distance
  [1
    [2[4][5]]
    [3[6][7]]
  ]
\end{forest}
\end{document}
6

I think, in theory, that this should be possible by adding

tikz={\node[draw,circle,red,fit=()(!1)(!2)}

to the root of the node that you want to circle. Here () refers to the node and (!1) and (!2) its children. Unfortunately, this doesn't quite work because it gives:

enter image description here

So, in practice, it looks better with a little extra tweaking:

enter image description here

See section 2.3 of the manual for more details.

Here is the full code:

\documentclass[border=4pt]{standalone}
\usepackage{forest}
\begin{document}

  \begin{forest}
    for tree={l+=0.08cm} % increase level distance
      [1
        [2,tikz={\node[draw,circle,red,fit=()(!1),inner sep=0mm,xshift=1mm]{};}
           [4][5]
        ]
        [3
           [6][7]
        ]
      ]
  \end{forest}

\end{document}

Another option, which might be preferable, is to use

tikz={\node[draw,circle,red,fit=()(!1)(!2),inner sep=0mm]{};}

giving

enter image description here

3

Here is another solution to use the istgame package. Since the istgame environment is (almost) the same as the tikzpicture environment, you can use any TikZ macros in the istgame environment.

enter image description here

\documentclass{standalone}

\usepackage{istgame}
\usepackage{makecell}

\begin{document}

\begin{istgame}
% tree (istgame macros)
\xtdistance{15mm}{30mm}
\istroot(1){1} \istb \istb \endist
\xtdistance{15mm}{15mm}
\istroot(2)(1-1)<180>{2}
  \istb*{}{4} \istb*{}{5} \endist
\istroot(3)(1-2)<0>{3}
  \istb*{}{6} \istb*{}{7} \endist
\xtSubgameOval(2){(2-1)(2-2)}[circle,solid,red,inner sep=10pt]
% comments (tikz macros)
\coordinate (aa) at ([shift={(-2.5,-.5)}]2);
\draw [->] (aa) node [left] {Block} -- + (1,0);
\coordinate (bb) at ([shift={(2,.3)}]3);
\draw [->] (bb) node [right] {\makecell[l]{Complete \\ binary \\ tree}} -- + (-1,0);
\end{istgame}
\end{document}

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.