7

I'm trying to draw a tree like this:

The root has three children and every of this two... The nodes in the first two tiers has additional informations..

I searched for examples like that, but could only find trees, which children always have the same amount of children (for example here: http://www.texample.net/tikz/examples/feature/trees/).

I thank you for your help!

enter image description here

3 Answers 3

12

You can use the positioning library to place nodes relative to each other. Like \node[left=<dimen/factor> of <another node>] {}. Just give the nodes inside your tree a name. To get any amount of children simply add or remove child{ ... }. For more information please take a look at the tikz manual (especially "18 Making Trees Grow" and "16.5.3 Advanced Placement Options")

Update thx for Qrrbrbirlbel noting that the baseline alignment wasn't right. So I used base left and base right as suggested.

Now it was necessary to use a coordinate like (wAB.east |- AB) to get horizontal lines (uses the x coordinate of wAB.east and the y coordiante of AB).

For an example, you can look at this tree (I tried to recreate your tree)

\documentclass{article} \usepackage{tikz} \usetikzlibrary{trees} \usetikzlibrary{positioning} \usepackage[normalem]{ulem} \begin{document} \begin{tikzpicture}[level distance=1.5cm, level 1/.style={sibling distance=4cm}, level 2/.style={sibling distance=2cm}] \node (abc) {$ABC$} child { %node abc node (AB) {AB} %node AB child { node {$A\rightarrow BC$} } child { node {$B\rightarrow AC$} } } child { node (AC) {AC} %node AC child { node {$A\rightarrow BC$} } child { node {$C\rightarrow AB$} } } child { node (A) {A} %node A child { node {$B\rightarrow AC$} } child { node {$C\rightarrow AB$} } }; %relative to node AB \node[base left=.5 of AB] (wAB) {\uwave{$A,b$}}; \draw (wAB.east |- AB) edge[<-] (AB); \draw node[base right=.5 of AB] {$C$} edge[<-] (AB); %relative to node AC \node[base left=.5 of AC] (wAC) {\uwave{$A,C$}} ; \draw (wAC.east |- AC) edge[<-] (AC); \draw node[right=.5 of AC] {$B$} edge[<-] (AC); %relative to node A \draw node[base left=.5 of A] {$BC$} edge[<-] (A); \node[base right=.5 of A] (wBC) {\uwave{$B,C$}}; \draw (wBC.west |- A) edge[<-] (A); %relative to node abc \node[base left=of abc] {$\uwave{\hspace{3ex}}=(k-1)-\mathrm{Itemsets}$} ; \node[base right=.5 of abc] (wABACBC) {\uwave{$AB,AC,BC$}}; \draw (wABACBC.west |- abc) edge[<-] (abc); \end{tikzpicture} \end{document} 

result

2
  • 1
    Use base right and base left. These things might also be achieved with pins. Commented Dec 20, 2013 at 4:53
  • @Qrrbrbirlbel thx for the suggestion. I didn't pay attention to the alignment of the baseline. Just enhanced my solution. Commented Dec 20, 2013 at 15:39
5

This can also be done using tikz-qtree, which is a package that takes the syntax of the qtree package for writing trees and combines it with TikZ. You can have an arbitrary number of children for each root with this package and its syntax.

Similar to @someonr's solution, this solution also uses the positioning library to place the additional information in the top tier, but the additional information in the second tier is simply added as part of the roots in the second tier. You could also do this with the positioning library and syntax, if you would prefer. You would just have to declare each root in the second tier of the tree as a \node, and then you could place the additional information relative to that node.

\documentclass{article} \usepackage{amsmath} % needed for \text command in math mode \usepackage{tikz-qtree} % needed for the tree syntax \usetikzlibrary{positioning} % needed for placing nodes relative to one another \usepackage[normalem]{ulem} % needed for wavy underline; the `normalem` option preserves the usual treatment of \emph as italic; without it, `ulem` causes \emph to be rendered as underlined material \begin{document} \begin{tikzpicture} \Tree [.\node(ABC){ABC}; [.\uwave{A,B}$\leftarrow$AB$\rightarrow$C [.A$\rightarrow$BC ] [.B$\rightarrow$AC ] ] [.\uwave{A,C}$\leftarrow$AC$\rightarrow$B [.A$\rightarrow$BC ] [.C$\rightarrow$AB ] ] [.\uwave{B,C}$\leftarrow$BC$\rightarrow$A [.B$\rightarrow$AC ] [.C$\rightarrow$AB ] ] ] \node [base right = of ABC] {\uwave{AB, AC, BC}}; \node [base left = of ABC] {$=(k-1) - \text{Itemsets}$}; \end{tikzpicture} \end{document} 

enter image description here

2

The sketch is difficult to read, and the nomenclature isn't the most obvious one for an audience.

So here's an approach using:

  • forest to draw the tree
  • color to tell content from annotation/explanation

The basic tree in forest can be coded as:

 \begin{forest} for tree={math content} % https://tex.stackexchange.com/a/566003/245790 [ABC,l sep*=3 [AB \rightarrow C[A \rightarrow BC][B \rightarrow AC]] [AC \rightarrow B[A \rightarrow BC][C \rightarrow AB]] [BC \rightarrow A[B \rightarrow AC][C \rightarrow AB]] ] \end{forest} 

From the various choices to add the comments/annotation I decided to:

  • assign names to the forest nodes (though parents and children would do as well)
  • define style ann for annotations, here via \tikzset{ }
  • put the extra text as tikz-nodes
  • their slight vertical shift supports the visual message of "not being main-content, i.e. being something different"

BTW the last node is aligned:

  • left, i.e. at west of (B1), or simply (X)
  • at level of (A)
\node[ann,anchor=west] at (X.west|-A) {$(k-1)$ itemsets}; 

result

\documentclass[10pt,border=3mm]{standalone} \usepackage{forest} \begin{document} \begin{forest} % ~~~ main tree ~~~~~~~~~~~~~ for tree={math content, % https://tex.stackexchange.com/a/566003/245790 l sep*=2.5} [ABC, name=A [AB \rightarrow C,name=B1[A \rightarrow BC][B \rightarrow AC]] [AC \rightarrow B,name=B2[A \rightarrow BC][C \rightarrow AB]] [BC \rightarrow A,name=B3[B \rightarrow AC][C \rightarrow AB]] ] % ~~~ annotations ~~~~~~~~~~~ \tikzset{ ann/.style={color=magenta}, } % ~~~ at tree ~~~~~~~~~~~~~~~~~~~~~~~~~~ \node[ann,anchor=west] at (A.east) {$\rightarrow AB, AC, BC$}; \node[ann,anchor=east] (X) at (B1.west) {$A,B \leftarrow$}; \node[ann,anchor=east] at (B2.west) {$A,C \leftarrow$}; \node[ann,anchor=west] at (B3.east) {$\rightarrow B,C$}; % ~~~ legend ~~~~~~~~~~~~~~~~~~~~~~~ \node[ann,anchor=west] at (X.west|-A) {$(k-1)$ itemsets}; \end{forest} \end{document} 
3
  • 1
    Is there an automated way to align the magenta labels? Commented Sep 10 at 20:52
  • 1
    Baseline, if I remember correctly. Commented Sep 10 at 21:33
  • 1
    @MS-SPO Following the projermbc comment. This is because of the comma in the magenta nodes (not the same depth). We can fix the problem with before typesetting nodes={for tree={content=\strut#1}} (documentation p. 74) Commented Sep 13 at 11:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.