This example shows how to work with a layer tree. The basic component for building layer trees is the LayerNode, and there are different types of containers for automatically adding a map's layers to the tree. The tree configuration of this example is pure JSON and can be edited live by clicking on the "Show/Edit Tree Config" button below the layers panel.
The js is not minified so it is readable. See tree.js.
Also see the Layer Container example for drag-n-drop support.