0% found this document useful (0 votes)
8 views

Unit 1(Long Answers)

Uploaded by

VENU DINO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit 1(Long Answers)

Uploaded by

VENU DINO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

UNIT -1 LONG ANSWERS

1)COMPARE AND CONTRAST TIME COMPLEXITY AND SPACE


COMPLEXITY WITH EXAMPLES?

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(log⁡n) 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(log⁡n) times. This gives the total time complexity:
O(n)×O(log⁡n)=O(nlog⁡n)
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(log⁡n) 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(nlog⁡n).
This is how we arrive at the overall time complexity of the
code being O(nlog⁡n).

6) DISCUSS HOW SINGLE AND DOUBLE ROTATIONS


PERFORMED IN AVL-TREE?
A) There are four rotations and they are classified
into two types.
1. RR Rotation (Single Left Rotation):-
When BST becomes unbalanced, due to a node is inserted
into the right subtree of the right subtree of A, then we
perform RR rotation, RR rotation is an anticlockwise rotation,
which is applied on the edge below a node having balance
factor -2

In above example, node A has balance factor -2 because a


node C is inserted in the right subtree of A right subtree. We
perform the RR rotation on the edge below A.

2. LL Rotation (Single Right Rotation ):-


When BST becomes unbalanced, due to a node is inserted
into the left subtree of the left subtree of C, then we perform
LL rotation, LL rotation is clockwise rotation, which is applied
on the edge below a node having balance factor 2.
In above example, node C has balance factor 2 because a
node A is inserted in the left subtree of C left subtree. We
perform the LL rotation on the edge below A.

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

A node B has been inserted into the right subtree of A


the left subtree of C, because of which C has become
an unbalanced node having balance factor 2. This case
is L R rotation where: Inserted node is in the right
subtree of left subtree of C.

As LR rotation = RR + LL rotation, hence RR


(anticlockwise) on subtree rooted at A is performed
first. By doing RR rotation, node A, has become the left
subtree of B.
After performing RR rotation, node C is still unbalanced,
i.e., having balance factor 2, as inserted node A is in the
left of left of C

Now we perform LL clockwise rotation on full tree, i.e.


on node C. node C has now become the right subtree
of node B, A is left subtree of B

Balance factor of each node is now either -1, 0, or 1,


i.e. BST is balanced now.

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
As RL rotation = LL rotation + RR rotation, hence, LL
(clockwise) on subtree rooted at C is performed first. By
doing RR rotation, node C has become the right subtree
of B.

After performing LL rotation, node A is still unbalanced,


i.e. having balance factor -2, which is because of the
right-subtree of the right-subtree node A.

Now we perform RR rotation (anticlockwise rotation) on


full tree, i.e. on node A. node C has now become the
right subtree of node B, and node A has become the left
subtree of B.

Balance factor of each node is now either -1, 0, or 1, i.e.,


BST is balanced now.

7) DISCUSS ABOUT THE DELETION OPERATION IN B-TREE?


A)
8)EXPLAIN THE INSERT AND DELETE OPERATIONS OF AVL-
TREE?
(OR)
9)DESCRIBE ABOUT THE OPERATIONS OF AVL-TREE?
A) INSERTION OPERATION IN AVL TREE
There are four rotations and they are classified
into two types.

1. RR Rotation (Single Left Rotation):-


When BST becomes unbalanced, due to a node is inserted
into the right subtree of the right subtree of A, then we
perform RR rotation, RR rotation is an anticlockwise rotation,
which is applied on the edge below a node having balance
factor -2

In above example, node A has balance factor -2 because a


node C is inserted in the right subtree of A right subtree. We
perform the RR rotation on the edge below A.
2. LL Rotation (Single Right Rotation ):-
When BST becomes unbalanced, due to a node is inserted
into the left subtree of the left subtree of C, then we perform
LL rotation, LL rotation is clockwise rotation, which is applied
on the edge below a node having balance factor 2.

In above example, node C has balance factor 2 because a


node A is inserted in the left subtree of C left subtree. We
perform the LL rotation on the edge below A.

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

A node B has been inserted into the right subtree of A


the left subtree of C, because of which C has become
an unbalanced node having balance factor 2. This case
is L R rotation where: Inserted node is in the right
subtree of left subtree of C.

As LR rotation = RR + LL rotation, hence RR


(anticlockwise) on subtree rooted at A is performed
first. By doing RR rotation, node A, has become the left
subtree of B.

After performing RR rotation, node C is still unbalanced,


i.e., having balance factor 2, as inserted node A is in the
left of left of C

Now we perform LL clockwise rotation on full tree, i.e.


on node C. node C has now become the right subtree
of node B, A is left subtree of B

Balance factor of each node is now either -1, 0, or 1,


i.e. BST is balanced now.

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

As RL rotation = LL rotation + RR rotation, hence, LL


(clockwise) on subtree rooted at C is performed first. By
doing RR rotation, node C has become the right subtree
of B.

After performing LL rotation, node A is still unbalanced,


i.e. having balance factor -2, which is because of the
right-subtree of the right-subtree node A.

Now we perform RR rotation (anticlockwise rotation) on


full tree, i.e. on node A. node C has now become the
right subtree of node B, and node A has become the left
subtree of B.

Balance factor of each node is now either -1, 0, or 1, i.e.,


BST is balanced now.

DELETION OPERATIONS IN AVL TREE


10) CREATE AN AVL TREE USING THE FOLLOWING DATA
ENTERED AS AN SEQUENTIAL SET .SHOW THE BALANCE
FACTOR IN THE RESULTING TREE :7,10,14,23,33,56,66,70,80?
A)
11) EXPLAIN ABOUT THE OPERATIONS OF B-TREE?
A)
DELETION OPERATION IN B-TREE
12) COMPARE AND CONTRAST AVL AND B TREE?
A)

13) GIVE AN ANALYSIS OF THE B-TREE INSERTION PROCESS?


A)

You might also like