3

Please how can I draw this tree with TikZ please?

enter image description here

My problem is how to join two children and create a new node with labels?

The current code is

\documentclass[a4paper,openany,11pt,oneside]{book} 

\usepackage{tikz} 
\usetikzlibrary{trees} 

\begin{document}

\begin{tikzpicture}[grow=right] 
    \tikzstyle{every node}=[draw,shape=circle]; 
    \node{r} 
        child { node {a} 
            child { node {c} }
            child { node {d} }
            child { node {e} } 
        } 
        child [missing] 
        child [missing] 
        child { node {b} 
            child { node {c} } 
            child { node {d} } 
            child { node {e} } 
         }; 
\end{tikzpicture}

\end{document}
  • 4
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user36296 Mar 2 '17 at 12:50
  • Thanks a lot my problem is how to join different nodes to a square? – user117159 Mar 2 '17 at 16:53
  • Please add the code you've got so we can understand which nodes you want to join to which square. Right now, i have no idea what the problem is or how you are drawing the tree. Also, I'm not sure it is a tree since some children have multiple parents. – cfr Mar 5 '17 at 17:16
  • Hello, thanls a lot for your help, this is l'example. in need to finish drawing the tree, i do until this : \documentclass[a4paper,openany,11pt,oneside]{book} \usepackage{tikz} \usetikzlibrary{trees} \begin{tikzpicture}[grow=right] \tikzstyle{every node}=[draw,shape=circle]; \node{r} child { node {a} child { node {c} } child { node {d} } child { node {e} } } child [missing] child [missing] child {node {b} child { node {c} } child { node {d} } child { node {e} } }; \end{tikzpicture} – user117159 Mar 8 '17 at 15:38
  • I added your code to the question, corrected and formatted it. So far this things works fine, you probably need to be more precise about your actual problem? – Timm Mar 9 '17 at 21:40
3

You can give a name to the tree, by giving a name to its root node, and then each node of the tree receives also an automatically generated name of the form (root-n-m), being root the name given to the root node, and n the ordinal of the first-level children, and m the ordinal of the second-level child. This name defines a "path" through the tree to any desired node.

Using those names you can add tikz commands to draw lines to other nodes, external to the tree.

For example:

\documentclass[border=5pt]{standalone}

\usepackage{tikz} 
\usetikzlibrary{trees, positioning} 

\begin{document}

\begin{tikzpicture}[grow=right] 
    \tikzset{
      every node/.style = {draw,shape=circle},
      every label/.style = {shape=rectangle, draw=none}
      }
    \node (t) {r} 
        child { node {a} 
            child { node {c} }
            child { node {d} }
            child { node {e} } 
            child { node {f} } 
        } 
        child [missing] 
        child [missing] 
        child { node {b} 
            child { node {c} } 
            child { node {d} } 
            child { node {e} } 
         }; 

    \node[rectangle, draw, below right=5mm and 3cm of t-4-3, label={$p=0.3$}]  (S1) {S11, s22};
    \node[rectangle, draw, below=1.8cm of S1 , label={$p=0.5$}]  (S2) {S11, s24};
    \node[rectangle, draw, below=of S2,        label={$p=0.12$}] (S3) {S12, s23};
    \node[rectangle, draw, below=1.8cm of S3,  label={$p=0.6$}]  (S4) {S13, s22};

    \foreach \start/\finish in {t-4-3/S1,t-4-3/S2,t-4-2/S3,t-4-1/S4,t-1-4/S1,t-1-3/S4,t-1-2/S3,t-1-1/S2}
       \draw[blue, ->] (\start) -- (\finish.west);
\end{tikzpicture}

\end{document}

Results in:

Result

2

Since this is not a tree, and since for an optimal result the nodes have to be positioned visually, I suggest to draw the nodes and edges explicitly.

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\colorlet{nblue}{blue!50}
\colorlet{nred}{brown!50!red}
\colorlet{norange}{orange!50}
\colorlet{ngreen}{green!50}
\usetikzlibrary{shapes,positioning}
\tikzset
  {common/.style={blue,draw,fill=#1,text=white},
   single/.style={common=#1,ellipse,inner sep=1pt},
   multiple/.style 2 args=
    {common=#1,rounded corners,label=above:{\scriptsize$P=#2$},
     minimum width=4em,inner sep=2pt
    }
  }
\begin{document}
\begin{tikzpicture}
  \node[single=nblue,inner sep=3pt]                              (s)       {$s$};
  \node[single=nblue,above right=of s,yshift=5mm]                (s1)      {$s_1$};
  \node[single=nblue,below right=of s,yshift=-5mm]               (s2)      {$s_2$};
  \node[single=nred,above right=of s1,yshift=-5mm]               (s11)     {$s_{11}$};
  \node[single=ngreen,right=of s1]                               (s12)     {$s_{12}$};
  \node[single=norange,below right=of s1,yshift=5mm]             (s13)     {$s_{13}$};
  \node[single=nred,above right=of s2]                           (s21)     {$s_{21}$};
  \node[single=norange,right=of s2,yshift=5mm]                   (s22)     {$s_{22}$};
  \node[single=ngreen,right=of s2,yshift=-5mm]                   (s23)     {$s_{23}$};
  \node[single=nred,below right=of s2]                           (s24)     {$s_{24}$};
  \node[multiple={nred}{0.2},right=of s12,xshift=1cm,yshift=2mm] (s11s22)  {$s_{11},s_{22}$};
  \node[multiple={nred}{0.5},below=of s11s22]                    (s11s24)  {$s_{11},s_{24}$};
  \node[multiple={ngreen}{0.12},below=of s11s24]                 (s12s23)  {$s_{12},s_{23}$};
  \node[multiple={norange}{0.6},below=of s12s23]                 (s13s22)  {$s_{13},s_{22}$};
  \node[multiple={ngreen}{0.12},right=of s11s22,yshift=-7mm]     (s12s23b) {$s_{12},s_{23}$};
  \node[multiple={nred}{0.3},below=of s12s23b]                   (s11s22b) {$s_{11},s_{22}$};
  \node[multiple={norange}{0.6},below=of s11s22b]                (s13s22b) {$s_{13},s_{22}$};
  \foreach \f/\t in
    {s/s1,s/s2,s1/s11,s1/s12,s1/s13,s2/s21,s2/s22,s2/s23,s2/s24,%
     s11/s11s22,s11/s11s24,s12/s12s23,s13/s13s22,s21/s11s22,s22/s13s22,%
     s22/s13s22,s23/s12s23,s24/s11s24,s11s22/s11s22b,s12s23/s12s23b,s13s22/s13s22b%
    }
    \draw[blue,-latex] (\f.east) -- (\t.west);
\end{tikzpicture}
\end{document}
  • how to modify all sij by $s_{ij}$ and si by $s_i$ – user117159 Mar 11 '17 at 10:52
  • 1
    With the search/replace function of your editor? – gernot says Reinstate Monica Mar 11 '17 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.