Segment tree - query complexity -
i having problems understanding segment tree complexity. clear if have update function has change 1 node, complexity log(n). have no idea why complexity of query(a,b), (a,b) interval needs checked, log(n). can provide me intuitive / formal proof understand this?
there 4 cases when query interval (x,y)
find(r,x,y) //r node % case 1 if r.first = x , r.last = y return {r} % case 2 if y <= r.middle return find(r.leftchild, x, y) % case 3 if x >= r.middle + 1 return find(r.rightchild, x, y) % case 4 p = find(r.leftchild, x, r.middle) q = find(r.rightchild, r.middle + 1, y) return p union q. intuitively, first 3 cases reduce level of tree height 1, since tree has height log n, if first 3 cases happen, running time o(log n).
for last case, find() divide problem 2 subproblems. however, assert can happen @ once. after called find(r.leftchild, x, r.middle), querying r.leftchild interval [x, r.middle]. r.middle same r.leftchild.last. if x > r.leftchild.middle, case 1; if x <= r.leftchild, call
find ( r.leftchild.leftchild, x, r.leftchild.middle ); find ( r.leftchild.rightchild, r.leftchild.middle + 1, , r.leftchild.last ); however, second find() returns r.leftchild.rightchild.sum , therefore takes constant time, , problem not separate 2 subproblems (strictly speaking, problem separated, though 1 subproblem takes o(1) time solve).
since same analysis holds on rightchild of r, conclude after case4 happens first time, running time t(h) (h remaining level of tree) be
t(h) <= t(h-1) + c (c constant) t(1) = c which yields:
t(h) <= c * h = o(h) = o(log n) (since h height of tree) hence end proof.
this first time contribute, hence if there problems, please kindly point them out , edit answer.
Comments
Post a Comment