Question

In: Computer Science

function TreeSuccessor(x) if x.right != NIL return TreeMinimum(x.right) // TreeMinimum is on page 291 y =...

function TreeSuccessor(x)
    if x.right != NIL
        return TreeMinimum(x.right)  // TreeMinimum is on page 291
    y = x.p   // parent
    while y != NIL and x == y.right
        x = y
        y = y.p
    return y

Tree-Minimum(x)

1 While x.left != NIL

2     x = x.left

3 return x

Consider the following algorithm.

procedure MysteryWalk(x)
    y = TreeMinimum(x)
    while y != NIL
        print y
        y = TreeSuccessor(y)

Determine what this algorithm does, and compute its running time, giving a justification for your answer.

Solutions

Expert Solution

procedure MysteryWalk(x)
    y = TreeMinimum(x)
    while y != NIL
        print y
        y = TreeSuccessor(y)


This procedure print all the nodes in Sorted Manner. 

We can say it does an Inorder Traversal of Binary Search tree

Lets see How ?
1. Finds the left most child which is the minimum number.
2. Starts printing the sucessors, which means to find the next element largest than it 
3. So in Binary search tree if we do in this manner which means we will get the sorted order and Results in Inorder traversal of tree.


Time Complexity

procedure MysteryWalk(x)
    y = TreeMinimum(x) ======> O(N) time, If the tree is ledt skewed we travel all the nodes so Its O(N)
    while y != NIL ====>We will print all the nodes that means we will traverse all at once , so while loop will run N time
        print y
        y = TreeSuccessor(y)====> Sucessor depends on height of tree , So O(h)


So The Overall Time compkexity is , O(N) + O(Nh) => O(Nh)

For left skewed tree if Height is N , then O(N2)



Thanks , let me know if there is any concern.


Related Solutions

Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.
Given:   S = [ 2, 1, 3, 4, 7, 5 ] Z = 8   Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.   Code in Java 8. 
Esther consumes goods X and Y, and her utility function is      U(X,Y)=XY+Y For this utility function,...
Esther consumes goods X and Y, and her utility function is      U(X,Y)=XY+Y For this utility function,      MUX=Y      MUY=X+1 a. What is Esther's MRSXY? Y/(X + 1) X/Y (X + 1)/Y X/(Y + 1) b. Suppose her daily income is $20, the price of X is $4 per unit, and the price of Y is $1 per unit. What is her best choice?      Instructions: Enter your answers as whole numbers.      X =      Y =      What is Esther's utility when her...
Suppose that the utility function of a consumer is U(x,y) = x ¼y ¾, where x...
Suppose that the utility function of a consumer is U(x,y) = x ¼y ¾, where x and y are the quantities of the good X and good Y consumed, respectively. The consumer's income is 400. (a) What is the demanded bundle when the price of good X is 10 and the price of good Y is 10? (b) Redo part (a) when the price of good X is doubled? (c) Redo part (a) when the price of good Y is...
Q(x,y) is a propositional function and the domain for the variables x & y is: {1,2,3}....
Q(x,y) is a propositional function and the domain for the variables x & y is: {1,2,3}. Assume Q(1,3), Q(2,1), Q(2,2), Q(2,3), Q(3,1), Q(3,2) are true, and Q(x,y) is false otherwise. Find which statements are true. 1. ∀yƎx(Q(x,y)->Q(y,x)) 2. ¬(ƎxƎy(Q(x,y)/\¬Q(y,x))) 3. ∀yƎx(Q(x,y) /\ y>=x)
Consider the following function: f (x , y , z ) = x 2 + y...
Consider the following function: f (x , y , z ) = x 2 + y 2 + z 2 − x y − y z + x + z (a) This function has one critical point. Find it. (b) Compute the Hessian of f , and use it to determine whether the critical point is a local man, local min, or neither? (c) Is the critical point a global max, global min, or neither? Justify your answer.
Your utility function over x and y is U ( x , y ) = l...
Your utility function over x and y is U ( x , y ) = l n ( x ) + 0.25 y. Your income is $20. You don’t know the prices of x or y so leave them as variables (p x and p y). a) (8 points) Find x*, your demand function for x. Find y*, your demand function for y. b) (10 points) Find the cross-price elasticity of demand for x (E x ∗ , p y:...
If the function u (x, y) is a harmonic conjugate of v (x, y) prove that...
If the function u (x, y) is a harmonic conjugate of v (x, y) prove that the curves u (x, y) = st. and v (x, y) = stations. are orthogonal to each other. These curves are called level curves. Now consider the function f (z) = 1 / z defined throughout the complex plane except the beginning of the axes. Draw them level curves for the real and imaginary part of this function and notice that they are two...
4. The joint density function of (X, Y ) is f(x,y)=2(x+y), 0≤y≤x≤1 . Find the correlation...
4. The joint density function of (X, Y ) is f(x,y)=2(x+y), 0≤y≤x≤1 . Find the correlation coefficient ρX,Y . 5. The height of female students in KU follows a normal distribution with mean 165.3 cm and s.d. 7.3cm. The height of male students in KU follows a normal distribution with mean 175.2 cm and s.d. 9.2cm. What is the probability that a random female student is taller than a male student in KU?
Jim’s utility function is U(x, y) = xy. Jerry’s utility function is U(x, y) = 1,000xy...
Jim’s utility function is U(x, y) = xy. Jerry’s utility function is U(x, y) = 1,000xy + 2,000. Tammy’s utility function is U(x, y) = xy(1 - xy). Oral’s utility function is -1/(10 + xy. Billy’s utility function is U(x, y) = x/y. Pat’s utility function is U(x, y) = -xy. a. No two of these people have the same preferences. b. They all have the same preferences except for Billy. c. Jim, Jerry, and Pat all have the same...
Suppose X and Y have joint probability density function f(x,y) = 6(x-y) when 0<y<x<1 and f(x,y)...
Suppose X and Y have joint probability density function f(x,y) = 6(x-y) when 0<y<x<1 and f(x,y) = 0 otherwise. (a) Indicate with a sketch the sample space in the x-y plane (b) Find the marginal density of X, fX(x) (c) Show that fX(x) is properly normalized, i.e., that it integrates to 1 on the sample space of X (d) Find the marginal density of Y, fY(y) (e) Show that fY(y) is properly normalized, i.e., that it integrates to 1 on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT