0% found this document useful (0 votes)
6 views8 pages

Dsa (SRG)

The document provides a comprehensive overview of data structures, including definitions, types, and algorithms for various operations such as sorting, searching, and traversing. It covers both linear and non-linear data structures, their complexities, and specific algorithms like bubble sort, insertion sort, and binary search. Additionally, it discusses conditions like overflow and underflow in data structures, along with concepts like priority queues and hash functions.

Uploaded by

darkside9547
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)
6 views8 pages

Dsa (SRG)

The document provides a comprehensive overview of data structures, including definitions, types, and algorithms for various operations such as sorting, searching, and traversing. It covers both linear and non-linear data structures, their complexities, and specific algorithms like bubble sort, insertion sort, and binary search. Additionally, it discusses conditions like overflow and underflow in data structures, along with concepts like priority queues and hash functions.

Uploaded by

darkside9547
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/ 8

1.

(a) Define data structure.


A data structure is a way of organizing, managing, and storing data in a computer so
that it can be accessed and used
efficiently.

(b) Different types of data structures:


1. Primitive Data Structures: Integer, Float, Character, Boolean, etc.
2. Non-Primitive Data Structures:

Linear Data Structures: Arrays, Linked Lists, Stacks, Queues.


Non-Linear Data Structures: Trees, Graphs.

Hash-based Data Structures: Hash Tables, Hash Maps.


File Structures: Files used for storing data
_______________________________________________________________________
2.
(a) Linear and Non-linear Data Structures:
Linear Data Structures: Data elements are arranged sequentially. Examples: Array,
Linked List, Stack, Queue.
Non-Linear Data Structures: Data elements are hierarchically connected or in a
non-sequential manner. Examples: Tree,
Graph.

(b) Purpose of Time and Space Complexity:


Time Complexity: Measures the time an algorithm takes to complete based on input
size.
Space Complexity: Measures the memory required by an algorithm during execution.
______________________________________________________________________
3.
(a) Difference between STACK and QUEUE:
(b) Algorithm for PUSH and POP in Stack using Linked List:

PUSH Algorithm:
1. Create a new node with the given value.
2. Set the new node's next pointer to point to the top of the stack.
3. Update the top pointer to the new node.

POP Algorithm:
1. If top is NULL, stack is empty (Underflow condition).
2. Store the value of the current top node.
3. Update the top pointer to point to the next node.
4. Delete the current top node.
_______________________________________________________________________
4.Why is Stack called LIFO?

Stack is called LIFO (Last In First Out) because the last element added to the stack is
the first one to be removed, like stacking and removing plates.
5.Convert infix to postfix:
Infix Expression: A + (B * C-(D/E%* F) * G)*H
Postfix Expression: ABC*DEF*/G*-H*+
________________________________________________________________________
6.Algorithm to convert infix to postfix:

1. Create an empty stack and an empty output list.


2. Scan the infix expression from left to right.
3. For each token:

If it’s an operand, add it to the output.


If it’s an operator:
While the top of the stack has an operator of higher or equal precedence, pop it to the

