Unit 1(Long Answers)
Unit 1(Long Answers)
A)
2)CONSTRUCT B-TREE OF ORDER -4 INSERTING VALUES WITH
THE FOLLOWING SET OF DATA (5,2,21,9,1,13,2,7,10,12,4,8?
A)
3) COMPARE AND CONTRAST ABOUT 0MEGA , THETA AND
BIG-0 NOTATIONS WITH EXAMPLES ?
(OR)
4)EXPLAIN IN DETAIL ABOUT ASYMPTOTIC NOTATIONS?
A)
5) CALCULATE AND EXPLAIN THE TIME COMPLEXITY OF THE
FOLLOWING CODE:
a. int i, j, k = 0;
b. for (i = n / 2; i <= n; i++) {
c. for (j = 2; j <= n; j = j * 2) {
d. k = k + n / 2; }
e.}
A) int i,j,k=0;
for(i=n/2i<=n;i++}
{
for(j=2;j<=n;j=j*2)
{
k=k+n/2;
}
}
Break down the code and analyze its time complexity.
The given code consists of two nested loops:
Code:
int i, j, k = 0;
for (i = n / 2; i <= n; i++) { // Loop 1
for (j = 2; j <= n; j = j * 2) { // Loop 2
k = k + n / 2; // Operation inside inner loop
}
}
Step-by-step analysis:
Outer loop:
for (i = n / 2; i <= n; i++) { ... }
Initialization: The loop variable i starts at i=n/2.
Condition: The loop runs as long as i≤n.
Update: The loop increments i by 1 in each iteration.
Let’s calculate how many iterations this loop performs:
i starts at n/2, and it runs until i=n. The total number of
iterations can be calculated as:
Number of iterations=n - n/2 = n/2
Therefore, the outer loop runs n/2 times.
In Big-O notation, constants are dropped, so the time
complexity of the outer loop is O(n).
Inner loop:
Initialization: The loop variable j starts at j=2.
Condition: The loop runs as long as j≤n.
Update: The loop multiplies j by 2 in each iteration.
This means the loop variable j takes the values:
2,4,8,16,…,2k
Where 2k is the largest power of 2 that is less than or equal
to n. The number of iterations is determined by how many
times we can multiply j by 2 before it exceeds n.
In mathematical terms, the number of iterations is:
log 2n
Thus, the inner loop runs O(log n) times.
Body of the inner loop:
k = k + n / 2;
This is a constant time operation O(1), since the value of
k is simply being updated by adding a constant value
n/2.
Total Time Complexity:
Now, let's combine the two loops to compute the total time
complexity.
The outer loop runs O(n) times.
For each iteration of the outer loop, the inner loop runs
O(logn) times.
The operation inside the inner loop takes O(1) time, but
it's performed during every iteration of the inner loop.
So, for each of the O(n) iterations of the outer loop, the inner
loop runs O(logn) times. This gives the total time complexity:
O(n)×O(logn)=O(nlogn)
Final Breakdown:
Outer loop: Runs O(n) times (from n/2 to n).
Inner loop: For each iteration of the outer loop, the
inner loop runs O(logn) times (doubling j each time).
Total time complexity: Since the loops are nested, the
overall time complexity is the product of the outer and
inner loops: O(nlogn).
This is how we arrive at the overall time complexity of the
code being O(nlogn).
3. LR Rotation:-
LR rotation = RR rotation + LL rotation, i.e., first RR rotation is
performed on subtree and then LL rotation is performed on
full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.
State Action
4. RL Rotation:-
R L rotation = LL rotation + RR rotation, i.e., first LL rotation is
performed on subtree and then RR rotation is performed on
full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.
State Action
3. LR Rotation:-
LR rotation = RR rotation + LL rotation, i.e., first RR rotation is
performed on subtree and then LL rotation is performed on
full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.
State Action
4. RL Rotation:-
R L rotation = LL rotation + RR rotation, i.e., first LL rotation is
performed on subtree and then RR rotation is performed on
full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.
State Action
A node B has been inserted into the left subtree of C the
right subtree of A, because of which A has become an
unbalanced node having balance factor - 2. This case is
RL rotation where: Inserted node is in the left subtree of
right subtree of A