8

I'm trying to have this tree in LaTeX enter image description here and I found this helpful response on this site but I easily got lost with all the brackets that are going on and I now have this enter image description here

Which is achieved by the following code:

\usepackage{tikz-qtree}
\usetikzlibrary{arrows.meta,bending}
\tikzset{every tree node/.style={minimum width=2em,draw,circle},
     blank/.style={draw=none},
     edge from parent/.style=
     {draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
     level distance=1.5cm}
\begin{tikzpicture} 
\Tree
[.51     
    [.12 ]
    [.87  
    \edge[blank]; \node[blank]{};
    \edge[.52
    \edge[]; {83}
     \edge[blank]; \node[blank]{};
    ]
    [.40
             \edge[]; {20}
             \edge[blank]; \node[blank]{};
         ]
    ]
]
\end{tikzpicture}

I know that someone can easily fix all these brackets for me but I would appreciate another method that is easier, much more, so that I can also do the rest of the trees that I want to make on my own.

Null
  • 1,525

3 Answers3

9

This is a solution using tikz, as an example consider the following tree

enter image description here

To create this code I used

\documentclass[border = 5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  ]
  \node {51}
  child {node {12}}
  child {node {87}};
\end{tikzpicture}
\end{document}

In this representation, it is very clear that both 12 and 87 are children of 57. If you want to grow the tree, just keep adding children

\documentclass[border = 5pt, tikz]{standalone}


\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {sibling distance = 30mm/#1}
  ]
  \node {51}
  child {node {12} 
        child {node {1}}
        child {node {43}
               child {node {36}}
               child {edge from parent[draw = none]}
              }
        }
  child {node {87}
         child {node {52}
                child {edge from parent[draw = none]}
                child {node {83}}
               }
         child {edge from parent[draw = none]}
        };
\end{tikzpicture}
\end{document}

enter image description here

caverac
  • 7,931
  • 2
  • 15
  • 31
8

i would simplify tree layout as follows:

enter image description here

and in drawing use package forest:

\documentclass[margin=3mm]{standalone}
\usepackage[edges]{forest}

\begin{document} \begin{forest} for tree={ grow=south, circle, draw, minimum size=3ex, inner sep=1pt, s sep=7mm } [51 [12 [1] [43 [36] ] ] [87 [52 [83] ] ] ] \end{forest} \end{document}

addendum: considering caverac comment:

enter image description here

\documentclass[margin=3mm]{standalone}
\usepackage[edges]{forest}

\begin{document} \begin{forest} for tree={ grow=south, circle, draw, minimum size=3ex, inner sep=1pt, s sep=3mm } [51 [12 [1] [43 [36] [,no edge, draw=none] ] ] [87 [52 [,no edge, draw=none] [83] ] [,no edge, draw=none] ] ] \end{forest} \end{document}

Edit:
In above MWE you can nodes options no edge, draw=none can replace with shorter phantom (@ Arne, thank you for remind me on this).

Zarko
  • 296,517
  • 3
    In a binary search tree, the notions of left child and right child are fundamental, and they kind of disappeared in your implementation. I never used forest before but I think it is possible to make that explicit, would you be so kind and add a correction to your answer? Thanks! – caverac Mar 20 '18 at 13:41
  • 1
    @caverac, this option is now added. – Zarko Mar 20 '18 at 13:47
  • The solution in the addedndum is much much easier to implement and to read compared to what i had. Thanks a lot (+1). –  Mar 20 '18 at 14:06
  • 1
    Indeed the easiest syntax ! – Tarass Mar 20 '18 at 14:19
  • the phantom option build in makes ,no edge, draw=none superfluous. – Arne Dec 11 '22 at 13:18
  • @Arne, you are right. With option phantom can be replaced no edge, draw=none. – Zarko Dec 11 '22 at 13:59
3

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[%level distance=5mm,
level 1/.style={level distance=10mm,sibling distance=24mm},
level 2/.style={level distance=10mm,sibling distance=16mm},
level 3/.style={level distance=10mm,sibling distance=16mm},
font=\scriptsize,inner sep=2pt,every node/.style={draw,circle,minimum size=3ex}]

\node {51} % root
child {node {12} % first level
        child {node{1}} % 2sd level
        child {node{43} 
            child {node{36}} % 3rd level
            child[missing]
        }
    edge from parent }
child   {node {87} % first level
            child {node {52} % 2sd level
                child[missing] % 3rd level
                child {node{83}}
            }
            child[missing] 
        } ;
\end{tikzpicture}
\end{document}
Tarass
  • 16,912