40-Implementation of AVL Tree-14!11!2024
40-Implementation of AVL Tree-14!11!2024
Answer Key
SLOT B2+TB2
Programme Name & Branch: B.Tech. Computer Science and Engineering
Course Name & code: Data Structures and Algorithms (BCSE202L)
Recursive algorithm:
O(n) is the time complexity, where n is the size of the linked list.
Ans) 2,1,4,3,6,5,7
Ans) +, -, *, a, b, c, d
Ans) The no. of nodes with 2 children is always one less than no.
of leaves. So, the answer is 19.
Explanation:
Sum of all degrees = 2 * |E|.
Here considering tree as a k-ary tree :
Sum of degrees of leaves + Sum of degrees for Internal Node
except root + Root's degree = 2 * (No. of nodes - 1).
Putting values of above terms,
L + (I-1)*(k+1) + k = 2 * (L + I - 1)
L + k*I - k + I -1 + k = 2*L + 2I - 2
L + K*I + I - 1 = 2*L + 2*I - 2
K*I + 1 - I = L
(K-1)*I + 1 = L
Given k = 2, L=20
==> (2-1)*I + 1 = 20
==> I = 19
==> T has 19 internal nodes which are having two children.
Answer is 3.
1
\
[2..7]
Therefore count is 26 = 64
Another Explanation:
Consider these cases,
1234567
1234576
1765432
1765423
7654321
7654312
7123456
7123465
For height 6, we have 2 choices. Either we select the root as 1 or
7.
Suppose we select 7.
Now, we have 6 nodes and remaining height = 5.
So, now we have 2 ways to select root for this sub-tree also.
Now, we keep on repeating the same procedure till remaining
height = 1
For this last case also, we have 2 ways.
Therefore, total number of ways = 26= 64
4. a) Explain the BUILD_MAX_HEAP(A) operation/algorithm on a 10 CO5 BL3
given array A. Also, analyse its time complexity? (5 Marks)
// Driver's Code
int main()
{
int arr[] = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17};
// Function call
buildHeap(arr, N);
printHeap(arr, N);
return 0;
}
b) Consider a max heap, represented by the array: 40, 30, 20, 10,
15, 16, 17, 8, 4. Now consider that a value 35 is inserted into this
heap. After insertion, the new heap is? (2 Marks)
Ans) The new heap is:
40, 35, 20, 10, 30, 16, 17, 8, 4, 15
Explanation:
The array 40, 30, 20, 10, 15, 16, 17, 8, 4 represents following
heap:
40
/ \
30 20
/ \ / \
10 15 16 17
/ \
8 4
Ans) Answer is 3.
Explanation:
Ans)
Deletion in AVL tree is carried out using 2 steps:
a) Deleting the element is same as in BST
b) Rebalancing the imbalance node by performing rotations.