3

My latex code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing} 
\usegdlibrary{trees}
\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
    % draw a tree
    \tikz [grow'=up, binary tree layout, nodes={circle,draw}]
      \node {9}
      child { node {8}
        child { node {6} 
          child { node {1} }
          child { node {2} }
        }
        child { node {7}
          child { node {3} }
          child { node {4} }
        }
      }
      child { node {5} };
\end{tikzpicture}
\end{figure}
\end{document}

In the output, node 5 is at the same level as node 8

enter image description here

In my expected output, node 5 should be at the top level with nodes 1, 2, 3 and 4.

enter image description here

How can I move node 5 to the top?

1 Answer 1

4

This is extremely straightforward if forest is an option.

\documentclass[border=5pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={circle, draw, l sep'+=2pt, grow'=90, edge+=blue, fill=blue, text=white, font=\sffamily, child anchor=parent, parent anchor=children},
  for leaves={tier=leaf},
  [9 
    [8
      [6
        [1]
        [2]
      ]
      [7
        [3]
        [4]
      ]
    ]
    [5]
  ]
\end{forest}
\end{document}

forest tree with leaves on single tier

Following Zarko's comment, I noticed that only some children's edges are drawn from a common point on the parent. If you don't want this, you can remove parent anchor=children and/or child anchor=parent or apply the option(s) more selectively.

without specified child/parent anchors

\documentclass[border=5pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={circle, draw, l sep'+=2pt, grow'=90, edge+=blue!50!gray, fill=blue!50!gray, text=white, font=\sffamily, inner sep=1.5pt,},
  for leaves={tier=leaf},
  [9 
    [8
      [6
        [1]
        [2]
      ]
      [7
        [3]
        [4]
      ]
    ]
    [5]
  ]
\end{forest}
\end{document}

If forest isn't an option, let me know and I can delete this.

4
  • This is of course an option, I would switch to this package if it is hard in tikz trees Dec 4, 2023 at 17:23
  • +1! , bur I would dropout options child anchor=parent, parent anchor=children. To my taste the result than is nicer :-)
    – Zarko
    Dec 4, 2023 at 17:54
  • @Zarko I agree, but this was the style of the 'expected output' as opposed to the current output of the OP's code. I don't much line common child anchors where the parent has a drawn shape like this. It looks a bit weird. For that matter, I'm not keen on the colouring either. (But that's partly because I didn't faff with the shade - the blue in the OP's question is better, I think.)
    – cfr
    Dec 4, 2023 at 17:59
  • @Zarko Actually, I just realised the example in the question only does that for some levels. Huh. Thanks.
    – cfr
    Dec 4, 2023 at 18:00

You must log in to answer this question.

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