rpart complexity parameter confusion I'm a little bit confused on the calculation for CP in the summary of an rpart object.
Take this example
df <- data.frame(x=c(1, 2, 3, 3, 3), 
                 y=factor(c("a", "a", "b", "a", "b")),
                 method="class")
mytree<-rpart(y ~ x, data = df, minbucket = 1, minsplit=1)
summary(mytree)

Call:
rpart(formula = y ~ x, data = df, minbucket = 1, minsplit = 1)
  n= 5 

    CP nsplit rel error xerror      xstd
1 0.50      0       1.0      1 0.5477226
2 0.01      1       0.5      2 0.4472136

Variable importance
  x 
100 

Node number 1: 5 observations,    complexity param=0.5
  predicted class=a  expected loss=0.4  P(node) =1
    class counts:     3     2
   probabilities: 0.600 0.400 
  left son=2 (2 obs) right son=3 (3 obs)
  Primary splits:
      x < 2.5 to the left,  improve=1.066667, (0 missing)

For the root node, I would've thought the CP should be 0.4 since the probability of misclassifying an element in the root is 0.4 and the tree size at the root is 0.  How is 0.5 the correct CP?
 A: As far as I know, the complexity parameter is not the error in that particular node. It is the amount by  which splitting that node improved the relative error. So in your example, splitting the original root node dropped the relative error from 1.0 to 0.5, so the CP of the root node is 0.5. The CP of the next node is only 0.01 (which is the default limit for deciding when to consider splits). So splitting that node only resulted in an improvement of 0.01, so the tree building stopped there.
A: It is not particularly easy to follow the rpart calculations for classification.  In addition, although the 'Long Intro' suggests that gini is used for classification, it seems that cost complexity pruning (and hence the values for cp) is reported based on accuracy rather than gini.  In this case, we can work through the calculations and replicate the 0.4 queried in the original question. Firstly, create the tree
df <- data.frame(x=c(1,2,3,3,3), y=factor(c("a", "a", "b", "a", "b")))
mytree <- rpart(y ~ x, data = df, minbucket = 1, minsplit=1, method="class")

and then typing
print(mytree)

we get
node), split,  n, loss, yval, (yprob)
1)     root    5   2     a (0.6000000 0.4000000)  
2)     x< 2.5  2   0     a (1.0000000 0.0000000) *
3)     x>=2.5  3   1     b (0.3333333 0.6666667) *

The 'loss' column is not gini (which you might have expected it to be). It is the number of errors made.  
The point at which this one split tree collapses (based on accuracy) is when
$$ 2 + \alpha * 1 = 1 + \alpha * 2$$ 
(where the first 2 above is the loss in the pruned tree and the second 2 is the number of terminal nodes in the full tree).
Solving for alpha, gives an alpha of 1.
As mentioned in an answer above, in the cptable, the error in the top line is scaled to 1 and then cp is scaled by the same amount.
The error in the top line is the number of errors in a tree with no splits ie 2.
Hence the alpha of 1 is scaled by dividing by 2 to give 0.50.
It is hard to read the C code in rpart, but the above is what I think it is doing.
A: The complexity parameter $\alpha$ specifies how the cost of a tree $C(T)$ is penalized by the number of terminal nodes $|T|$, resulting in a regularized cost $C_{\alpha}(T)$ (see http://cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf, Section 4). 
$C_{\alpha}(T) = C(T) + \alpha |T|$                  
Small $\alpha$ results in larger trees and potential overfitting, large $\alpha$ in small trees and potential underfitting. 
