Here you have a starting point:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[
tlabel/.style={pos=0.4,right=-1pt,font=\footnotesize\color{red!70!black}},
]
\node{S}
child {node {a}}
child {node {S}
child {node {a}}
child {node {S}
child {node {$\varepsilon$}
edge from parent node[tlabel,pos=0.2] {2}
}
edge from parent node[tlabel] {1}
}
child {node {B}
child {node {B}
child {node {b}
edge from parent node[tlabel,pos=0.2] {5}
}
edge from parent node[tlabel] {4}
}
child {node {b}}
}
edge from parent node[tlabel] {1}
}
child {node {B}
child[missing] {}
child[missing] {}
child {node {b}
edge from parent node[tlabel,pos=0.15,right=2pt] {5}
}
};
\end{tikzpicture}
\end{document}
![enter image description here](https://i.stack.imgur.com/IXeSm.png)
This is an example modified based on page 1 of the forest
package documentation.
Note the default anchors of the forest
package is not the center of a node, and thus there appear to be white spaces between edges. To fix this, set both parent anchor
and child anchor
to center
, so that they can "connect" with each other. The grow
key specifies the direction to which the tree should grow. Since you want it to grow upwards, grow=north
would do the trick.
\documentclass[border=2pt]{standalone}
\usepackage{forest}
\begin{document}
\pgfmathsetseed{14285}
\begin{forest}
random tree/.style n args={2}{% #1=max levels, #2=max children
if={#1>0}{repeat={random(0,#2)}{append={[,random tree={#1-1}{#2}]}}}{},
parent anchor=center, child anchor=center, grow=north},
[,random tree={4}{3}]
\end{forest}
\end{document}
![enter image description here](https://i.stack.imgur.com/tN4AR.png)
Update: Attaching tree to another node
This is probably not the best way to attach a forest
tree to another tikz
node, but it's the only thing I can come up with that works.
The idea is to put the forest
tree inside a node, and use node positioning commands to "attach" it to another node. The main imperfection is that you have to manually adjust the yshift
and \scalebox
parameters. (I've tried using inner sep=0pt
and outer sep=0pt
. But those screwed up the forest
picture.)
\documentclass{standalone}
\usepackage{forest}
\begin{document}
\pgfmathsetseed{12987}
\begin{tikzpicture}
\node(s)[circle,fill=red]{some node};
\node[yshift=-5.5pt,anchor=south]at(s.north){
\scalebox{.5}{
\begin{forest}
random tree/.style n args={2}{% #1=max levels, #2=max children
if={#1>0}{repeat={random(0,#2)}{append={[,random tree={#1-1}{#2}]}}}{},
parent anchor=center, child anchor=center, grow=north},
[,random tree={3}{3}]
\end{forest}
}
};
\end{tikzpicture}
\end{document}
![enter image description here](https://i.stack.imgur.com/MSKQM.png)
Best Answer
I just came across this thanks to the duplicate at Vertical trees with TikZ. The following code seems to work, though I will admit to not testing it fully and making up the numbers so that it just works.
Let's start with some output:
Here's the code:
We define a new growth function. The idea is that we have a global counter that keeps track of how many nodes we've processed so far and position the children according to this. The main problem is that the way trees are built: the origin for each branch is the parent node. So we want to apply an absolute transformation (the total height from the root) but have to take into account the relative transformation first. This takes a bit of bookkeeping, but once that's figured out then the rest is ... plain sailing.
The edges are the easy bit - we simply define them to be
|-
, ie vertical and then horizontal.