0% found this document useful (0 votes)
12 views

DSA2

DSA

Uploaded by

darkcom2024
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)
12 views

DSA2

DSA

Uploaded by

darkcom2024
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/ 11

Q.1 a) Explain any one Linear & Non Linear Data structure.

=>

Linear Data Structure: Stack

A linear data structure is one in which elements are stored in a sequential or linear
manner. The data elements are arranged in such a way that there is a single line of
processing. Stacks are a prime example of a linear data structure.

Stack

A stack is a collection of elements with two primary operations:

 Push: Add an element to the collection.

 Pop: Remove the most recently added element.

Stacks follow the Last In, First Out (LIFO) principle, meaning the last element
added is the first one to be removed.
Operations in a Stack:
1. Push (element): Adds an element to the top of the stack.
2. Pop(): Removes the element from the top of the stack and returns it.

3. Peek/Top(): Returns the element at the top of the stack without removing it.

4. isEmpty(): Checks whether the stack is empty.

5. Size(): Returns the number of elements in the stack.

Non-Linear Data Structure: Tree

A non-linear data structure is one in which elements are arranged in a hierarchical


manner, unlike linear structures where elements are stored sequentially. Trees are
one of the most common non-linear data structures.
Tree
A tree is a hierarchical structure made up of nodes connected by edges. Each tree
has a root node, and every other node is connected to the root (or another node)
through edges.
Components of a Tree:

 Root: The top node of the tree.

 Node: Each element in the tree.

 Child: A node connected to another node.

 Parent: A node that has one or more child nodes.

 Leaf: A node that does not have any children.

 Edge: The connection between two nodes.

 Height: The number of edges from the root to the farthest leaf.
 Depth: The number of edges from the root to a particular node.

Operations in a Tree:
1. Insert(): Adds a node to the tree.

2. Delete(): Removes a node from the tree.

3. Search(): Finds a node in the tree.

4. Traversal: Visits all nodes in a tree in a specific order. Common tree traversal
methods are:
o Pre-order Traversal

o In-order Traversal

o Post-order Traversal

o Level-order Traversal (Breadth-First Search)

Q.3 b) Explain with example insertion of a node.

=> Insertion of a Node in a Linked List

Inserting a node into a linked list involves adding a new node at a specific position in
the list. A linked list is made up of nodes, where each node contains data and a
reference (or link) to the next node in the list. There are three types of node insertion
in a linked list:

1. Insertion at the Beginning


2. Insertion at the End
3. Insertion at a Specific Position
1. Insertion at the Beginning (Head of the List)

Inserting a node at the beginning of the list means that the new node becomes the
head of the list, and the current head becomes the second node.
Steps:

1. Create a new node.

2. Set the new node's next pointer to the current head of the list.

3. Update the head of the list to point to the new node.


2. Insertion at the End (Tail of the List)

Inserting a node at the end means that the new node is added after the last node of
the list. If the list is empty, the new node becomes the head.
Steps:

1. Create a new node.


2. Traverse the list to reach the last node.

3. Set the next pointer of the last node to the new node.

4. The new node’s next pointer will be nullptr, indicating it is the last node.
3. Insertion at a Specific Position

Inserting a node at a specific position means that you insert the new node between
two existing nodes. The position is typically specified by an index.
Steps:

1. Create a new node.

2. Traverse the list to find the node before the desired position.

3. Set the next pointer of the new node to the next pointer of the previous node.
4. Set the next pointer of the previous node to the new node.

Q.4 a) Explain the reverse operation of linked list.

=> Reversing a linked list involves changing the direction of the links between the
nodes so that the last node becomes the first and the first node becomes the last.
This operation is useful in various applications, such as processing data in reverse
order or reversing the sequence of elements in a list.
There are several methods to reverse a linked list, but the most commonly used and
efficient method is to reverse the list iteratively. Below, I'll explain the iterative
approach to reverse a singly linked list, and provide an example in C/C++.

Q.4 a) Write a note on implementation of Circular Linked List.

=> Circular Linked List

A circular linked list is a variation of a singly linked list where the last node's next
pointer points back to the first node, forming a circular chain of nodes. Unlike a singly
linked list, where the last node’s next is nullptr (indicating the end of the list), in a
circular linked list, the next pointer of the last node points to the first node, creating a
loop.

Circular linked lists can be singly circular or doubly circular:


1. Singly Circular Linked List: In this type, each node has a single link
(pointer) to the next node. The last node’s next points to the first node, and
the list can be traversed in one direction only.
2. Doubly Circular Linked List: In this type, each node has two links: one
pointing to the next node and one pointing to the previous node. The last
node’s next points to the first node, and the first node’s previous points to the
last node.
Basic Operations:

1. Insertion at the End:

o To insert at the end, we traverse the list to the last node and change its
next pointer to the new node.
o The new node’s next pointer is then set to the first node, completing
the circle.
2. Insertion at the Beginning:

