0% found this document useful (0 votes)
46 views13 pages

Advance Data Structure ASSIGNMENT

The document contains details about an internal assignment for a student named Babar with roll number 0000 pursuing an MCA program in semester 2. It includes the course name as Advanced Data Structures and lists some questions related to data structures, asymptotic notations, linked lists, binary search trees, and evaluating postfix expressions using a stack.

Uploaded by

Mudasir Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views13 pages

Advance Data Structure ASSIGNMENT

The document contains details about an internal assignment for a student named Babar with roll number 0000 pursuing an MCA program in semester 2. It includes the course name as Advanced Data Structures and lists some questions related to data structures, asymptotic notations, linked lists, binary search trees, and evaluating postfix expressions using a stack.

Uploaded by

Mudasir Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Internal Assignment

NAME: Babar

ROLL NUMBER: 0000

PROGRAM: MASTER OF COMPUTER APPLICATION (MCA)

SEMESTER: II

COURSE NAME: ADVANCED DATA STRUCTURE

CODE:

SESSION: MAY 2023


Set-I

Q1.

a) What do you understand by data structures? Discuss briefly on types of data structures.
 Data structures refer to the way data is organized, stored, and manipulated in a computer's memory. They
provide a systematic way of managing and accessing data efficiently. Different data structures are designed
to handle different types of data and operations effectively.
Types of data structure are as follow –
1. Array: An array is a collection of elements of the same type stored in contiguous memory locations. It
allows random access to elements using an index, making it efficient for accessing elements directly.
However, its size is fixed once declared.
2. Linked List: A linked list consists of nodes where each node contains data and a reference (pointer) to
the next node. It allows dynamic memory allocation, making it suitable for efficient insertion and
deletion operations. However, accessing elements in a linked list requires traversal from the beginning.
3. Stack: A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from
the top only. It follows the "push" (add) and "pop" (remove) operations. Stacks are used for tasks like
function call management, expression evaluation, and undo mechanisms.
4. Queue: A queue is a First-In-First-Out (FIFO) data structure where elements are added at the rear
(enqueue) and removed from the front (dequeue). It follows the "enqueue" and "dequeue" operations.
Queues are used in scheduling algorithms, breadth-first search, and handling requests in real-world
scenarios.
5. Tree: A tree is a hierarchical data structure with a set of connected nodes, where one node serves as the
root and each node has zero or more child nodes. Trees are commonly used in file systems, binary
search trees, and hierarchical data representations.
6. Graph: A graph is a collection of nodes (vertices) connected by edges. Graphs can be directed or
undirected, and they are used to model relationships between entities, such as social networks, web page
links, and transportation networks. Graphs have various representations like adjacency list or matrix.
7. Hash Table: A hash table (hash map) is a data structure that uses a hash function to map keys to values,
allowing efficient retrieval, insertion, and deletion operations. It provides constant-time average-case
complexity for these operations. Hash tables are widely used in databases, caches, and language
compilers.
b) Explain asymptotic notations in detail.
 Asymptotic notations are mathematical tools used to describe the behavior of algorithms and functions as
their input sizes approach infinity. They provide a way to analyze the efficiency and performance of
algorithms without getting into the specific details of the hardware or implementation.
Three commonly used asymptotic notations –
1. Big O notation (O): Big O notation represents the upper bound or worst-case scenario of an algorithm's
time complexity. It describes the maximum number of resources (time or space) required by the
algorithm as a function of the input size. In simple terms, it gives an upper limit on how the algorithm's
performance grows with the input.
For example, if an algorithm has a time complexity of O(n), it means that the algorithm's running time
grows linearly with the input size. If the input size doubles, the running time also doubles.
Common Big O notations are –
 O (1): Constant time complexity. The algorithm's performance remains constant regardless of the
input size.
 O (log n): Logarithmic time complexity. The algorithm's performance grows logarithmically
with the input size.
 O(n): Linear time complexity. The algorithm's performance grows linearly with the input size.
 O(n^2): Quadratic time complexity. The algorithm's performance grows quadratically with the
input size.
 O(2^n): Exponential time complexity. The algorithm's performance grows exponentially with the
input size.
2. Omega notation (Ω): Omega notation represents the lower bound or best-case scenario of an
algorithm's time complexity. It describes the minimum number of resources (time or space) required by
the algorithm as a function of the input size. Omega notation provides information about the lower limit
of an algorithm's performance. For example, if an algorithm has a time complexity of Ω(n), it means that the
algorithm's running time is at least linear with the input size. It could be better, but it won't perform worse.
3. Theta notation (Θ): Theta notation represents both the upper and lower bounds of an algorithm's time
complexity. It provides a tight bound on the algorithm's performance, indicating that the algorithm's
running time is both upper and lower bounded by the same function.
For example, if an algorithm has a time complexity of Θ(n), it means that the algorithm's running time
grows linearly with the input size, and there are both upper and lower bounds that grow at the same rate.
Q2.
a) Define linked list and its types. Explain the algorithm for inserting a new note beginning.
 A linked list is a data structure consisting of a sequence of nodes, where each node contains data and a
reference (or link) to the next node in the sequence. It is called a "linked" list because the nodes are linked
together through these references.
Each node in a linked list typically contains two components:
i. Data: This is the value or information stored in the node.
ii. Next: This is a reference (or printer) to the next node in the sequence.

Types of Linked List:

1. Singly Linked List: In a singly linked list, each node contains a data element and a reference
to the next node in the list. The last node points to null, indicating the end of the list.
2. Doubly Linked List: In a doubly linked list, each node has two references, one pointing to
the next node and the other pointing to the previous node. This allows traversal both forward
and backward in the list.
3. Circular Linked List: In a circular linked list, the last node in the list points back to the first
node, creating a circular structure. This allows traversal starting from any node and looping
back to the beginning.
4. Skip List: A skip list is an enhanced version of a linked list that allows for faster searching
by including multiple layers of linked lists with "skip" references. These skip references
enable skipping ahead in the list during search operations, reducing the number of
comparisons required.
To insert a new node at the beginning of a linked list, we can follow the following steps:

Step 1  Create a new Node

Step 2  Set the next pointer of the new node.

Step 3  Update the head pointer.

Here is a sample algorithm –

function insertAtBeginning(list, data):

// Step 1: Create a new node


newNode = createNode(data)

// Step 2: Set the next pointer of the new node

newNode.next = list.head

// Step 3: Update the head pointer

list.head = newNode
b) Evaluate following postfix expressions using
STACK: 5 6 3 ^ 9 1 - ++

Step Input Symbol Operations Stack Calculation
1. 5 Push 5
2. 6 Push 5, 6
3. 3 Push 5, 6, 3
4. ^ Pop (2 elements evaluate) 5 6^3 = 216
5. Push result (216) 5, 216
6. 9 Push 5, 216, 9
7. 1 Push 5, 216, 9, 1
8. - Pop (2 elements evaluate) 5, 216 9–1=8
9. Push result (8) 5, 216, 8
10. + Pop (2 elements evaluate) 5 216 + 8 = 224
11. Push result (224) 5, 224
12. + Pop (2 elements evaluate) Empty 5 + 224 = 229
13. Push result (229) 229
14. No more elements pop Empty 229 (Result)
Q3.
a) Define and explain the concept of binary search tree and discuss the algorithm for insertion in BST.
 A binary search tree (BST) is a type of binary tree that follows a specific ordering property. It is a
hierarchical data structure where each node has at most two child nodes, referred to as the left child and the
right child. In a binary search tree, the values or keys of the nodes are organized in a way that allows for
efficient searching, insertion, and deletion operations.
The ordering property of a binary search tree states that for any node:
1. All values in the left subtree are less than the value of the node.
2. All values in the right subtree are greater than the value of the node.
3. The ordering property holds recursively for all nodes in the tree.
This property allows for efficient searching because it enables a binary search algorithm. When searching
for a specific value in a binary search tree, you compare the target value with the value of the current node.
If the target value is less, you move to the left child; if it is greater, you move to the right child. By
repeatedly comparing and traversing left or right, you can quickly narrow down the search space.
The binary search tree's ordering property also facilitates efficient insertion and deletion operations. When
inserting a new value into the tree, you traverse the tree following the ordering property until you find an
appropriate position to insert the new node. Similarly, when deleting a node, you need to carefully maintain
the ordering property while rearranging the tree.
The algorithm for the inserting a new node into a binary search tree (BST) is as follows –
1. If the tree is empty:
 Create a new node with the given value
 Set it as the root of the tree.
 Exit the algorithm
2. If the tree is not empty:
 Start at the root node.
 Compare the value of the node with the value of the current node.
 If the value of the new node is less than the current node’s value:
 If the current node has a left child, move to the left child and repeat Step 2.
 If the current node does not have a left child, create a new node with the given value and
set it as the left child of the current node.
 Exit the algorithm.
 If the value of the new node is greater than the current node's value:
 If the current node has a right child, move to the right child and repeat Step 2.
 If the current node does not have a right child, create a new node with the given value
and set it as the right child of the current node.
 Exit the algorithm.
b) Explain different types of traversing techniques for Binary Tree
 Traversing a binary tree means visiting each node in the tree exactly once. There are three main techniques
for traversing a binary tree: inorder, preorder, and postorder traversal. Each technique follows a different
order of visiting the nodes, resulting in different output sequences.
1. Inorder Traversal-
In inorder traversal, the left subtree is visited first, followed by the root node, and then the right subtree.
This technique produces a sorted output sequence when applied to binary search trees.
The process for inorder traversal is as follow:
 Recursively traverse the left subtree.
 Visit the current node.
 Recursively traverse the right subtree.
2. Preorder Traversal-
In preorder traversal, the root node is visited first, followed by the left subtree, and then the right
subtree. This technique is commonly used to create a copy of the tree or to retrieve the prefix expression
from an expression tree. The process for preorder traversal is as follow:

 Visit the current node.


 Recursively traverse the left subtree.
 Recursively traverse the right subtree.
3. Postorder traversal-
In postorder traversal, the left subtree is visited first, followed by the right subtree, and then the root
node. This technique is commonly used to delete a binary tree or to retrieve the postfix expression from
an expression tree. The process for postorder traversal is as follow:

 Recursively traverse the left subtree.


 Recursively traverse the right subtree.
 Visit the current node.
Set – II

Q4.

a) What is static memory allocation and dynamic memory allocation?



1. Static memory allocation:
Static memory allocation refers to the allocation of memory for variables and data structures during the
compile-time or program initialization phase. The memory for static variables is allocated before the
program starts executing and remains fixed throughout the program's lifetime.
In static memory allocation, the size of the memory is determined in advance, and the compiler assigns
memory addresses to variables and data structures. This allocation is typically done for global variables,
static variables, and local variables declared with the static keyword. The memory for these variables is
allocated from a region called the "stack" or "data segment" depending on the programming language.
2. Dynamic memory allocation:
Dynamic memory allocation involves allocating memory for variables and data structures during
runtime, as needed. It allows programs to allocate memory at runtime based on dynamic requirements,
such as user input, data size, or program logic.
In dynamic memory allocation, the memory is allocated from a region called the "heap" or "free store."
The program requests memory from the operating system at runtime and receives a pointer to the
allocated memory. This pointer can be used to access and manipulate the dynamically allocated
memory.

b) Explain algorithm for BFS. Demonstrate BFS using suitable example?


 Breadth-First Search (BFS) is a graph traversal algorithm that explores all the vertices of a graph in breadth-
first order, i.e., exploring all the vertices at the same depth before moving on to the vertices at the next
depth. It uses a queue data structure to keep track of the vertices to be visited.
The algorithm for BFS can be summarized as follow:
1. Create an empty queue and a Boolean array to track visited vertices.
2. Choose a starting vertex and enqueue it into the queue.
3. Mark the starting vertex as visited.
4. Repeat the following steps while the queue is not empty:
 Dequeue a vertex from the queue.
 Process the vertex (e.g., print, store, or perform any desired operation).
 Enqueue all the unvisited neighboring vertices of the dequeued vertex.
 Enqueue all the unvisited neighboring vertices of the dequeued vertex.
5. If there are still unvisited vertices, choose one of them as the new starting vertex and repeat from step 2.

Q5.

a) Write an algorithm to implement Selection sort with suitable example.


 The selection sort algorithm sorts an array by repeatedly finding the minimum element from the unsorted
part of the array and placing it at the beginning. The algorithm works as follows:
1. Start with the first element as the current minimum.
2. Iterate through the array, starting from the second element:
 Compare each element with the current minimum.
 If a smaller element is found, update the current minimum.
3. Swap the current minimum element with the first element of the unsorted part of the array.
4. Repeat steps 2 and 3 for the remaining unsorted part of the array until the entire array is
sorted. Here is an example algorithm in pseudo code to implement selection sort:
function selectionSort(arr):
n = length of arr

for i from 0 to n-2:


minIndex = i

for j from i+1 to n-1:


if arr[j] < arr[minIndex]:
minIndex = j

swap arr[i] with arr[minIndex]

return arr
b) What is static hashing? Discuss its disadvantages.
 Static hashing, also known as fixed-size hashing, is a hashing technique where the hash table's size remains
fixed or static throughout the lifetime of the data structure. In static hashing, a fixed number of buckets or
slots are allocated to store data items based on their hash values. Here’s how static hashing typically works:

1. Determine the number of buckets or slots in the hash table based on the expected or maximum number
of data items to be stored.

2. Compute the hash value for each data item using a hash function. The hash function maps the data item
to a specific bucket or slot.
3. Store the data item in the corresponding bucket or slot in the hash table.
4. In case of a collision, a collision technique such as chaining can be used to handle it

Disadvantages of static hashing:

1. Wasted space: Static hashing suffers from the problem of wasted space. If the number of data items
to be stored is much smaller than the number of buckets allocated, the hash table may have a
significant amount of empty or underutilized slots. This leads to inefficient memory usage.
2. Limited scalability: Static hashing is not easily scalable. Once the hash table is created with a fixed
number of buckets, it is challenging to resize it dynamically to accommodate a larger number of data
items efficiently. Resizing typically involves rehashing all the data items, which can be a costly
operation.
3. Uneven distribution: Depending on the quality of the hash function and the nature of the data, static
hashing may result in an uneven distribution of data items across buckets. If the hash function
doesn't distribute data uniformly, some buckets may become significantly overloaded, leading to
degraded performance.
4. Increased collisions: In static hashing, collisions occur when multiple data items map to the same
bucket. As the number of data items increases or the number of buckets remains fixed, the chances
of collisions increase. Collisions can impact the efficiency of retrieval operations, as they require
additional steps to resolve.
5. Poor performance in dynamic scenarios: Static hashing is not suitable for scenarios where the
number of data items fluctuates significantly over time. If the number of data items varies
dynamically, static hashing may result in inefficient memory usage and performance degradation
due to increased collisions and wasted space.

Q6.
a) Explain how Dijkstra’s algorithm is used to find the shortest path of Directed weighted graph.
 Dijkstra's algorithm is a popular graph algorithm used to find the shortest path in a directed weighted
graph from a given source vertex to all other vertices. It works efficiently for graphs with non-
negative edge weights.
Step-by-step explanation of Dijkstra’s algorithm-
1. Initialize:
 Create a distance array to keep track of the shortest distance from the source vertex to each
vertex in the graph. Initialize it with infinity for all vertices except the source vertex, which is
set to 0.
 Create a visited array to track the visited vertices. Initialize all vertices as unvisited.
 Set the source vertex as the current vertex.
2. Repeat the following steps until all vertices have been visited-
 Find the unvisited vertex with the minimum distance from the current vertex. This can be
done by iterating through all unvisited vertices and selecting the one with the smallest
distance value.
 Mark the selected vertex as visited.
 For the selected vertex, update the distance values of its neighboring vertices. Calculate the
new distance by adding the weight of the edge between the current vertex and the
neighboring vertex to the current distance value. If the new distance is smaller than the
previous distance, update the distance value.
 Set the selected vertex as the new current vertex.
3. After all vertices have been visited, the distance array will contain the shortest distances from the
source vertex to each vertex in the graph.
4. To find the shortest path from the source vertex to a specific vertex, backtrack from the
destination vertex using the distance values and the graph’s edge weights. Follow the path with
decreasing distance values until reaching the source vertex.

b) What do you mean by external storage device? Explain its various types.
 An external storage device, also known as an external drive, refers to any device that provides
additional storage capacity to a computer or other electronic device. It is typically connected to the
device through a USB, Thunderbolt, eSATA, or FireWire interface. External storage devices offer a
convenient way to expand storage space, backup data, and transport files between different systems.
Types of external storage devices:
1. External Hard Disk (HDD): External HDDs are similar to the internal hard drives found in
computers but are housed in an external enclosure. They offer large storage capacities, ranging
from a few hundred gigabytes to several terabytes. HDDs use spinning magnetic disks to store
data and are relatively inexpensive. They are suitable for storing large files, such as videos,
games, and backups.

2. Solid-State Driver (SSD): External SSDs utilize solid-state flash memory to store data, offering
faster data transfer speeds compared to HDDs. They are more expensive but provide improved
performance, durability, and energy efficiency. SSDs are ideal for users who require high-speed
data access or need to run applications directly from the external drive.
3. USB Flash Drive: USB flash drives, also known as thumb drives or memory sticks, are small,
portable storage devices that connect to a computer's USB port. They are compact, lightweight,
and do not have any moving parts. USB flash drives are available in various capacities, typically
ranging from a few gigabytes to several terabytes. They are commonly used for transferring files
between devices, carrying data on the go, or creating backups.
4. Network-Attached Storage (NAS): NAS devices are dedicated storage units connected to a
local network. They provide centralized storage that can be accessed by multiple devices
simultaneously. NAS devices often come with multiple hard drive bays and offer advanced
features like RAID configurations, remote access, media streaming, and automated backups.
They are popular for home or office environments where multiple users need to share and access
files.
5. External Optical Drives: Although becoming less common due to the rise of digital media,
external optical drives, such as CD/DVD or Blu-ray drives, can still be useful. They allow users
to read or write data to optical discs, making them suitable for tasks like installing software,
watching movies, or creating backups on optical media.

You might also like