# Use Tikz to draw this beautiful symmetric tree

To illustrate a problem I need to make this figure.

I can to do:

\documentclass[12pt,fleqn]{article}
\usepackage[portuguese,brazil]{babel}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}[scale=.7]
\draw (-1.5,-7)--(1,-7)--(1,7)--(-1.5,7)--(-1.5,-7);
\draw (2,-7)--(3,-7)--(3,-0.5)--(2,-0.5)--(2,-7);
\draw (4,-7)--(5,-7)--(5,-0.5)--(4,-0.5)--(4,-7);
\draw (6,-7)--(8,-7)--(8,-0.5)--(6,-0.5)--(6,-7);
\draw (2,0.5)--(3,0.5)--(3,7)--(2,7)--(2,0.5);
\draw (4,0.5)--(5,0.5)--(5,7)--(4,7)--(4,0.5);
\draw (6,0.5)--(8,0.5)--(8,7)--(6,7)--(6,0.5);

\node at (-0.2,0) {\textbf{START}};
\node at (2.5,-3.5) {\textbf{T}};
\node at (2.5,3.5) {\textbf{H}};
\node at (4.5,-1.5) {\textbf{1}};
\node at (4.5,-2.5) {\textbf{2}};
\node at (4.5,-3.5) {\textbf{3}};
\node at (4.5,-4.5) {\textbf{4}};
\node at (4.5,-5.5) {\textbf{5}};
\node at (4.5,-6.5) {\textbf{6}};
\node at (4.5,1.5) {\textbf{1}};
\node at (4.5,2.5) {\textbf{2}};
\node at (4.5,3.5) {\textbf{3}};
\node at (4.5,4.5) {\textbf{4}};
\node at (4.5,5.5) {\textbf{5}};
\node at (4.5,6.5) {\textbf{6}};
\node at (4.5,-1.5) {\textbf{1}};
\node at (4.5,-2.5) {\textbf{2}};
\node at (4.5,-3.5) {\textbf{3}};
\node at (4.5,-4.5) {\textbf{4}};
\node at (4.5,-5.5) {\textbf{5}};
\node at (4.5,-6.5) {\textbf{6}};
\node at (4.5,1.5) {\textbf{1}};
\node at (4.5,2.5) {\textbf{2}};
\node at (4.5,3.5) {\textbf{3}};
\node at (4.5,4.5) {\textbf{4}};
\node at (4.5,5.5) {\textbf{5}};
\node at (4.5,6.5) {\textbf{6}};
\node at (7,-1.5) {\textbf{(T,1)}};
\node at (7,-2.5) {\textbf{(T,2)}};
\node at (7,-3.5) {\textbf{(T,3)}};
\node at (7,-4.5) {\textbf{(T,4)}};
\node at (7,-5.5) {\textbf{(T,5)}};
\node at (7,-6.5) {\textbf{(T,6)}};
\node at (7,1.5) {\textbf{(H,1)}};
\node at (7,2.5) {\textbf{(H,2)}};
\node at (7,3.5) {\textbf{(H,3)}};
\node at (7,4.5) {\textbf{(H,4)}};
\node at (7,5.5) {\textbf{(H,5)}};
\node at (7,6.5) {\textbf{(H,6)}};
\end{tikzpicture}
\end{document}


• If you assign names to the nodes, you can draw lines connecting the anchors. For example, \draw (start.east) -- (h.west); Also, you can draw or fill a rectangle using, for example \fill[color=blue,opacity=.5] (-1.5,-7) rectangle (1.5,.7); – John Kormylo May 26 '16 at 15:24
• John Kormylo, With your suggestion, now I can make the drawing. – benedito May 26 '16 at 16:34

This is a modification of your original code. I think there are better ways of achieving this, using less explicit coordinates, but this is at least some inspiration.

\documentclass[12pt,fleqn]{article}
\usepackage[portuguese,brazil]{babel}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}[scale=.7,every node/.append style={font=\bfseries\sffamily}]
\fill[blue!10] (-1.5,-7) rectangle (1,7);
\fill[blue!10] (2,-7) rectangle (3,-0.5);
\fill[blue!10] (2,0.5) rectangle (3,7);

\fill[red!10] (4,-7) rectangle (5,-0.5);
\fill[red!10] (4,0.5) rectangle (5,7);

\fill[yellow!30] (6,-7) rectangle (8,-0.5);
\fill[yellow!30] (6,0.5) rectangle (8,7);

\node (start) at (-0.2,0) {START};
\node (T) at (2.5,-3.75) {T};
\node (H) at (2.5,3.75) {H};

\foreach [count=\i] \y in {1.25,2.25,...,6.25}
{
\node (t\i) at (4.5,-\y) {\i};
\node (h\i) at (4.5,\y) {\i};
\node (tp\i) at (7,-\y) {(T,\i)};
\node (hp\i) at (7,\y) {(H,\i)};

\draw (H.east) -- (h\i);
\draw (T.east) -- (t\i);
}

\draw (start.east) -- (H);
\draw (start.east) -- (T);

\end{tikzpicture}
\end{document}


Here's a version of the diagram constructed using the power tree-drawing package, forest, in combination with the TikZ libraries fit and backgrounds. Forest's relative node names are used to locate the coordinates to include in the shaded boxes, fit is used to ensure they are correctly included and backgrounds is used to add the shading behind the tree after the tree is constructed.

While Forest takes a little getting used to, it is worth the trouble, especially since the full power of TikZ remains available in addition to Forest's own facilities.

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{fit,backgrounds}
\begin{document}
\begin{forest}
before packing={
for tree={
grow'=0,
parent anchor=children,
child anchor=parent,
l sep'+=20pt,
},
where={level()>0}{
s sep'-=10pt,
}{}
},
where level=0{
font=\sffamily\bfseries,
tikz+={
\begin{scope}[on background layer, every node/.append style={fill opacity=.25, inner ysep=0pt}]
\node [fill=blue!50!cyan, fit=() ( |- !F.north) ( |- !L.south)] {};
\end{scope}
},
for children={
tikz+={
\begin{scope}[on background layer, every node/.append style={fill opacity=.25, inner ysep=0pt}]
\node [fill=blue!25!cyan, fit=() ( |- !F.north) ( |- !L.south)] {};
\node [fill=blue!75!cyan, fit=(!1) (!6) (!1 |- !F.north) (!6 |- !L.south)] {};
\node [fill=orange!50!yellow, fit=(!11) (!61) (!11 |- !F.north) (!61 |- !L.south)] {};
\end{scope}
},
}
}{},
where n children=0{
before typesetting nodes={
font=\sffamily\bfseries,
repeat=6{
append={
[, content/.option=n, font=\sffamily\bfseries,
[, content/.wrap 2 pgfmath args={(##1,##2)}{content("!uu")}{n("!u")}, no edge, font=\sffamily\bfseries,
]
]
}
}
}
}{}
[START
[H
]
[T
]
]
\end{forest}
\end{document}