o Insert a new node at the beginning and set the new node’s next to the
current head.

o Traverse the list to the last node and update its next pointer to the new
node to maintain the circular structure.
3. Traversal:

o Start from the head and keep traversing using the next pointer until you
reach the head again (since it’s a circle).
4. Deletion:
o Deletion requires adjusting the next pointer of the previous node to
remove the target node and maintaining the circular structure.

Q.5 a) Write a note on Applications of Queue as a Data Structure.

=> Applications of Queue as a Data Structure

A queue is a linear data structure that follows the First In First Out (FIFO) principle,
meaning the element that is added first is the one that gets removed first. The two
primary operations for a queue are enqueue (to add an element) and dequeue (to
remove an element). Queues are highly useful in scenarios where the order of
processing or execution matters and tasks must be processed in the same order in
which they arrive.
1. Scheduling Algorithms (CPU Scheduling)

In operating systems, queues are commonly used for scheduling processes. When
multiple processes are waiting for CPU time, they are placed in a queue. The Round-
Robin Scheduling Algorithm is a typical example where processes are assigned a
fixed time slot to execute
 Example: In time-sharing systems, multiple processes share the CPU by
executing for a set amount of time in a circular fashion. A queue helps to
manage this order.
2. Print Queue

In environments with multiple users sending print jobs to a printer, a print queue is
maintained. Print jobs are added to the queue in the order they are received, and the
printer processes them one by one.
 Example: When several users print documents to a shared printer, the print
queue ensures the order of the jobs is maintained and processed in FIFO.
3. Task Scheduling in Real-Time Systems

Queue-based scheduling is critical for real-time systems where tasks must be


executed in a certain order to meet timing constraints. For instance, in embedded
systems, events or tasks are processed in the order they arrive using a queue.
 Example: In a real-time event-driven system, tasks like sensor readings or
communication signals may be processed in the order of their arrival to
ensure timely responses.
4. Breadth-First Search (BFS) Algorithm

In graph theory, Breadth-First Search (BFS) is an algorithm used to explore a


graph level by level. A queue is used to keep track of nodes that need to be visited
next. The BFS algorithm starts from a source node, adds it to the queue, and
processes all adjacent nodes before moving to the next level.
 Example: Finding the shortest path in an unweighted graph. BFS explores
all nodes layer by layer, making it ideal for problems like shortest path
calculations or network broadcasting.

5. Handling Requests in Web Servers

Web servers often handle multiple client requests concurrently. These requests are
placed in a queue, and the server processes them one by one. This ensures that the
server doesn’t get overloaded and that each request is handled in the order it was
received.
 Example: When a web server receives multiple client requests, they are
queued up, and the server processes each request in the order they arrived,
ensuring fair access.

Q.6 a) What is a tree? What are the applications of trees?

=> A tree is a hierarchical data structure that consists of nodes connected by edges.
It is a collection of elements, where each element (node) has a parent-child
relationship with other elements.
 The root is the topmost node in the tree, and it does not have a parent.

 Nodes represent the data in the tree and can have child nodes, except for the
leaf nodes (which are the nodes with no children).
 Edges are the connections between nodes.
 A binary tree is a type of tree where each node has at most two children,
often referred to as the left child and the right child.
Binary Search Trees (BST) for Searching:

 Efficient searching: Binary Search Trees are widely used for searching
operations, such as searching for an element in a collection. Their average
time complexity is O(log n), making them efficient for large datasets.

Expression Parsing (Syntax Trees):

 Mathematical Expressions: Trees are used to represent mathematical


expressions, especially in compilers. The nodes of the tree represent
operands, and the edges represent operators. Expression trees are used in
evaluating expressions in programming languages and compilers.
Database Indexing (B-Trees):
 Efficient Searching and Insertion: B-Trees and B+ Trees are used
extensively in database indexing to allow fast data retrieval, insertion, and
deletion. These trees are optimized for systems that read and write large
blocks of data.
File Systems:

 Hierarchical Storage: File systems on computers are typically organized as


trees where directories are nodes and files are leaf nodes. The hierarchy
allows for efficient management and traversal of files.
Decision Trees in Machine Learning:

 Classification and Regression: Decision trees are used in machine learning


algorithms to classify data. The tree is built by splitting the dataset into
subsets based on feature values, with the leaves representing classification or
regression outcomes.
Prefix and Suffix Trees (Tries):

 Text Searching and Autocompletion: Tries are used in text search and
retrieval applications. A trie is useful in implementing auto-completion in
search engines or for dictionary lookups.

Q.8 a) Write down the Applications of binary tree in details.

=> A binary tree is a tree data structure made up of nodes also known as left and
right nodes-each of which has a maximum of two offspring. The tree starts at the root
node.

Application of Binary Tree


o Binary trees are applied in data compression methods in the form of the
Huffman coding tree.

o Expression Trees, a binary tree application, are used in compilers.

