设整棵树(含根)当前节点数为 n。对任意节点 i,要让它被删除,必须在某一步选到它自己或它的某个祖先。
记 Depth(i) 为节点 i 的祖先个数(不含自身),则“选到祖先链上的任一点”这个事件的大小为 Depth(i)+1 个节点,因此
Pr(i 在下一步被删除)=nDepth(i)+1.
用指示变量 Xi 表示“节点 i 在某一步中是被选中的节点(从而触发一次删除)”。总步数为
X=X1+X2+⋯+Xn.
在给定节点 i 最终会被删除的前提下,i 自己成为触发删除的被选节点的条件概率为
Pr(i 被选中∣i 被删除)=Depth(i)+11.
由期望线性性得到
E[X]=i∑E[Xi]=i∑Depth(i)+11.
Original Explanation
Every node in the tree has an equal probability of being selected. If a node is to be removed, we know that either the node itself, or one of the node's ancestors must have been chosen.
The number of ancestors of a node i is Depth[i]
P(i is removed)=Depth[i] / Size of Tree
P(i is chosen)=1 / Size of Tree
P(i is removed ∣ i is chosen)=1
We can use Bayes theorem to solve this problem where: P(A∣B)=P(B)P(B∣A)∗P(A) and therefore P(i is chosen ∣ i is removed)=1/Depth[i]
We now use Linearity of Expectation to find the expected number of subtree removals.
Define indicator variable Xi=1 if i node was chosen.
X=X1+X2…Xn
E[X]=i=1∑nE[Xi]
E[X]=∑1∗P(Xi was chosen ∣i was removed)
E[X]=i=1∑nDepth[i]1