Ds 2016
Ds 2016
Ds 2016
An Abstract Data Type (ADT) is a data type that is defined by its behavior rather than its
implementation. It provides a set of operations on data, abstracting away the details of how
these operations are implemented. Examples include stacks, queues, and lists.
vi) Write down the infix expression (325)/(3*2-3)+5 into its equivalent postfix form.
Postfix expression: 3 2 * 5 * 3 2 * 3 - / 5 +
Singly Linked List: Each node points to the next node, and the last node points to
null.
Circular Linked List: The last node points back to the first node, forming a circular
structure.
xi) Write down the implementing structure of a tree in C.
In C, a tree is typically implemented using a struct with pointers. Example:
struct Node {
int data;
struct Node *left;
struct Node *right;
};
1. In-order traversal
2. Pre-order traversal
3. Post-order traversal
4. Level-order traversal
xiii) Construct a binary tree with the elements 78, 26, 94, 43, 23, 14, 53, 76, 29.
Building a binary tree from a list of elements can vary depending on the rules (e.g., binary
search tree or general binary tree). For a Binary Search Tree:
markdown
Copy code
78
/ \
26 94
/ \ /
23 43 76
/ /
14 53
\
29
For example, consider a graph with 4 vertices (A, B, C, D) and the following edges:
A-B
A-C
B-D
[0110100110000100]\begin{bmatrix} 0 & 1 & 1 & 0 \\ 1 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0 \\ 0
& 1 & 0 & 0 \\ \end{bmatrix}0110100110000100
Here, each row and column represents a vertex, and the presence of 1 denotes an edge
between vertices.
Question 9
Internal Sorting: Sorting is performed in the main memory. It's used when the entire
dataset can fit into the main memory. Examples include Quick Sort, Merge Sort, and
Bubble Sort.
External Sorting: Sorting is performed when the dataset is too large to fit into the
main memory, and it requires access to external storage like a hard disk. Techniques
like External Merge Sort and Multiway Merge Sort are used in these cases.
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
Best Case: O(n)O(n)O(n) – When the array is already sorted, Bubble Sort only needs
one pass to confirm the order.
Average Case: O(n2)O(n^2)O(n2) – In most cases, each element is compared with
others, leading to a quadratic complexity.
Worst Case: O(n2)O(n^2)O(n2) – When the array is sorted in reverse order, Bubble
Sort performs the maximum number of comparisons and swaps.
Question 10
a) Show the sorting steps of the following elements stored in an integer array Sample[]
= {2, 84, 15, 75, 39, 27, 85, 71, 63, 97, 27, 14} using selection sort.
Selection Sort works by repeatedly finding the minimum element from the unsorted part of
the array and moving it to the sorted part.
I'll skip through all the steps here, but each step involves selecting the minimum of the
remaining unsorted elements and placing it in the correct position.
b) When does bubble sort give a better order of complexity than quick sort?
Bubble Sort can perform better than Quick Sort when the array is already or nearly sorted
because it can stop early in the best-case scenario (O(n)). Quick Sort has an average and
worst time complexity of O(nlogn)O(n \log n)O(nlogn) and O(n2)O(n^2)O(n2), respectively,
and does not take advantage of an already-sorted array.
Max-Heap: The root node has the largest value, and every parent node is greater than
or equal to its children.
Min-Heap: The root node has the smallest value, and every parent node is less than or
equal to its children.
(xiii) Construct a Binary tree with the elements 78, 26, 94, 43, 23, 14, 53, 76, 76, 29.
To construct a binary search tree (BST), insert the elements one by one:
Question 2
Data Type: Specifies the kind of value a variable can hold (e.g., int, float, char).
Data Structure: Defines the way data is organized, managed, and stored in memory
(e.g., array, linked list, stack).
Question 3
c
Copy code
#define MAX 100
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack underflow\n");
return -1;
} else {
return stack[top--];
}
}
Question 4
b) How the limitations in linear Queue can be avoided using Circular Queue?
In a linear queue, space can be wasted when elements are dequeued from the front. In a
circular queue, the positions freed by dequeuing can be reused by adjusting pointers
circularly, maximizing space usage.
c) Explain ‘Queue Full’ and ‘Queue Empty’ conditions for a Circular Queue using code
of implementation.
#define MAX 5
int queue[MAX];
int front = -1;
int rear = -1;