o Another binary tree application that searches maximum or minimum in O(log


N) time complexity is priority queue.

o Display data that is hierarchical.

o Utilized in spreadsheet and Microsoft Excel editing applications.

o Syntax trees are used by several well-known computer compilers, including


GCC and AOCL, to execute arithmetic operations. They are helpful for
indexing segmentation at the database and storing cache in the system.
o For putting priority queues into action.
o Utilized to provide quick memory allocation in computers (binary search tree)
by finding items more quickly.

o Encoding and decoding operations

Q.9 a) Differentiate depth first search and breadth first search technique in
detail.

=>

Parameters BFS DFS

BFS stands for Breadth DFS stands for Depth First


Stands for First Search. Search.

BFS(Breadth First Search)


uses Queue data structure DFS(Depth First Search) uses
Data for finding the shortest Stack data structure.
Structure path.

DFS is also a traversal


BFS is a traversal
approach in which the traverse
approach in which we first
begins at the root node and
walk through all nodes on
proceeds through the nodes as
the same level before
far as possible until we reach
moving on to the next
the node with no unvisited
level.
Definition nearby nodes.

Conceptual BFS builds the tree level by DFS builds the tree sub-tree by
Difference level. sub-tree.

Approach It works on the concept of It works on the concept of LIFO


used FIFO (First In First Out). (Last In First Out).

BFS is more suitable for DFS is more suitable when


searching vertices closer there are solutions away from
Suitable for to the given source. source.
Parameters BFS DFS

BFS is used in various DFS is used in various


applications such as applications such as acyclic
bipartite graphs, shortest graphs and finding strongly
paths, etc. If weight of connected components etc.
every edge is same, then There are many applications
BFS gives shortest pat where both BFS and DFS can
from source to every other be used like Topological
Applications vertex. Sorting, Cycle Detection, etc.

Q. List out the types of sorting available in Data Structures.

=> Bubble Sort:

 Repeatedly steps through the list, compares adjacent elements, and swaps
them if they are in the wrong order. The process repeats until the list is sorted.

 Time Complexity: O(n²)


 Space Complexity: O(1)
Selection Sort:

 Repeatedly selects the smallest (or largest) element from the unsorted part of
the list and swaps it with the first unsorted element.

 Time Complexity: O(n²)

 Space Complexity: O(1)


Insertion Sort:

 Builds the final sorted array one item at a time by inserting each element into
its correct position in the already sorted part of the list.

 Time Complexity: O(n²) (worst case)

 Space Complexity: O(1)


Merge Sort:

 A divide-and-conquer algorithm that splits the list into halves, recursively sorts
each half, and then merges the sorted halves.
 Time Complexity: O(n log n)
 Space Complexity: O(n)
Quick Sort:

 A divide-and-conquer algorithm that selects a pivot element, partitions the


array into two subarrays (elements smaller than the pivot and elements
larger), and recursively sorts the subarrays.

 Time Complexity: O(n log n) (average case), O(n²) (worst case)

 Space Complexity: O(log n)


Heap Sort:

 Converts the list into a binary heap and then repeatedly extracts the maximum
(or minimum) element to build the sorted array.

 Time Complexity: O(n log n)

 Space Complexity: O(1)

Q. What is the purpose of quick sort and advantage?

=> Purpose of Quick Sort:

Quick Sort is a divide-and-conquer algorithm used to sort an array or list of elements.


Its main purpose is to efficiently sort large datasets by dividing the problem into
smaller subproblems (subarrays) and recursively solving them. It works by choosing
a pivot element and partitioning the array around it, such that elements less than the
pivot are on the left, and elements greater than the pivot are on the right. Then, the
process is recursively repeated on the left and right subarrays.

The general steps involved in Quick Sort are:


1. Choose a pivot element from the array.

2. Partition the array into two subarrays:

o One subarray with elements smaller than the pivot.

o One subarray with elements larger than the pivot.

3. Recursively apply Quick Sort to the subarrays.

4. Combine the results (no actual combining is necessary since the array is
sorted in place).

Advantages of Quick Sort:

1. Efficient Time Complexity (Average Case):


2. In-Place Sorting:
3. Cache Efficiency:

4. Divide-and-Conquer:

5. Tail Recursion:

Q. Write a note on Applications of Stack as a Data Structure.

=> Applications of Stacks:

 Function calls: Stacks are used to keep track of the return addresses of
function calls, allowing the program to return to the correct location after a
function has finished executing.

 Recursion: Stacks are used to store the local variables and return addresses
of recursive function calls, allowing the program to keep track of the current
state of the recursion.

 Expression evaluation: Stacks are used to evaluate expressions in postfix


notation (Reverse Polish Notation).

 Syntax parsing: Stacks are used to check the validity of syntax in


programming languages and other formal languages.

 Memory management: Stacks are used to allocate and manage memory in


some operating systems and programming languages.

You might also like