# Chapter 13: Manipulation of density estimates. Code

## The order of going through rectangles

We illustrate algorithm LeafsFirst in the case of calculating a level set tree.

```N<-c(16,16)
pcf<-sim.data(N=N,type="mulmod")
lst<-leafsfirst(pcf)
leafsfirst.visu(lst,pcf,lkmbound=28)
```

Next we illustrate algorithm LeafsFirst in the case of calculating a shape tree.

```st<-leafsfirst(pcf,propor=0.1)
leafsfirst.visu(st,pcf,propor=0.1,lkmbound=28)
```

## Dynamic illustration

We illustrate algorithm LeafsFirst in the case of calculating a shape tree. Function ``plottwin'' makes 2 windows. The first window shows a level set and the second window shows the shape tree, with the vector representation. Click with the mouse at the second window to let the algorithm proceed.

```N<-c(8,8)                     # size of the grid
pcf<-sim.data(N=N,type="fox") # piecewise constant function
lev<-0.002                    # level of the level set
refe<-c(0,0)                  # reference point
st<-leafsfirst(pcf,lev,refe)  # shape tree

plottwin(st,pcf,lev,refe)
```

We used a level set of the fox density and we applied a very sparse grid to approximate the function.