Question

You are given a general search tree (not necessarily AVL tree). Formulate a function that prints...

You are given a general search tree (not necessarily AVL tree). Formulate a function that prints out the n values of the search tree in ascending order. Determine the worst-case time and space complexities of your function.

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

If given a general search tree, and not an AVL tree, the inorder traversal will prints the n values of the search tree in ascending order. The function goes here:

inorder(Node *root)

{

if(root != NULL)

{

inorder(root->left);

print(root->info);

inorder(root->right);

}

}

The above traversal function will traverse the elements of the search tree in inorder.

But when the tree is severely skewed, that is, when the elements are inserted in ascending order, assume, 1, 2, 3, 4, 5 the tree looks as shown below:

In this case, the above given function will traverse this tree in O(n) time. This is the worst case efficiency of the binary search tree. Note that this happens only when the tree is not balanced. And the space complexity will be as usual, O(n), where n is the number of nodes.

If you have any further queries, just get back to me.

Add a comment
Know the answer?
Add Answer to:
You are given a general search tree (not necessarily AVL tree). Formulate a function that prints...
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