0% found this document useful (0 votes)
13 views2 pages

Quiz1 Set1

Uploaded by

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

Quiz1 Set1

Uploaded by

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

Quiz 1.

COL106 Sem-I, 2024-25


Sunday September 1, 10 am - 11 am (25 points)

Name:

Entry Number: Group:


1. (2 points) Suppose we run the parenthesis matching algorithm using stacks on the following
input: (()()())(()(())). What is the maximum number of parentheses that will appear on the
stack at any point of time during this algorithm? Answer:

2. Consider the following pseudo-code. Assume A is an array of storing n elements from A[0] to
A[n-1].

S = Stack () ; # initialize an empty stack


i =0;
while ( i < n ) {
if S is empty {
S . push ( A [ i ]) ;
Increase i ;
}
Else if ( S . top > A [ i ])
S . pop () ;
Else {
S . push ( A [ i ]) ;
Increase i ;
}
}

Suppose we run the algorithm on the input A = [5, 4, 6, 8, 9, 4, 2, 3]


(a) (2 points) What is the content of the stack when the algorithm terminates? (Write from
bottom to top) Answer:
(b) (1 point) What is the worst-case running time of the above algorithm on a sequence of
size n? Use θ notation to express the answer. Answer:

3. (4 points) Given a binary tree, you would like to output the sum of the depths of all the leaf
nodes. Assume that each node of the tree stores left child, right child, and parent values only.
Complete the following pseudocode, it is called with FindDepthSum(r,0), where r is the root.
FindDepthSum(p,d) returns an integer. The parameter p is the reference to a Node and the
parameter d is an integer.

int FindDepthSum ( Node p , int d ) {


if ( p is a leaf )
return ______________ ;

if ( p is NULL )
return _____________ ;
else
return _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ;
}

Paper Set-1
Quiz 1. COL106 Page 2 of 2

4. State TRUE or FALSE:


(a) (1 point) 3n is O(2n ) Answer:
√ n
log n
(b) (1 point) 2 is O( ) Answer:
log n
5. (3 points) You are given a binary tree whose inorder traversal is BCDEFHJMPRSTUWX and
whose postorder traversal is EDCJHFMSRWXUTPB. What is the preorder traversal of the
tree? Answer:

6. (3 points) A tree has 10006 leaves. Half of the internal nodes have 3 children, and the other
half have 4 children each. How many nodes are there? Answer:

7. (2 points) What is the minimum number of nodes in an AVL tree of height 10?
Answer:

8. (4 points) Starting from an empty AVL tree, the following elements are inserted (in this order)
in the AVL tree: 6,9,10,3,2,1,8,7. You should use the rotation/balancing procedures described
in the class. Show the final AVL tree after the insert operations. After constructing the AVL
tree, delete the node containing element 1. Show the final tree.

9. (2 points) Suppose you have a doubly linked list and are allowed to use a constant amount of
other variables. You are maintaining a sequence of numbers using this doubly linked list, where
insert or delete can happen at either end. For each of the following operations, state whether
it can be done in constant time or not. (Yes/No)

i. Insert an element at the beginning. Answer:


ii. Insert an element at the end. Answer:
iii. Report the maximum element in the current list. Answer:
iv. Report the maximum element in the list before the last delete event (at either end).
Answer:

Paper Set-1

You might also like