1. 程式人生 > >演算法導論[Exercises 14.2-4]

演算法導論[Exercises 14.2-4]

Let * be an associative binary operator, and let a be a field maintained in each node of a red-black tree. Suppose that we want to include in each node x an additional field f such that f[x] = a[x1] * a[x2] * ··· * a[xm], where x1, x2,..., xm is the inorder listing of nodes in the subtree rooted at x

. Show that the f fields can be properly updated in O(1) time after a rotation. Modify your argument slightly to show that the size fields in order-statistic trees can be maintained in O(1) time per rotation. 

1)容易得到不變式 f(x) = f(left[x])*a[x]*f(right[x])]

2)在一次反轉過程中,無非是a,b,r三個子樹的調整,對於三個子樹的f域不需要調整,因為不影響他們內部的順序,

3)對於求B的f域, f(B) = f(b)*a[B]*f(r)

4)求A的f域f(A) = f(a)*a[A]*f(B) 

以上以left-rotate為例,right-roate類似

                       B                                                               A

               /             /                         -->                         /          /

      A                         r                                               a                  B

a            b                                                                              b                r