16

I am drawing a tree and this is my effort below. It works OK except I would like 8 to be the left child of 5 (not drawn straight below it) and 10 to the right child of 8 (not drawn straight below it). How can I do that?

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[sibling distance=8em,
  every node/.style = {shape=circle,
    draw, align=center}]]
  \node {1}
    child { node {3} 
    child { node {9}}
    child { node {7}}}
child { node {5}
    child { node {8}
        child { node {10} 
            child { node {12}}
            child { node {15}
                child { node {20}}
                child {node {18}
}}}}};
\end{tikzpicture}

enter image description here

4
  • Welcome to TeX.SE!! You just need to add child {edge from parent[draw=none]} at the correct height of the tree. And of course add more horizontal space between nodes because they will overlap.
    – manooooh
    Commented Feb 25, 2020 at 21:38
  • 15
    You should not allow your children to climb a tree and it is worse when you force them Commented Feb 26, 2020 at 10:23
  • 7
    @SinanSamet To be fair, I was trying to stop them from sliding straight down a long vertical branch.
    – Simd
    Commented Feb 26, 2020 at 11:23
  • 11
    @SinanSamet I love when questions like this appear in the Hot Network Questions; I have that momentary confusion until I see that it's not actually in the Parenting.SE.
    – Richard
    Commented Feb 26, 2020 at 14:55

2 Answers 2

15

Welcome to TeX.SE!!

You want this:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    level 1/.style ={sibling distance=12em},
    level 2/.style ={sibling distance=6em},
    every node/.style = {shape=circle,
    draw, align=center}]
]
  \node {1}
child { node {3} 
    child { node {9}}
    child { node {7}}}
child { node {5}
    child { node {8}
        child {edge from parent[draw=none]} % Added
        child { node {10} 
            child { node {12}}
            child { node {15}
                child { node {20}}
                child {node {18}
}}}}
child {edge from parent[draw=none]} % Added
};
\end{tikzpicture}
\end{document}

Tree

It differs from @Zarko's answer because of the left alignment of 8.

0
12

first version: ups, after seeing @manooooh answer (+1) I recognize, that I misread the question and of course the resulted tree was wrong (for correct see @manooooh answer)

second version This tree can be simply drawn with forest package. Hopefull, that this time is correct designed tree:

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}

\begin{document}
    \begin{forest}
for tree = {draw,
           shape=circle, 
           inner sep=0pt, 
           minimum size=1.6em,
           l sep=6mm, 
           s sep=9mm  
           }
[1
    [3
        [9]
        [7]
    ]
    [5
        [8
            [,phantom]
            [10
                [12]
                [15
                    [20]
                    [18]
                ]
            ]
        ]
        [,phantom]
    ] 
]
    \end{forest}
\end{document}

enter image description here

1
  • 1
    I love that phantom option.
    – Arne
    Commented Dec 8, 2022 at 9:15

You must log in to answer this question.

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