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.

7

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}

addedndum: 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}
  • 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). – Does it matter Mar 20 '18 at 14:06
  • 1
    Indeed the easiest syntax ! – Tarass Mar 20 '18 at 14:19
7

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

4

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}

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.