Question

Write a recursive algorithm in pseudo code that returns the height of a binary tree in...

Write a recursive algorithm in pseudo code that returns the height of a binary tree in linear time iff all nodes meet the balance-property of AVL-trees, and -1 otherwise. You are not allowed to store the height in the nodes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

the function balanced(), takes the root of the tree and a variable for height as input. It recursively checks if the subtrees at a given root are also balanced and also keeps track of the height of the tree at the same time. The getHeight function is just a helper function that calls the balanced function and returns -1 if the avl tree given isn't balanced. The height is maintained only in the variable. The node is assumed to have a left and right pointer.

The balanced function runs in linear time as every node is visited only once, so if there are N nodes in the tree, the time taken will be in the order of O(n).

Add a comment
Know the answer?
Add Answer to:
Write a recursive algorithm in pseudo code that returns the height of a binary tree in...
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