[Tex/LaTex] How to make binary search trees in an easy and straight forward way

tikz-treestrees

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.

Best Answer

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}