Question


8. You are given a binary tree T (V,E) with a designated root node. In addition, there is an array z with a value for each node in V. Define a new array z as follows: for each u e V, zu is the maximum of the -values associated with us descendants. Give a linear-time algorithm which calculates the entire z array. (7 points)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

When we represent the values of nodes of the binary tree T in the form of an array.

We know that, any node present at the position 'i' in the index has :

Left child at position = 2i+1 and

Right child at position = 2i+2

Therefore, for every i in the array, we just need to get the maximum of the value present at '2i+1' and '2i+2'.

This will get you the answer in linear time.

ALGORITHM:

for i = 0:sizeof(x)

if( 2i+2 <= sizeof(x) )

temp = max( x[2i+1] , x[2i+2] )

else

temp = -1

z[i] = temp

return z

Add a comment
Know the answer?
Add Answer to:
8. You are given a binary tree T (V,E) with a designated root node. In addition,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT