3

Update:

What I have done is this:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
  level 1/.style={sibling distance=3cm},
  level 2/.style={sibling distance=1.5cm}]
  \node {16}
    child {node {14}
      child {node [red] {4}
            child {node {2}}
            child {node {8}}
        }
      child {node [circle, draw, red] {7}
            child {node {1}}
        }
    }
    child {node {10}
    child {node {9}}
      child {node {3}}
    };
\end{tikzpicture}
\end{document}

which produce what follow: enter image description here

I am quite new to tikz and I saw that there is many different packages to draw trees. I do not know if it is best to use "forest"

Original question:

Can someone help me in reproducing the content of the attached image in tikz? Thank you so much. enter image description here

3
  • 1
    What have you tried so far? Please show a minimal working example (MWE) of your current code.
    – epR8GaYuh
    Aug 7, 2019 at 6:34
  • Welcome to TeX.SE. What part you are having difficulty with? Can you at place the text in the desired position? Can you get the correct color of the text? If so, it would really be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. Aug 7, 2019 at 7:01
  • Thank you for your replies. I updated my question.
    – Lando
    Aug 7, 2019 at 18:07

1 Answer 1

6

In general, the TikZ basic tree methods are the least flexible and have the least practical syntax. For drawing trees of this sort, I recommend the forest package. This allows you to draw this tree very simply:

\documentclass{article}
\usepackage[linguistics]{forest}
\newcommand{\lbl}[2][red]{{\color{#1}^#2}}
\begin{document}
\begin{forest}for tree=math content
[\lbl{1}16 
    [\lbl{2}14,name=14 
        [4,red,name=4 
            [2] 
            [8] 
        ] 
        [7,draw=red,circle 
            [1]
        ]
    ] 
    [\lbl{3}10,name=10 
        [\lbl{6}9 ] 
        [3,name=3]
    ]
]
\node [left of=4] (i) {i};
\draw[->] (i) -- (4);
\node[draw,fit=(i) (4)] {};
\node[draw,dashed,ellipse,rotate=45,fit=(10.north) (3)]{};
\draw[<->,] (4.north west) to[bend left] (14.south west);
\node [above of=i] {exchange};
\end{forest}
\end{document}

output of code

1
  • Thank you for the complete example.
    – Lando
    Aug 8, 2019 at 7:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .