How to align parent nodes to the left/top in a tree
I'm not sure if this is what you want, but here's a solution using the forest
package. It provides an alignment parameter for aligning the children of a node which does most of what you want. I didn't know whether you wanted square edges or not, but they seem more appropriate for this kind of tree, (in the screen shot image the lines look odd, but this is an artefact of the screen rendering). I've added a simple alternative that looks a bit nicer without the squared edges.
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
grow right/.style={for tree={%
calign=last,
grow=east,
,s sep=.5cm,
parent anchor=east,
child anchor=west,
edge path={\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0pt,-10pt) |- (.child anchor)
\forestoption{edge label};}
}
}
,grow right
[Root [Child1 ]
[Child2
[GChild1 ]
[Gchild2 ]
[GChild3 ]
]
[Child3
[Gchild4 ]
[Gchild5 ]
]
]
\end{forest}
\hfill
\begin{forest}
[Root,for tree={calign=last,grow=east,draw, parent anchor=east,child anchor=west} [Child1 ]
[Child2
[GChild1 ]
[Gchild2 ]
[GChild3 ]
]
[Child3
[Gchild4 ]
[Gchild5 ]
]
]
\end{forest}
\end{document}
Edit: Notice that the children are organized in a reversed order, due to the counter-clockwise coordinates. Using the options calign=first, reversed=true
in the for tree = {...}
block produces the exact tree needed, as shown below:
This is just a supplement to Alan Munn's answer which uses the edges
library and other features of the current version of Forest. This therefore requires version 2+.
\documentclass[tikz,border=10pt,multi,rgb]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
grow'=0,
parent anchor=children,
child anchor=parent,
anchor=parent,
if n children=0{folder}{},
edge path'={(!u.parent anchor) -- ++(5pt,0) |- (.child anchor)},
},
where n=1{
calign with current edge
}{},
[Root
[Child1]
[Child2
[GChild1]
[Gchild2]
[GChild3]
]
[Child3
[Gchild4 ]
[Gchild5 ]
]
]
\end{forest}
\end{document}
This code produces the squared-edge version of the tree:
I think the small offset at the start of the path, prior to the vertical line, looks a bit neater, but this could be eliminated if preferred by changing the definition of the edge path to
edge path'={(!u.parent anchor) |- (.child anchor)},
Alternatively, deleting this line altogether results in a tree using the default path: