2

I want to add a line 1st toss, 2nd toss, 3nd toss and outcome like this picture

enter image description here

I tried

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
\tikzset{grow'=right}
\tikzset{execute at begin node=\strut}
\tikzset{every tree node/.style={anchor=base west}}
\Tree [ [.H [.H [.H ][.T ]][.T [.H ][.T ]] ] [.T [. H [.H ][.T ] ][.T [.H ] [.T ]] ]] 
\end{tikzpicture}
\end{document}

and I got

enter image description here

minthao_2011
  • 4,534
  • 7
  • 36
  • 61

2 Answers2

5

Here's a forest solution which adds the labels automatically, as well as the contents of the nodes and the list of outcomes.

auto-tree

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  /tikz/every node/.append style={font=\sffamily},
  toss/.style={
    before drawing tree={
      tikz/.wrap pgfmath arg={\node at ([yshift=10pt].center |- h.north) {Toss ##1};}{level()},
    }
  },
  tosses/.style={
    name=h,
    for ancestors'={if level=0{}{toss}},
    before packing={
      !1.tikz={\node at ([yshift=10pt].center |- h.north) {Outcomes};}
    }
  },
  for tree={
    grow'=0,
    parent anchor=east,
    child anchor=west,
    anchor=west,
    tier/.wrap pgfmath arg={tier #1}{level()},
    l sep+=20pt,
    font=\sffamily,
    delay={
      if level=0{}{
        if n=1{content=H}{content=T}
      }
    }
  },
  before typesetting nodes={
    where n children=0{
      append={[, edge={dashed}, anchor=west, font=\sffamily, content/.wrap 3 pgfmath args={(#1,#2,#3)}{content("!uuu")}{content("!uu")}{content("!u")}]}
    }{}
  }
  [
    [
      [
        [, tosses]
        []
      ]
      [
        []
        []
      ]
    ]
    [
      [
        []
        []
      ]
      [
        []
        []
      ]
    ]
  ]
\end{forest}
\end{document}

A brief introduction to forest can be found in the second part of my answer here.

The complexity of the configuration here is only due to the automatisation, which relies on doing particular things at particular stages of the drawing process. The actual tree specification is simple.

[Indeed it is just a bunch of square brackets, one comma and the string tosses. What could be simpler than that?!]

EDIT

You can adjust the contents of the nodes and labels in the preamble of the tree. I've added three comments to highlight the points at which this should be done and substituted a circle and filled circle from pifont for H and T to illustrate the idea. Since this presumably makes 'Toss' inappropriate, I've also changed the label prefix to 'Ball'.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest,pifont}
\begin{document}
\begin{forest}
  /tikz/every node/.append style={font=\sffamily},
  toss/.style={
    before drawing tree={% specify the prefix of the labels at the top here
      tikz/.wrap pgfmath arg={\node at ([yshift=10pt].center |- h.north) {Ball ##1};}{level()},
    }
  },
  tosses/.style={
    name=h,
    for ancestors'={if level=0{}{toss}},
    before packing={% specify the label for the final (rightmost) level here
      !1.tikz={\node at ([yshift=10pt].center |- h.north) {Outcomes};}
    }
  },
  for tree={
    grow'=0,
    parent anchor=east,
    child anchor=west,
    anchor=west,
    tier/.wrap pgfmath arg={tier #1}{level()},
    l sep+=20pt,
    font=\sffamily,
    delay={
      if level=0{}{% specify the contents of the nodes here
        if n=1{content=\ding{109}}{content=\ding{108}},
      }
    }
  },
  before typesetting nodes={
    where n children=0{
      append={[, edge={dashed}, anchor=west, font=\sffamily, content/.wrap 3 pgfmath args={(#1,#2,#3)}{content("!uuu")}{content("!uu")}{content("!u")}]}
    }{}
  }
  [
    [
      [
        [, tosses]
        []
      ]
      [
        []
        []
      ]
    ]
    [
      [
        []
        []
      ]
      [
        []
        []
      ]
    ]
  ]
\end{forest}
\end{document}

variation with white/black balls in place of a coin

cfr
  • 198,882
  • How can I change the letter H by white ball (a small circle) and T by black ball? Please help me. – minthao_2011 Oct 23 '15 at 04:53
  • @minthao_2011 You can change the content of the nodes in the preamble of the tree. Please see edit above for an explanation of how to do this and an example. – cfr Oct 23 '15 at 12:44
3

The easiest way given what you've done is to draw two trees, one on top of the other using TikZ's scope mechanism. I've increased the level distance of the tree branches except the last branch to accommodate the labels, and manually shifted the label tree to the left to line up the labels better.

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
\tikzset{grow'=right}
\tikzset{execute at begin node=\strut}
\tikzset{every tree node/.style={anchor=base west}, level distance=1in,level 4+/.style={level distance=.5in,edge from parent/.append style={dashed}}}
\Tree [ 
    [.H 
        [.H 
            [.H [.{(H,H,H)} ]]
            [.T [.{(H,H,T)} ]]]
        [.T 
            [.H [.{(H,T,H)} ]]
            [.T [.{(H,T,T)} ]]]] 
    [.T 
        [.H 
            [.H [.{(T,H,H)} ]]
            [.T [.{(T,H,T)} ]]]
        [.T 
            [.H [.{(T,T,H)} ]] 
            [.T [.{(T,T,T)} ]]]]
    ]
\begin{scope}[xshift=-1cm,yshift=4.5cm, edge from parent/.style={draw=none},align=center,level 4+/.style={level distance=.8in}]
\Tree [.{} [.{First Toss} [.{Second Toss} [.{Third Toss} [.{Outcomes} ]  ] ] ] ]
\end{scope}
\end{tikzpicture}
\end{document}

For a similar question see: How can I align captions with each level of a tree drawn with tikz-qtree?

output of code

Alan Munn
  • 218,180
  • It's not a linguist's tree, though, right? So I'm allowed to give a forest answer? – cfr Oct 22 '15 at 03:08
  • @cfr Yes, of course. :) I was hoping you'd jump in, actually. (That's why I said "easiest given what you've done"). – Alan Munn Oct 22 '15 at 03:10
  • Thanks. This may be a duplicate. I'm sure I've done something like this before. (But it might not be a duplicate, because the question might be using a different package.) – cfr Oct 22 '15 at 03:13
  • The question you've linked to would work well for justtrees.... – cfr Oct 22 '15 at 03:14