Btech Data Structures
Btech Data Structures
UPID: 003443
The Figures in the margin indicates full marks. Candidate are required to give
their answer in their own words as far as Practicable
ii) Which of the following is essential for converting an infix expression to postfix
notation?
Ans. Operator stack is used to convert infix expression to postfix form whereas
operand stack is used to convert postfix to infix notation.
Ans. Sorting Technique is called stable if it keeps elements with equal keys in the
same relative order in the output as they were in the input.
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
vii) What is the search complexity in direct addressing?
Ans. O (1)
Ans. the best case for a linear search is when the target value is equal to the first
element of the list.
x) The values in a BST can be sorted in ascending order by using which of the
traversals method?
xi) Which notation comprises a set of all functions h(n) that are greater than or equal
to cg(n) for all values of n >= n0 ?
xii) A circular queue is implemented using an array of size 10. The array index starts
with 0. front is 6. and rear is 9. The insertion of next element takes place at the
array index.
Ans. 0
Algorithm:
Repeat the following steps while start is less than or equal to end:
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
If arr[mid] is equal to x, return mid.
Ans. Definition:
Stack: A stack is a linear data structure that follows the Last In, First Out (LIFO)
principle, where the last element added is the first one to be removed.
Queue: A queue is a linear data structure that follows the First In, First Out (FIFO)
principle, where the first element added is the first one to be removed.
Operations:
Stack: Supports operations like push (to add an element), pop (to remove the top
element), and peek (to view the top element without removing it).
Queue: Supports operations like enqueue (to add an element), dequeue (to remove
the front element), and peek (to view the front element without removing it).
Order of Elements:
Stack: Elements are processed in a last in, first out (LIFO) order.
Queue: Elements are processed in a first in, first out (FIFO) order.
Implementation:
Real-world Analogy:
Stack: Similar to a stack of plates, where you can only add or remove the top plate.
Queue: Similar to a queue of people waiting in line, where the person who joined
the line first gets served first.
4. Convert the following infix expression into postfix expression using the stack data
structure with detailed explanation:
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
Ans. The postfix expression for the given infix expression A-(B/C+(D/E*F)/G)*H is
ABC/DE*F/G+/H*-.
Step 1: Initialize an empty stack and an empty string to store the postfix expression.
If the character is an operand (A-Z or a-z), add it to the postfix expression string.
If the stack is empty or the top of the stack contains an opening parenthesis, push
the operator onto the stack.
If the precedence of the operator is greater than the precedence of the operator at
the top of the stack, push the operator onto the stack.
If the precedence of the operator is less than or equal to the precedence of the
operator at the top of the stack, pop operators from the stack and add them to the
postfix expression string until either the stack is empty, an opening parenthesis is
encountered, or the precedence condition is satisfied. Then, push the current operator
onto the stack.
If the character is a closing parenthesis, pop operators from the stack and add them
to the postfix expression string until an opening parenthesis is encountered. Discard
the opening parenthesis.
Step 4: After scanning the entire infix expression, pop any remaining operators from
the stack and add them to the postfix expression string.
5. Show how the following polynomial can be represented using a linked list.
7 * 2y * 2 - 4x * 2y - 5xy * 2 - 2
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
polynomial. Each node will contain three fields: coefficient, x exponent, and y
exponent. Here's how the polynomial can be represented using a linked list:
7 * 2y * 2 - 4x * 2y - 5xy * 2 - 2
In B-trees, splitting and merging occur on a node when it becomes full or empty,
respectively.
In B*-trees, splitting and merging are more aggressive and can occur even if a node
is not full or empty, leading to a shallower tree.
Node Occupancy:
B-trees have a lower threshold for node occupancy, which can lead to more frequent
splits and merges.
B*-trees have a higher threshold for node occupancy, reducing the frequency of splits
and merges and maintaining a more balanced tree.
Storage Efficiency:
B*-trees are more storage-efficient than B-trees because they have fewer internal
nodes due to the more aggressive splitting and merging strategy.
Search Performance:
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
B*-trees typically have better search performance than B-trees due to their shallower
structure, resulting from fewer splits and merges.
Efficient Use of Space: Circular queues make more efficient use of the allocated
space since elements can wrap around to the beginning of the queue, utilizing space
that would otherwise be wasted in a simple queue.
Faster Operations: In a circular queue, both insertion and deletion operations are
typically faster compared to a simple queue. This is because there is no need to
shift elements when the queue reaches its capacity or when elements are dequeued
from the front.
Better Performance: Circular queues can offer better performance for scenarios
where the queue is expected to have a large number of operations, as the overhead
of shifting elements is eliminated.
Overall, circular queues are a more efficient and practical choice for many
applications compared to simple queues.
If the character is an operator (* or -), pop the top two elements from the stack,
perform the operation, and push the result back onto the stack.
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
After scanning the entire expression, the final result will be the only element left in
the stack
*:
-:
Pop 9: Stack: []
Perform 9 - 9: Result = 0
(c) Write a program to convert infix expression to its equivalent postfix expression
using stack.
if op == '+' or op == '-':
return 1
if op == '*' or op == '/':
return 2
return 0
def infix_to_postfix(expression):
stack = []
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
postfix = ""
if char.isalnum():
postfix += char
stack.append('(')
postfix += stack.pop()
else:
postfix += stack.pop()
stack.append(char)
while stack:
postfix += stack.pop()
return postfix
# Example usage
infix_expr = "A-(B/C+(D/E*F)/G)*H"
postfix_expr = infix_to_postfix(infix_expr)
Efficient Operations: Insertion and deletion operations at the beginning and end of a
circular linked list can be performed in O(1) time complexity, similar to a regular
linked list.
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
Traversal: Circular linked lists can be easily traversed starting from any node, unlike
linear linked lists where traversal is only possible from the head node.
Space Efficiency: Circular linked lists can save memory compared to doubly linked
lists because they do not require separate pointers for the head and tail nodes.
Complexity: Circular linked lists can be more complex to implement and manage
compared to linear linked lists, especially when dealing with operations that involve
reordering or inserting nodes in specific positions.
Traversal Issues: If not implemented carefully, circular linked lists can lead to infinite
loops during traversal if there are issues with the pointers or node linking.
Round-Robin Scheduling: Circular linked lists are used in operating systems for
round-robin scheduling algorithms, where processes are executed in a circular order.
Music Player Playlist: Circular linked lists can be used to implement a playlist in a
music player, where songs are played in a loop, and the last song is followed by the
first song in the playlist.
9. (a) What are the best worst and average case time complexity of binary search
algorithm?
Ans. The best, worst, and average case time complexities of the binary search
algorithm are as follows:
The best case occurs when the target element is found at the middle of the array in
the first comparison.
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
The worst case occurs when the target element is not present in the array, and the
algorithm has to keep dividing the array in half until it reaches a single element.
The average case also has a time complexity of O(log n), as the algorithm divides
the array in half at each step, leading to a logarithmic time complexity.
Ans. Linear search and binary search are two different algorithms used to search for
an element in a list. Here are the main differences between them:
Algorithm:
Linear Search: Involves iterating over each element in the list sequentially until the
target element is found or the end of the list is reached.
Binary Search: Requires a sorted list and works by repeatedly dividing the list in half
and narrowing down the search range based on whether the middle element is
greater than or less than the target element.
Time Complexity:
Linear Search: O(n) in the worst-case scenario, where n is the number of elements
in the list.
Requirement:
Comparison:
Linear Search: Compares each element in the list with the target element until a
match is found or the end of the list is reached.
Binary Search: Compares the target element with the middle element of the sorted
list to determine which half of the list to search next.
Space Complexity:
Both linear search and binary search have a space complexity of O(1), as they do
not require additional space proportional to the input size to perform the search.
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
(c) What are the preconditions for performing binary search in an array?
Ans. Before performing binary search on an array, the array must meet the following
preconditions:
Sorted Array: The array must be sorted in ascending or descending order. Binary
search relies on the sorted nature of the array to determine which half of the array
to search in.
Unique Elements: While binary search can be adapted for arrays with duplicate
elements, it is typically used on arrays where all elements are unique. This ensures
that the search can accurately determine the position of the target element.
Random Access: Binary search requires the ability to access elements in the array
in constant time (O(1)). This means that the array should support random access,
such as arrays in most programming languages.
Size of Array: Binary search is most effective on large arrays. For very small arrays,
the overhead of the binary search algorithm may outweigh the benefits, and a linear
search may be more efficient.
if (arr[i] == target) {
int main() {
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
int n = sizeof(arr) / sizeof(arr[0]);
if (result != -1) {
} else {
return 0;
Operations of a Stack:
Peek (or Top): Returns the top element of the stack without removing it is Empty:
Checks if the stack is empty.Is Full: Checks if the stack is full (if it has a fixed size).
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
Push 5: Stack = [5]
Size: Returns 2
Stacks are commonly used in programming for tasks such as managing function calls
(call stack), implementing undo mechanisms, and parsing expressions
(b) Write an algorithm to delete an item from a circular array of queue. Also find
the complexity of the algorithm.
Ans. To delete an item from a circular array representing a queue, we can follow
these steps:
If not empty, move the front pointer to the next element (front = (front + 1) %
MAX_SIZE).
If front becomes equal to rear, set front and rear to -1 to indicate an empty queue.
1. if front == -1
3. deletedElement = Queue[front]
4. if front == rear
5. front = rear = -1
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
6. else
8. return deletedElement
Time Complexity: This measures the amount of time an algorithm takes to run as a
function of the size of the input. It is typically expressed using Big O notation (e.g.,
O(n), O(log n), O(n^2)).
Best Case, Worst Case, and Average Case: These describe the time or space
complexity of an algorithm under different scenarios. The best case is the scenario in
which the algorithm performs the fewest operations, the worst case is the scenario in
which it performs the most operations, and the average case is the scenario that is
expected to occur most often.
Scalability: This refers to how well an algorithm performs as the size of the input
grows. An algorithm is said to be scalable if its performance does not significantly
degrade as the input size increases.
Stability: In sorting algorithms, stability refers to whether the relative order of equal
elements is preserved in the sorted output.
Correctness: This measures whether an algorithm produces the correct output for all
possible inputs.
Simplicity and Understandability: These are subjective measures that assess how
easy it is to understand and implement an algorithm.
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB
Ans. Asymptotic notation is used in computer science to describe the behaviour of a
function as its input approaches infinity. It provides a concise way to analyse the
performance of algorithms without getting bogged down in the details of exact
runtimes or memory usage. The three main types of asymptotic notation are Big O,
Big Omega, and Big Theta.
Big O notation describes the upper bound of an algorithm's running time or space
complexity. It represents the worst-case scenario.
Example: If an algorithm has a time complexity of O(n), it means that the algorithm's
running time grows linearly with the size of the input.
Big Omega notation describes the lower bound of an algorithm's running time or
space complexity. It represents the best-case scenario.
Example: If an algorithm has a time complexity of Ω(n), it means that the algorithm's
running time is at least linear with the size of the input.
Big Theta notation describes both the upper and lower bounds of an algorithm's
running time or space complexity. It represents the average-case scenario.
Example: If an algorithm has a time complexity of Θ(n), it means that the algorithm's
running time grows linearly with the size of the input, and this growth is both the
best and worst case
Prepared By Pria Bharti, AP-CSE, Dream Institute of Technology, Thakurpukur, Samali, Kolkata - WB