













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A set of practice questions and answers related to data structures and algorithms, focusing on stacks, queues, linked lists, trees, graphs, and hash tables. It covers topics such as stack and queue operations, postfix expression evaluation, recursive list deletion, graph traversal, binary tree construction, heap properties, topological sorting, and avl tree rotations. The questions are designed to test understanding of fundamental concepts and problem-solving skills in data structures and algorithms. Useful for students preparing for exams or viva voce in data structures and algorithms courses. It includes detailed solutions and explanations for each question, enhancing learning and comprehension. The content is structured to facilitate quick review and practice, making it an effective study aid. It also explores the application of dijkstras algorithm and huffman trees, providing a comprehensive overview of essential data structure and algorithm topics.
Typology: Exams
1 / 21
This page cannot be seen from the preview
Don't miss anything!
(a) Consider the stack data structure, supporting two operations push and pop, as discussed in class. Suppose that for the above sequence, each letter (such as E) corresponds to a push of that letter onto the stack and each asterisk (*) corresponds to a pop operation on the stack. Show the sequence of values returned by the pop operations.
(b) Consider the queue data structure, supporting two operations insert and remove, as discussed in class. Suppose that for the above sequence, each letter (such as E) corresponds to an insert of that letter into the queue and each asterisk (*) corresponds to a remove operation on the queue. Show the sequence of values returned by the remove operations.
Answer : a). Answer for stack Output: SYEUQTSAONIE.
Public Stack mergeSortedStacks(Stack A, Stack B) { …... } Answer :
Answer :-
a hash table with collisions resolved by chaining. Let the table have 9 slots, and let the hash function be h ( k )= k mod9.
Solution: Let us number our slots 0,1,…,8.
Then our resulting hash table will look like following:
for j= 1 to 10
MAKESET(j)
end for
UNION(1,2) ; UNION(1,3); UNION(4,5); UNION(4,6); UNION(1,6); UNION(7,8);
UNION(9,10); UNION(7,10); UNION(3,8); FIND(4); FIND(3);
Give the tree from executing the above steps using union-by-rank with no path-compression Be sure to label the nodes in the final tree including their final ranks.
Give the tree from executing the above steps using union-by-rank with path-compression Be sure to label the nodes in the final tree including their final ranks.
Answer :
below. char freq encoding
which one or more of the following are possible values for the frequency of the character I. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Answer :
6 ≤ freq(I) ≤ 15. Since I is not touched until after merging L with {B, F, H}, freq(I) ≥ freq(L) = 5 and freq(I) ≥ freq({B, F, H}) = 2 + 1 + 3 = 6. • Since I is merged with {B, F, H, L} instead of M or S, freq(I) ≤ freq(M) = 15 and freq(I) ≤ freq(S) = 15
Answer : will swap 6 with its larger child until 6 reaches a position where it satisfies the max-heap property. The correct heap is:
A common mistake on this problem was to use something resembling , recursively moving the larger child up to replace the deleted parent. This does not result in a valid BST.
Answer :Highlighted (ingreen) are the edges picked to make a MST. In the right side of MST, we could either pick edge ‘a’ or ‘b’. In the left side, we could either pick ‘c’ or ‘d’ or ‘e’ in MST. There are 2 options for one edge to be picked and 3 options for another edge to be picked. Therefore, total 2*3 possible MSTs.
such that the root of tree is 12 and the root of left subtree is 7?
Answer :Number of nodes in left subtree = 11 {1, 2, 3, 4….11} Number of nodes in the right subtree = 8 {13, 14, ….20} Since for the left subtree root is 7 Number of elements in the left part of left subtree = 6 {1, 2, 3..6} Number of elements in the right part of left subtree = 4 {8, 9, 10, 11} We know number of Binary Search trees with n nodes = (C(2n,n)/n+1) Number of BST with 6 nodes = (C(12,6)/7) = 132 Number of BST with 4 nodes = (C(8,4)/5) = 14 Number of BST with 8 nodes = (C(16,8)/9) =1430 Total number of BST = 2642640
is not?
Answer :A binary tree is max-heap if it is a complete binary tree (A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible) and it follows the max-heap property (value of each parent is greater than or equal to the values of its children). A) is not a max-heap because it is not a complete binary tree
B) is a max-heap because it is a complete binary tree and follows max-heap property.
C) is not a max-heap because 8 is a child of 5 in this tree, so violates the max-heap property.
D) is not a max-heap because 8 is a child of 5 in this tree, so violates the max-heap property.
There are many other nodes in this tree which violate max-heap property in this tree.
collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. What is the maximum, minimum, and average chain lengths in the hash table.
Answer :-Following are values of hash function for all keys 5 ----- > 28 -----> 19 ----- >1{chained with 28} 15 ------ > 20 ----- > 33 ----- >6{chained with 15} 12 ----- > 17 -------- >
CE No
CF Yes
FE No
DF No
BF No
Resultant MST:
Note: As there are edges with equal weights, there may be multiple correct Answer s
possible
a) Insert 84 in the given below AVL tree. Re-draw the tree. b) Delete 66 from the resultant tree in (a). Re-draw the tree.
Solution:-
or (for deletion using predecessor)
showing the discovery and finishing times of visit for each node.
Solution Ordering 1: G A B D C E F
A B C D E F G
D(t) 1 2 3 9 4 5 13
F(t) 12 11 8 10 7 6 14
Ordering 2: A B C G D E F
A B C D E F G
D(t) 9 10 11 2 3 4 1
F(t) 14 13 12 7 6 5 8
(Optional) Other possible ordering: GABCDEF)
Both Correct 3 marks
Mentioned any wrong one with D and F 2.5 marks
c) What single edge could be removed from the graph such that Dijkstra’s algorithm would happen to compute correct Answer s for all vertices in the remaining graph.
Solution:
c) The edge E to G
a legal way to drive from one point of the city to another. The opposition is not convinced and argues the CM’s claim to be false. Formulate this problem as a graph problem and give a linear time algorithm to check the validity of the claim. Explain the time complexity of your algorithm.
Answer :-This problem could be represented as a directed graph with the vertices as the points in the city and edge from point A to B representing the oneway road in direction from A to B. The task is to check if for all pairs of vertices there exists a path or not. This is clearly a problem on SCC. If the entire road network(the graph) is one SCC then the CM’s claim is true, else false.
Algorithm1. Compute finishing time for every vertex by calling DFS on graph. Store these vertices in decreasing order of their finishing time in a List L.
Time Complexity: O(V+E) as DFS has time complexity of O(V+E) step 1 and step 3 take O(V+E) , step 2 is O(E), step 4 is O(1). Note that this is linear time in terms of size of graph(including the edges and vertices)
keys and values are at the nodes (the letters we write below are just “names” for the nodes for the
(a) What node is the successor of node A? (b) What node is the successor of node B? (c) What node is the successor of node C? (d) What is the height of the tree? (e) What is the maximum number of nodes that could be added to the tree without increasing its height? (f) Is the tree an AVL tree? (g) If we remove only node H, is the result an AVL tree? (h) If we remove only node J, is the result an AVL tree? (i) If we remove only node K, is the result an AVL tree?
Answer :-(a) J (b) E (c) K (d) 4 (e) 18 (f) yes (g) yes (h) no (i) no
priorities 3, 9, 11, and 15. (a) Show every possible binary min-heap that could match this description. For each, draw the appropriate tree and the array representation. (You can show just the priorities, not the corresponding items.) (b) For one of your Answer s to part (a), show what happens with 4 deleteMin operations. Clearly indicate which heap you are starting with and show the heap after each deleteMin. You can just draw the tree (not the array) after each step.
Solution: