binary search tree - What is the worst case complexity of the given program? -
program takes input balanced binary search tree n leaf nodes , computes value of function g(x) each node x. if cost of computing g(x) min{no. of leaf-nodes in left-subtree of x, no. of leaf-nodes in right-subtree of x} worst-case time complexity of program is
my solution :
for each internal node maximum cost n/2. every leaf node cost zero.
and number of internal nodes balanced binary tree : leaf nodes - 1
so total cost :
n/2 * (n-1)
o(n^2)
am right?
i have different solution. worst case tree happen complete tree got right , therefore number of leaf nodes n/2.
but case root node.
for nodes @ level 1: total cost 2*n/4=n/2
for nodes @ level 2: total cost 4*n/8=n/2
and on till last level log(n) , cost again = n/2
total cost therefore in worst case =n/2*log (n) = o(n*logn), in complete tree can happen last level not filled in asymptotic analysis ignore such intricate details.
Comments
Post a Comment