Question

1 Number of Levels in a Binary Tree (20 points) 1.1 Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In par- ticular, the algorithm should return 0 and 1 for the empty and single-node trees respectively. Please provide the pseudocode for your algorithm. What is the running time of your algorithm in the worst case using O) notation 1.2 Design a divide-and-conquer algorithm for computing the number of levels in a COMPLETE binary tree. In particular, the algorithm should return 0 and 1 for the empty and single-node trees respectively Please provide the pseudocode for your algorithm. What is the running time of your algorithm in the worst case using O) notation? (A complete binary tree refers to the type of binary trees where every level except the last level is completely filled and all the nodes are left justified. An example of the complete binarv tree is shown below Figure 1: An Example of Complete Binary Tree

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

As, it is the HomeworkLib policy to just answer one question in an answer.Hence, I have posted only answer to your first question and the rest you'll have to post as another question.Thankyou

Algorithm:

This is the algorithm to calculate the number of levels in the binary tree using the divide and conquer rule.

int numLevel(Node *roots)
{
if(roots == NULL) return
Initialising a Queue
Adding roots to QUEUE
Add a Dummy Node(NULL pointer) to Queue
num_of_levels = 1
while(QUEUE is not enmpty)
{
Dequeuing a node from Queue and assigning it to the root
if(roots == NULL and QUEUE is not empyy)
{
Adding NULL pointer(dummy node) to QUEUE
num_of_levels ++
}
if(roots->lt)
Adding roots->left element to QUEUE
if(roots->rt)
Adding roots->right element to QUEUE   
}
return num_of_levels
}

Add a comment
Know the answer?
Add Answer to:
Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In particular,...
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