How to arrange nodes inside sub-graph in layered layout or tree layout way

My subgraph contains some nodes inside it and connections among them. I want to arrange these nodes as per tree or layered layout.
Currenetly, when i expand or contract subgraph inside subgraph1 then it expands and hides other nodes inside the subgraph1. I just wanted to arrange everything inside the parent subgraph so that there will not be overlapping.
How can it be done in subgraphs?

Look at the SubGraphApp sample. There is a recursive layout done there.

Thanks for the reply.
I had seen the SubGraphApp sample where under LayoutDocument botton there is code where LayoutSubGraph method is recursively called. Initially I thought it is only to arrange nodes in document not for arranging nodes in subgraph whenever i expand or collapse nodes inside subgraph. So I asked the same question again. I will look into it in detail.
Thanks
yogesh