Dsaa - 2 Marks With Answers
Dsaa - 2 Marks With Answers
PART A ( 2 Marks)
Common Applications:
Circular queues are used in various applications, including:
Operating Systems: For managing processes or tasks in a queue, where the order of
execution needs to be maintained.
Memory Management: To allocate and deallocate memory blocks in a circular manner.
Buffering Data Streams: In scenarios where data needs to be processed in a continuous
stream, such as in network communication or audio/video processing.
Traffic Management: In computer-controlled traffic systems, to switch on traffic lights one
by one repeatedly.
9. How would you use a stack to evaluate the expression ((2 + 3) * 5)?
Infix expression ((2 + 3) * 5)
Its equivalent postfix expression is 23+5*
The inorder traversal of the given binary tree is: 5, 10, 12, 15, 17, 20, 25
5. How would you delete a node from a Red-Black Tree while maintaining its balancing properties?
To delete a node from a Red-Black Tree while maintaining its balancing properties, you perform
standard Binary Search Tree (BST) deletion, followed by a series of recoloring and rotations to
restore the Red-Black properties.
Standard BST Deletion:
Locate the node: Find the node to be deleted using standard BST search.
Handle different cases:
Node with no children: Simply remove the node.
Node with one child: Replace the node with its child.
Node with two children: Replace the node with its in-order successor (or predecessor), then delete
the successor (which will have at most one child).
6. Define AVL tree.
An AVL tree is a self-balancing binary search tree (BST) where, for every node, the difference in
height between its left and right subtrees is at most one, ensuring efficient search, insertion, and
deletion operations with O(log n) time complexity.
7. In the context of Splay Trees, explain the 'Zig-Zig' rotation process during a search operation.
In a Splay Tree, a 'Zig-Zig' rotation (or double rotation) occurs when a node (X) and its parent (Y)
are both left or both right children of their respective parents (Y and Z), and it's used during splaying
to bring X to the root.
Example:
Imagine X, Y, and Z are all left children.
First, rotate around Y and Z (a right rotation). Now Y is a child of X, and X is a child of Z.
Then, rotate around X and Y (another right rotation). Now X is the child of Z.
This double rotation effectively brings X closer to the root.
8. Explain the difference between a full binary tree and a complete binary tree.
In a full binary tree every node has either 0 or 2 child nodes. In a complete binary tree, all levels are
completely filled except possibly the last level. There is no particular sequence for filling in nodes.
All nodes should be on the left.
9. What is the key difference between a Binary Tree and a Splay Tree in terms of node access?
Splay trees are binary search trees which are self-adjusting. Self-adjusting basically means that
whenever a splay tree is accessed for insertion or deletion of a node, then that node pushes all the
remaining nodes to become root.
10. Differentiate Binary Tree and Binary Search Tree
4. Compare the performance of quicksort with other sorting algorithms in terms of time complexity
and efficiency.
Quicksort is generally faster and more efficient than other sorting algorithms, like Bubble Sort and
Merge Sort, but its performance depends on the input data.
Time complexity
Quicksort: On average, Quicksort has a time complexity of O(n log n), but can degrade to O(n^2)
in the worst case.
Merge Sort: Merge Sort always has a time complexity of O(n log n), even in the worst case.
Efficiency
Quicksort: Quicksort is often faster than HeapSort and is preferred for sorting arrays.
Merge Sort: Merge Sort is more reliable in terms of time complexity than Quicksort.
5. Apply the divide and conquer strategy to solve a sorting problem. Demonstrate how it works in
practice.
The divide and conquer strategy for sorting involves splitting an array into smaller subarrays,
sorting the subarrays, and then combining the results.
How it works
Split the array into two halves
Recursively sort each half
Compare the first elements of each half
Place the smaller element into the output array
Repeat until one of the halves is empty
6. Explain the concept of graph traversal techniques and list some applications.
Graph traversal techniques are methods to visit all the nodes (vertices) in a graph systematically,
such as Breadth-First Search (BFS) and Depth-First Search (DFS).
Applications include:
Finding shortest paths
Detecting cycles
Searching networks and social graphs
Solving puzzles and games
Web crawling and network broadcasting
7. What is meant by a spanning tree? Give an example.
A spanning tree of a connected graph is a subgraph that includes all the vertices of the original
graph, is connected, and contains no cycles (i.e., it’s a tree).
Example:
For a graph with vertices A, B, C, D connected in a square with diagonals, a spanning tree could be
edges AB, BC, CD — connecting all vertices without forming any cycle.
8. Discuss the significance of Huffman coding in data compression? Why do we need it?
Huffman coding is a lossless data compression technique that assigns shorter codes to more
frequent symbols and longer codes to less frequent ones, minimizing the overall data size.
Significance:
Reduces storage space and transmission time.
Ensures efficient encoding by exploiting symbol frequency, making it essential for file
compression, multimedia, and communication systems.
9. Use the concept of spanning trees to find a minimum spanning tree in a weighted graph.
To find a Minimum Spanning Tree (MST) in a weighted graph using spanning tree concepts:
1. Select edges with the smallest weights that connect all vertices without forming cycles.
2. Common algorithms:
o Kruskal’s Algorithm: Sort edges by weight and add edges one by one if they don’t
create a cycle.
o Prim’s Algorithm: Start from a vertex and repeatedly add the smallest edge
connecting the tree to a new vertex.
The resulting MST connects all vertices with minimum total edge weight.
10. Compare the advantages of using Huffman coding with the other encoding schemes.
Advantages of Huffman Coding over other encoding schemes:
1. Optimal Prefix Codes: Produces the shortest possible average code length for given symbol
frequencies, minimizing data size.
2. Lossless Compression: Ensures no data loss, unlike some fixed-length or simple encoding
methods, making it highly efficient for text and data compression.
9. Define a Hamiltonian circuit and calculate the number of such circuits in a graph with five vertices.
A Hamiltonian circuit is a closed loop in a graph that visits each vertex exactly once and returns
to the starting vertex.
In a complete graph with nnn vertices, the number of Hamiltonian circuits is:
(n−1)!2\frac{(n-1)!}{2}2(n−1)!
For 5 vertices:
(5−1)!2=4!2=242=12\frac{(5-1)!}{2} = \frac{4!}{2} = \frac{24}{2} = 122(5−1)!=24!=224=12
Answer: A Hamiltonian circuit visits each vertex once and returns to the start; there are 12 such
circuits in a complete graph with 5 vertices.
10. Compare Breadth-First Search (BFS) and Depth-First Search (DFS)
BFS (Breadth-First Search):
Traverses level by level, starting from the root node and exploring all adjacent nodes
before moving to their neighbors.
Uses a queue to keep track of nodes to be visited, ensuring that nodes at the same level are
explored before moving to the next level.
Guarantees finding the shortest path in an unweighted graph.
Suitable for finding shortest paths in graphs where edge weights are uniform.
DFS (Depth-First Search):
Explores a graph by going as deep as possible along each branch before backtracking to
explore other branches.
Uses a stack or recursion to keep track of nodes to be visited.
Does not guarantee finding the shortest path.
More efficient for exploring graphs where the solution path is not necessarily the shortest.