output.
Push the current operator to the stack.
If it’s (, push it to the stack.
If it’s ), pop operators to the output until ( is found; discard (.
4. Pop all remaining operators in the stack to the output.
________________________________________________________________________
7.Bubble Sort Algorithm:

Bubble_Sort(arr):
fori = 0 to n-1:
for j = 0 to n-i-2:
if arr[j] > arr[j+1]:
Swap(arr[j], arr[j+1])

Time Complexity: O(n)


Space Complexity: O(1)
________________________________________________________________________
8.Insertion Sort Algorithm:

Insertion_Sort(arr):
fori = 1ton-1:
key = arr[i]
j=i-1

while j >= 0 and arr[j] > key:


arr[j+1] = arr[j]
j=j-1

arr[j+1] = key

Time Complexity: O(n2)


Space Complexity: O(1)
9.Algorithm to search an ITEM in a sorted linked list:

1. Start at the head of the list.


2. Traverse through each node.
3. Compare the current node’s value with ITEM.
4. If a match is found, return its position.
5. If the end is reached without a match, return "ITEM not found.”
________________________________________________________________________
10.
(a) Overflow and Underflow Conditions in Linked List:
Overflow: Occurs when memory is insufficient to allocate a new node.
Underflow: Occurs when trying to delete from an empty linked list.

(b) Algorithm to insert ITEM at the beginning:


1. Create a new node with ITEM.
2. Set the new node’s next to the current head.
3. Update head to the new node.
________________________________________________________________________
11.(a) Non-recursive algorithm for preorder and inorder traversal:
Preorder:
1. Initialize an empty stack.
2. Push root node.
3. While stack is not empty:

Pop and print the node.


Push the right child, then the left child.
Inorder:
1. Initialize an empty stack and set current = root.
2. While current is not null or stack is not empty:
Push current to stack; move to left child.
If null, pop from stack; print it, move to right child.

(b) Construct binary tree:


Postorder: DEFGHIBCA
(c) Algorithm to search node in BST:
1. Start at root.
2. If node = key, return node.
3. If key < node, move to left subtree.
4. If key > node, move to right subtree.
________________________________________________________________________
12.
(a) Binary Search Tree:
A binary search tree (BST) is a tree where each node has:
Values in the left subtree < root.
Values in the right subtree > root.
(b) Algorithm to insert in BST:
1. Start at root.
2. If tree is empty, set root to new node.
3. Recursively find the correct position:
If key < current node, go to left.
If key > current node, go to right.
4. Insert node.
________________________________________________________________________
13.
(a) Adjacency List vs. Adjacency Matrix:
(b) BFS Traversing Method:
1. Initialize a queue and mark starting node as visited.
2. Enqueue the starting node.
3. While queue is not empty:
Dequeue a node, process it.

Enqueue its unvisited neighbors.

(c) Traverse Graph:


BFS: Starting from 3.
DFS: Starting from 3.
________________________________________________________________________
15. Sorting and Searching Algorithms
(a) Why do we need sorting algorithms?
Sorting algorithms are essential because they organize data in a specific order
(ascending or descending), making it
easier to:

1. Search data efficiently (e.g., binary search).


2. Present data in a readable and structured manner.
3. Optimize other algorithms (like data retrieval or merges).
4. Detect duplicates or anomalies in datasets.

(b) Binary Search Algorithm


Algorithm:

def binary_search(arr, target):


low =0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid # Target found
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1 # Target not found
Complexity Analysis:
Time Complexity:
Best case: (when the target is at the middle).

Worst/Average case: (as the array is divided by 2 at each step).

Space Complexity: (iterative approach).


(c) Deletion of a Node from a Circular Linked List
Algorithm:

1. Start from the head node and traverse to find the node at the specified position.
2. If the node to delete is the head:
Update the last node's next pointer to point to head.next.

Move head to head.next.

3. Otherwise:
Traverse to the previous node of the target node.
Update its next pointer to skip the target node.
4. Free the memory of the target node.
________________________________________________________________________
16. Searching and Quick Sort
(a) Why do we need searching algorithms?
Searching algorithms help locate specific data within large datasets quickly and
efficiently. They are crucial in:

1. Databases and information retrieval systems.


2. Problem-solving in real-time systems (e.g., route optimization).
3. Reducing time complexity compared to linear searches.

(b) Quick Sort Algorithm


Algorithm:

def quick_sort(arr):
if len(arr) <= 1:
return arr # Base case: already sorted
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]

middle = [x for x in arr if x == pivot]


right = [x for x in arr if x > pivot]

return quick_sort(left) + middle + quick_sort(right)


Complexity Analysis:
Time Complexity:
Best/Average case: .
Worst case: (occurs when the pivot is poorly chosen).
Space Complexity: for recursive stack space.

(c) Finding the First Matching Node in a Circular Header Linked List
Algorithm:
1. Initialize a pointer at the header node.
2. Traverse the list while comparing each node's value with ITEM.
3. If found, return the position/index.
4. If the traversal reaches the header again, the item is not in the list.

Circular Queue Conditions


Check for Empty and Full:
Empty Condition: or .
Full Condition: .
________________________________________________________________________
17. Queue and Linked List Operations
(b) Deleting an Element in a Queue Using Linked List
Algorithm:

1. Check if the queue is empty.


2. If not:
Store the front node temporarily.
Move the front pointer to front.next.
Free the memory of the temporary node.
3. If the queue becomes empty, set rear = None.

(c) Insert a Node After a Specified Node in a Singly Linked List


Algorithm:
1. Traverse the list to find the specified node.
2. Create a new node with the given data.
3. Update the next pointer of the new node to the next pointer of the specified node.
4. Update the next pointer of the specified node to the new node.
________________________________________________________________________
18. Queue Overflow and Underflow
(a) Overflow and Underflow Conditions in Array Queue Representation
Overflow: Occurs when trying to enqueue into a queue that is already full.
Underflow: Occurs when trying to dequeue from an empty queue.

(b) Insert an Element in a Queue Using Linked List


Algorithm:

1. Create a new node for the element.


2. If the queue is empty:
Set both front and rear to the new node.
3. Otherwise:
Update the next pointer of the rear node to the new node.

Move the rear pointer to the new node.


Short Notes:
a) Priority Queue and DEQUES
Priority Queue: A type of queue where each element is associated with a priority, and
elements with higher priority are
dequeued before elements with lower priority.

DEQUES (Double-Ended Queues): A data structure where elements can be added or


removed from both ends, i.e., front
and rear.

b) Threaded Binary Tree


A binary tree where null pointers are replaced with pointers to the in-order
predecessor or successor to make in-order
traversal faster without recursion or stack usage.

c) Radix Sort
A non-comparative sorting algorithm that processes each digit of the numbers
sequentially, starting from the least
significant digit to the most significant digit, using a stable sort like counting sort as a
subroutine.

d) Hash Function
A function that converts input data into a fixed-size numerical value (hash code),
typically used in hash tables for fast data
retrieval.

e) Linear Search
A search algorithm that traverses each element in a list sequentially until the desired
element is found or the list ends.

f) Complete Binary Tree


A binary tree in which all levels are fully filled except possibly the last level, which is
filled from left to right.

g) Hash Function and Collision Resolution Techniques


Hash Function: Maps data to a fixed-size value (hash).
Collision Resolution Techniques:
Chaining: Uses linked lists to store multiple elements at the same hash index.

Open Addressing: Probes for the next available slot using methods like linear
probing, quadratic probing, or double
hashing.

h) Binary Search
A search algorithm that repeatedly divides a sorted array into halves to locate a target
element, reducing the search range
by half each time.
i) Merge Sort
A divide-and-conquer sorting algorithm that divides the array into smaller subarrays,
sorts them recursively, and merges
them back together.

j) Sparse Matrix
A matrix with a majority of its elements as zero, typically represented using efficient
data structures like a coordinate list
or compressed sparse row (CSR).

k) Tower of Hanoi Problem


A classic recursive problem involving moving a stack of disks from one peg to
another, following the rules:
1. Only one disk can be moved at a time.
2. Larger disks cannot be placed on smaller disks.
3. Disks must be moved using an auxiliary peg.

You might also like