Highlight a group of nodes in a tikz tree
The use of the hobby
package here leads to a very similar result to the picture posted by the OP:
I was in doubt if posting this answer because it's pretty much the same of the one given in Highlighting some nodes of a TikZ binomial tree, but it could be seen as a better example of use of the hobby
package. The point is that in the answer Highlighting some nodes of a TikZ binomial tree several [tension in]
s were used, but I saw that the result could be improved by simply adding more points along the curve. This from one hand complicate a bit the path definition, but I think is a good price to pay seeing the result.
The code:
\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{hobby,backgrounds,calc,trees}
\begin{document}
\begin{tikzpicture}
\node (f) {f}
child { node (g) {g}
child { node (a) {a}
}
child { node (b) {b}
}
}
child { node (h) {h}
child { node (c) {c}
}
};
\begin{pgfonlayer}{background}
\draw[blue,fill=blue,opacity=0.2](f.north) to[closed,curve through={($(f.south west)!0.5!(g.north west)$) .. (g.south west) .. (h.south west) .. (c.south west) .. (c.south east) .. ($(c.north east)!0.5!(h.south east)$) .. (h.east).. ($(h.north east)!0.5!(f.south east)$)}](f.north);
\draw[red,fill=red,opacity=0.2](g.north) to[closed,curve through={($(g.south west)!0.5!(a.north west)$) .. (a.south west) .. (a.south east) ..(b.south west) .. (b.south east) .. ($(b.north east)!0.5!(g.south east)$) }] (g.north);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
You can use Jake's answer to padded boundary of convex hull and layers:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,calc,trees}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\newcommand{\convexpath}[2]{
[
create hullnodes/.code={
\global\edef\namelist{#1}
\foreach [count=\counter] \nodename in \namelist {
\global\edef\numberofnodes{\counter}
\node at (\nodename) [draw=none,name=hullnode\counter] {};
}
\node at (hullnode\numberofnodes) [name=hullnode0,draw=none] {};
\pgfmathtruncatemacro\lastnumber{\numberofnodes+1}
\node at (hullnode1) [name=hullnode\lastnumber,draw=none] {};
},
create hullnodes
]
($(hullnode1)!#2!-90:(hullnode0)$)
\foreach [
evaluate=\currentnode as \previousnode using \currentnode-1,
evaluate=\currentnode as \nextnode using \currentnode+1
] \currentnode in {1,...,\numberofnodes} {
let
\p1 = ($(hullnode\currentnode)!#2!-90:(hullnode\previousnode)$),
\p2 = ($(hullnode\currentnode)!#2!90:(hullnode\nextnode)$),
\p3 = ($(\p1) - (hullnode\currentnode)$),
\n1 = {atan2(\y3,\x3)},
\p4 = ($(\p2) - (hullnode\currentnode)$),
\n2 = {atan2(\y4,\x4)},
\n{delta} = {-Mod(\n1-\n2,360)}
in
{-- (\p1) arc[start angle=\n1, delta angle=\n{delta}, radius=#2] -- (\p2)}
}
-- cycle
}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\node (f) {f}
child { node (g) {g}
child { node (a) {a}
}
child { node (b) {b}
}
}
child { node (h) {h}
child { node (c) {c}
}
};
\begin{pgfonlayer}{background}
\fill[red,opacity=0.3] \convexpath{a,g,b}{8pt};
\fill[blue,opacity=0.3] \convexpath{g,f,h,c,h,f}{8pt};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}