You are on page 1of 6

Data Structure Using C++

Assignment – 2

Name – Ashok Pahal

Class – MCA 1st OL

Roll No. – 2723760016

Q1. Differentiate root vs leaf node.

Ans. Root Node:

 The root node is the topmost node in a tree hierarchy.


 It serves as the starting point for traversing or accessing other nodes in the tree.

Leaf Node:

 A leaf node is a node in a tree that has no children, meaning it does not have any nodes
connected below it.

Q2. Explain adjacency in graph.

Ans. In graph theory, the term "adjacency" is used to describe relationships between vertices (nodes) in
a graph. The adjacency of vertices in a graph can be expressed through an adjacency matrix or an
adjacency list.

Q3. Define circular linked list.

Ans. Circular Linked List:

 A circular linked list is a type of linked list in which the last node of the list points back to the first
node, forming a loop or circle.

 In a standard linked list, the last node points to null, but in a circular linked list, the last node's
pointer is connected to the first node.

Q4. What is directed weighted acyclic graph?

 Ans. A directed weighted acyclic graph (DAG) is a graph that is directed (edges have a direction)
and acyclic (contains no cycles).
Data Structure Using C++

 Each edge in a DAG has a weight assigned to it, indicating some measure of distance, cost, time,
or any other relevant metric.

Q5. Differentiate b/w strict and complete binary tree.

Ans. Strict Binary Tree:

 In a strict binary tree, each node can have either 0 or 2 children.

 No node in a strict binary tree has only one child.

Complete Binary Tree:

 In a complete binary tree, all levels are completely filled, except possibly for the last
level, which is filled from left to right.

 This means that all nodes are as left as possible on each level.

Q6. What is the best, worst, average case complexity of bubble & selection sort?

Ans. Bubble Sort:

 Best-case complexity: O(n) - when the list is already sorted.


 Worst-case complexity: O(n^2) - when the list is in reverse order.

Selection Sort:

 Best-case complexity: O(n^2) - same as worst and average case.


 Worst-case complexity: O(n^2) - when the list is in reverse order.

Q7. Write a short note hashing.

Ans. Hashing is a technique used in computer science to efficiently map data of arbitrary size to fixed-
size values, typically for the purpose of quickly retrieving or storing information in data structures like
hash tables. The core idea behind hashing is to use a hash function that transforms the input data into a
fixed-size value, known as the hash code or hash value.

Key concepts related to hashing:

 Hash Function
 Hash Code
 Hash Table
Data Structure Using C++

 Collision
 Crypytographic Hash Functions

Q8. Discuss application of BFS

Ans. Breadth-First Search (BFS) is a graph traversal algorithm that explores a graph level by level. It
starts at the root (or a selected node) and explores all the neighbor nodes at the current depth prior to
moving on to nodes at the next depth level. BFS is versatile and finds applications in various fields:

Shortest Path and Minimum Spanning Tree:

 BFS can be used to find the shortest path between two nodes in an unweighted graph.
Since BFS explores nodes level by level, the first time a node is reached, it is guaranteed
to be the shortest path to that node.

 In the case of a connected, undirected graph, BFS can also be used to find the minimum
spanning tree, which is a tree that spans all the vertices with the minimum possible total
edge weight.

Q9. List the steps of merge sort in short.

Ans. Merge Sort is a divide-and-conquer algorithm for sorting a list. Here are the steps of the Merge
Sort algorithm in a nutshell:

1. Divide:

 Divide the unsorted list into two halves recursively until each sub-list contains only one
element.

2. Conquer:

 Merge the sub-lists in a bottom-up manner. Compare and combine the elements of the
divided lists to create sorted merged lists.

3. Combine:

 Continue merging until a single sorted list is obtained, which represents the sorted
version of the original list.
Data Structure Using C++

Q10. Explain types of Tree traversal algorithms.

Ans. Tree traversal algorithms are used to visit and process all nodes in a tree data structure in a specific
order. There are several types of tree traversal algorithms, each with its own characteristics and use
cases. The three main types are:

1. In-Order Traversal:
 Visit the left subtree, then the root, and finally the right subtree.
 For binary search trees (BST), the in-order traversal visits nodes in ascending order.
 In-order traversal is commonly used for sorting elements in a BST.
2. Pre-Order Traversal:
 Visit the root, then the left subtree, and finally the right subtree.
 Pre-order traversal is often used to create a prefix expression (Polish notation) in expression
trees.
3. Post-Order Traversal:
 Visit the left subtree, then the right subtree, and finally the root.
 Post-order traversal is useful for deleting nodes in a tree, especially in memory
management.

Q11. What is linked list? Explain the advantages of linked list over an array.

Ans. A linked list is a linear data structure in which elements are stored in nodes, and each node points
to the next node in the sequence, forming a chain-like structure. Unlike arrays, linked lists do not have a
fixed size and provide dynamic memory allocation for elements. There are various types of linked lists,
such as singly linked lists, doubly linked lists, and circular linked lists.

Here are some key characteristics and advantages of linked lists over arrays:

1. Dynamic Size:

 Linked lists can dynamically adjust their size during runtime. Elements can be easily
inserted or deleted from any position in the list without the need to resize the entire
data structure.

2. Memory Utilization:

 Linked lists use memory more efficiently than arrays. In arrays, a fixed-size block of
memory needs to be allocated, and if the allocated space is not fully utilized, there may
be wasted memory. Linked lists only use as much memory as needed for the current
elements.

3. Insertion and Deletion:


Data Structure Using C++

 Inserting or deleting elements in a linked list is more efficient than in an array. In a


linked list, adding or removing a node involves adjusting pointers, while in an array,
elements may need to be shifted, resulting in higher time complexity.

4. No Pre-allocation of Memory:

 Unlike arrays, linked lists do not require pre-allocation of memory for a fixed size. This
flexibility is particularly beneficial when the number of elements in the list is unknown
or may vary.

5. Ease of Implementation:

 Implementing certain operations, such as insertion and deletion, can be simpler with
linked lists compared to arrays. There is no need to worry about resizing or shifting
elements.

6. No Wasted Memory:

 In an array, memory may be wasted if a predetermined size is allocated but not fully
utilized. Linked lists allocate memory for each node as needed, minimizing wasted
space.

7. Dynamic Data Structures:

 Linked lists can easily represent dynamic data structures, such as stacks and queues,
where the size of the data structure changes dynamically during program execution.

Q12. Perform insertion sort on given list: 5,1,25,15,4,46,99,37,22,10.write pseudo code.

Ans. InsertionSort(arr):

n = length of arr

for i from 1 to n-1:

key = arr[i]

j=i-1

// Move elements of arr[0..i-1] that are greater than key to one position ahead of their current
position

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

arr[j + 1] = arr[j]
Data Structure Using C++

j=j-1

arr[j + 1] = key

// Example usage:

arr = [5, 1, 25, 15, 4, 46, 99, 37, 22, 10]

InsertionSort(arr)

Explanation:

 The for loop iterates through each element in the array starting from the second element (index
1).

 The current element (key) is compared with the elements before it in the array (arr[0] to arr[i-
1]).

 If an element is greater than the key, it is moved to the right to make space for the key.

 The key is then inserted in its correct position in the sorted part of the array.

After running the Insertion Sort algorithm on the given list, the sorted list will be:

1,4,5,10,15,22,25,37,46,991,4,5,10,15,22,25,37,46,99

You might also like