Design a divide-and-conquer algorithm for computing the number of levels in a bi
ID: 3883389 • Letter: D
Question
Design a divide-and-conquer algorithm for computing the number of levels in a 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? 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 binary tree is shown below.)Explanation / Answer
As, it is the chegg 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
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.