# Visualization of nested Piecewise output with a tree diagram (or something similar)

I have outputs which are in the form of nested Piecewise functions and I want to represent these diagrammatically. There are multiple levels in general. A simple example with two levels is:

Piecewise[{{Piecewise[{{f = a, y > 0}, {f = b, y <= 0}}], z > 0}, {f = c, z <= 0}}]


where y and z are Reals. I would like to express this result as a tree diagram (or something similar), where vertices would be labeled by the conditions and leaves would be the values of f. How can I write a function which can turn this output into such a diagram?

• Have you tried TreeForm? – bill s Oct 25 '16 at 15:24
• TreeForm looks like it will be useful. Thank you @bill s – JohnF Oct 25 '16 at 18:42

You might also want to use PiecewiseExpand

p = Piecewise[{{Piecewise[{{f = a, y > 0}, {f = b, y <= 0}}], z > 0},
{f = c, z <= 0}}];

p2 = p // PiecewiseExpand;

p == p2 // Simplify

(*  True  *)

p2 // TreeForm p = Piecewise[{{Piecewise[{{f = a, y > 0},
{f = b, y <= 0}}], z > 0}, {f = c, z <= 0}}];
TreeForm[p] 