0% found this document useful (0 votes)
11 views42 pages

Take Print Question Bank

The document outlines fundamental concepts in problem solving, algorithms, and data structures. It defines problem solving, algorithms, and compares space and time complexity, while also detailing flowcharts, pseudocode, and the building blocks of algorithms. Additionally, it covers data structures such as linked lists, stacks, and queues, along with their operations and advantages.

Uploaded by

darkface00505
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)
11 views42 pages

Take Print Question Bank

The document outlines fundamental concepts in problem solving, algorithms, and data structures. It defines problem solving, algorithms, and compares space and time complexity, while also detailing flowcharts, pseudocode, and the building blocks of algorithms. Additionally, it covers data structures such as linked lists, stacks, and queues, along with their operations and advantages.

Uploaded by

darkface00505
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/ 42

Sss

DATA STRUCTURES - MODULE 1 – PROBLEM SOLVING


PART A - 2 Marks
I. Define Problem solving.
1. Problem solving is the act of
a. defining a problem;
b. determining the source of the problem;
c. identifying, prioritizing, and
d. selecting alternatives for a solution; and
e. implementing a solution
2. Steps of Problem Solving :
The various steps for problem solving are,
i. Problem Understanding phase
ii. Getting started on a problem
iii. Picking of specific examples
iv. Similarities among problems
v. Moving backwards from the solution
II. Write an algorithm to add two numbers.
Step 1: Start
Step 2: Input two numbers a and b
Step 3: Calculate sum = a + b
Step 4: Print sum
Step 5: Stop
III. Define Algorithm.
1. An algorithm is a sequence of steps written in simple English phrases to describe
the solution to a given problem.
2. It basically breaks the solution of a problem into a series of simple descriptive steps.
3. Example, algorithm to add two numbers,
Step 1: Start
Step 2: Input two numbers a and b
Step 3: Calculate sum = a + b
Step 4: Print sum
Step 5: Stop
IV. Compare Space complexity and Time complexity
1. Space complexity is the measure of the amount of memory space required by an
algorithm for its complete execution, for a given set of inputs.
2. Time complexity is the measure of the running time of an algorithm for a given set
of inputs.
V. List the three Asymptotic notations.
The three most important asymptotic notations are:
1. Big-Oh notation
2. Omega notation
3. Theta notation
VI. Draw any four symbols of Flowchart. [Refer Question No : XI below]
VII. Define Pseudocode
1. It represents the problem solution with the help of generic syntax and normal
English phrases.
2. It makes the development of program code easier by utilizing high-level
programming constructs for representing conditional and looping scenarios.

VIII. List the steps in Problem Solving.


The various steps for problem solving are,
i. Problem Understanding phase
ii. Getting started on a problem
iii. Picking of specific examples
iv. Similarities among problems
v. Moving backwards from the solution

IX. List the building blocks of Algorithm


An algorithm has three building blocks which are as following:
i) Sequence
ii) Decision
iii) Repetition

X. Write an example algorithm for sequence , selection and iteration.


Example for Sequence
Write an algorithm to compute and display the sum of two numbers.
Step 1: Start
Step 2: Input two numbers a and b
Step 3: Calculate sum = a + b
Step 4: Print sum
Step 5: Stop
Example for Selection : Write an algorithm that checks if two numbers are equal.
Step 1: Start
Step 2: Input the first number, x
Step 3: Input the second number, y
Step 4: If x = y then
Print “Both are Equal”
else
Print “ Both are not equal”
Step 5: Stop
Example for Iteration : Write a algorithm that prints the “Hello” 5 times.
Step 1: Start
Step 2: Set i = 0
Step 3: Repeat steps 4-5 while i<=5
Step 4: Print “Hello”
Step 5: Set i = i + 1
Step 6: Stop
PART B – 6 Marks
XI. Demonstrate Flow chart with its symbols
 A flowchart is defined as a pictorial representation of an algorithm.
 Flowchart uses various graphical elements to describe the flow of information and
control.

XII. Discuss Flow chart and its characteristics.


1. A flowchart is defined as a pictorial representation of an algorithm.
2. Flowchart uses various graphical elements to describe the flow of information and
control.
3. The objective of using flowcharts is to ease the understanding of programming logic.
4. This helps in developing the corresponding programming code easier and faster.
5. Characteristics of Flowchart:
 Standard flow of control in a flowchart is either from top to bottom or from left to
right.
 Flowchart instructions are simple and concise.
 None of the arrows in a flowchart intersect each other.
 A flowchart always begins with a Start symbol and ends with a Stop symbol.
6. Example :
Draw a Flowchart to compute and display the sum of two numbers.

XIII. Explain briefly Top down design with an example C Program


1. Top down design suggests taking the general statements about the solution one at
a time, and then breaking them down into a more precise subtask / sub-problem.
2. These sub problems should more accurately describe how the final goal can be
reached.
3. The process of repeatedly breaking a task down into a subtask and then each
subtask into smaller subtask must continue until the sub-problem can be
implemented as the program statement.
4. With each spitting, it is essential to define how sub-problems interact with each
other.
5. In this way, the overall structure of the solution to the problem can be maintained.

6. Example C Program for Top Down Approach –


Find Maximum of 2 Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max;
printf(“Enter Two Numbers : “);
scanf(“%d%d”&a,&b);
max = maximum(a,b);
printf(“The Maximum Number is : %d”,max);
getch();
}
int maximum(int n1, int n2)
{
if (n1 > n2)
{
return(n1);
}
else
{
return(n2);
}
}
XIV. Explain briefly Pseudocode with an example.[Refer Notes]
XV. Explain Asymptotic notation of an Algorithm.[Refer Notes]

PART C - 10 Marks
XVI. Sequence the steps involved in Problem solving.
1. Problem solving is the act of defining a problem; determining the source of the
problem; identifying, prioritizing, and selecting alternatives for a solution; and
implementing a solution
2. Problem solving is a creative process which defines systematization and
mechanization.
3. Steps of Problem Solving :
The various steps for problem solving are,
(i) Problem Understanding phase
(ii) Getting started on a problem
(iii) Picking of specific examples
(iv) Similarities among problems
(v) Moving backwards from the solution
(i) Problem Understanding phase :
1. Problem understanding is the first step towards the solution of the
problem.
2. This phase defines the Problem statement which is a set of precisely
defined tasks.
(ii) Getting started on a problem :
1. There are many ways of solving a problem and there may be many
solutions for a problem.
2. So, it is difficult to recognize which solution would be more productive.
3. The best advice is not to get concerned with the details of the
implementation straightway without analysing.
(iii) Picking of specific examples :
1. Pick a specific problem and work out the mechanism to solve that
particular problem.
2. It is usually much easier to work out the details of a solution to a specific
problem because the relationship between the mechanism and the
problem is more clearly defined.
(iv) Similarities among problems :
1. This phases identifies if any similarities exists between the current
problem and the past problems which have being already solved.
2. But sometimes, it blocks from discovering a better solution to the
problem.
(v) Moving backwards from the solution :
1. This phases checks if the given problem is already having a solution.
2. If a known solution exists, then try to work backwards to the starting
point.
3. Even a guess at the solution to the problem may be enough to give a start
on the problem.

XVII. Explain in detail building blocks of an Algorithm.


An algorithm exhibits three building blocks which are as following:
i) Sequence
ii) Decision
iii) Repetition

i) Sequence:
1. Sequence means that each step of the algorithm is executed in the specified
sequential order, where each step is executed exactly once.
2. Diagrammatic representation of Sequence,
3. Example:
Write an algorithm to compute and display the sum of two numbers.
Step 1: Start
Step 2: Input two numbers a and b
Step 3: Calculate sum = a + b
Step 4: Print sum
Step 5: Stop
(ii) Decision/Selection
1. Selection statements are used to determine the set of steps to be executed, based on
a Condition (Boolean Expression).
2. If the conditional result is true, one set of statements in the algorithm will be executed,
otherwise it will execute the other set of statements in the algorithm
3a. A decision statement can be stated in the following manner.
If condition then
Process 1
Else
Process 2
This is popularly known as the if-else construct.
3b. Here, if the condition is true then process 1 is executed but if the condition is false then
the else part - process 2 is executed.
4. Diagrammatic representation,

5. Example: Write an algorithm that checks if two numbers are equal.


Step 1: Start
Step 2: Input the first number, x
Step 3: Input the second number, y
Step 4: If x = y then
Print “Both are Equal”
else
Print “ Both are not equal”
Step 5: Stop
(iii) Repetition/Iteration
1. Repetition involves executing one or more steps for a number of times.
2. It can be implemented by using the constructs like while, do while and for loops.
3. Diagrammatic representation,

4. Example : Write a algorithm that prints the “Hello” 5 times.


Step 1: Start
Step 2: Set i = 0
Step 3: Repeat steps 4-5 while i<=5
Step 4: Print “Hello”
Step 5: Set i = i + 1
Step 6: Stop
XVIII. Elaborate Algorithm with its advantages and disadvantages.[Refer Notes]
XIX. Explain Flowchart with its Advantages and Disadvantages. [Refer Notes]
XX. Explain in detail Top Down Approach [Refer Question XIII above]
Sss
DATA STRUCTURES - MODULE 2 – LISTS, STACKS & QUEUES
PART A - 2 Marks
I. List the advantages of Data structures.
1. Efficiency
2. Flexibility
3. Reusability
4. Maintainability

II. State the concept of Linked lists.


1. A Linked List is a linear data structure used to store a collection of data elements
dynamically
2. Linked list can be visualized as a chain of nodes, where every node has data and a
pointer to denote the next node.
3. Representation,

III. What are the different types of Linked List?


Linked list can be categorized into three types namely,
1. Singly Linked List - the nodes only point to the address of the next node in the list.
2. Doubly Linked List - the nodes point to the addresses of both previous and next
nodes.
3. Circular Linked List - the last node in the list will point to the first node in the list. It
can either be singly linked or doubly linked.

IV. What are the basic operations of Stacks?


1. push() - Used for inserting an element at the top
2. pop() – used for deleting an element at the top
3. size() - Return the number of elements in the stack.
4. isEmpty() - Return true if the stack is empty, otherwise return false
5. isfull() - Return true if the stack is full, otherwise return false.
6. top() - Return the element at the top of the stack.
V. What are the operations of Queues?
1. enqueue() – Insert an element at the end of the queue.
2. dequeue() – Remove and return the first element of the queue.
3. peek() – Return the element of the queue without removing it.
4. size() – Return the number of elements in the queue.
5. isEmpty() – Return true if the queue is empty, otherwise return false.
6. isFull() – Return true if the queue is full, otherwise return false.

VI. Expand (a) ADT and (b) FIFO


ADT – Abstract Data Type
FIFO – First In First Out
VII. Describe Header Linked list.
1. Header Linked List is a list which contains a special node called Header node at the
beginning of the list.
2. Header node consist of ,
a. data part = which is generally used to hold any global information about the
entire linked list.
b. NEXT part - points to the first node in the list.
3. Representation,

VIII. What are Arrays?


1. Array is a Linear Data structure that contains multiple data elements of the same
data type with a single name.
2. Eg, Representation in C Program–
int marks[5];
where marks is an array of 5 integers

IX. Give the algorithm for Push and Pop of Stacks


Algorithm for Push() :
push(stack[MAX],element)
Step 1: Start
Step 2: If top = MAX-1
Display message ”Stack Full” and
Stop
Step 3: top = top + 1
Step 4: stack[top] = element
Step 5: Stop

Algorithm for Pop() :


pop(stack[MAX],element)
Step 1: Start
Step 2: If top = NULL
Display message ”Stack Empty” and
Stop
Step 3: Return stack[top] and set top = top - 1
Step 4: Stop

X. Give the classification of Data Structures

PART B - 6 Marks

XI. Discuss Queues and its operations.


1. Queues are linear data structure which follows a particular order in which the
operations are performed
2. The order followed by Queues are FIFO(First In First Out).
3. In Queue, items are inserted in one end and deleted in other end.
4. The queue has two ends,
(i) Front - which is used for deletion
(ii) Rear – which is used for Insertion
5. The basic operations of the queue are as follows:
 enqueue() – Insert an element at the end of the queue.
 dequeue() – Remove and return the first element of the queue, if the queue is not
empty.
 peek() – Return the element of the queue without removing it, if the queue is not
empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise return false.
 isFull() – Return true if the queue is full, otherwise return false.

6. Both queue operations (insert and delete) are performed on arrays with a constant
check being made to ensure that the array does not go out of bounds.
(i) enqueue() Operation:
 The Insert operation involves checking whether or not the queue pointer
rear is pointing at the upper bound of the array.
 If it is not, rear is incremented by 1 and the new item is added at the end
of the queue.
(ii) dequeue():
 The Delete operation involves checking whether or not the queue pointer
front is already pointing at NULL (empty queue).
 If it is not, the item that is being currently pointed is removed from the
queue and the front pointer is incremented by 1.

XII. Differentiate Stacks and Queues.

XIII. Differentiate Linear and Non Linear Data structure.


Linear Data Structure :
1. A Data structure where data elements are arranged sequentially or linearly where each
and every element is attached to its previous and next adjacent is called a linear data
structure.
2. In linear data structure, single level is involved.
3. Elements are arranged in one dimension, also known as linear dimension.
4. Various types are :
a. Static : Array
b. Dynamic : Linked lists ,Stack , Queue.
Non-Linear Data Structure:
1. A Data structure where data elements are not arranged sequentially or linearly, but,
data elements are arranged in random order without forming a linear structure is
called a Non-linear data structure.
2. Non-linear data structure does not involve a single level.
3. Elements are arranged in one-to-many, many-to-one and many-to-many dimensions.
4. Various Types are :
a. Tree
b. Graph

XIV. Explain operation of Stacks. [ Refer Notes]


XV. Explain different types of Linked lists. [Refer Question XVIII below]
XVI. Explain briefly the Enqueue and Dequeue operation.[Refer Notes]

PART C - 6 Marks

XVII. Detail the classification of Data structure.


Data Structrures are basically of two types
 Built-in/ Primitive Data Structures
 User-Defined / Non-Primitive Data Structures

1. Linear Data Structure :


i. Data structure where data elements are arranged sequentially or linearly where
each and every element is attached to its previous and next adjacent is called a
linear data structure.
ii. Various types are :
a. Static : Array
b. Dynamic : Linked lists ,Stack , Queue.

2. Non-Linear Data Structure:


i. Data structure where data elements are not arranged sequentially or linearly,
but, data elements are arranged in random order without forming a linear
structure is called a Non-linear data structure.
ii. Various Types are :
a. Tree
b. Graph

3. Linear Types :
a) Array : An Array is a Linear Data structure that contains multiple data elements of the
same data type with a single name.

b) Linked Lists : Linked list is a linear data structure which is a collection of data elements
stored dynamically in such a manner that each element points at the next element in
the list.

c) Stacks : Stack is a linear data structure which follows a particular order in which the
operations are performed wherein the order may be LIFO(Last In First Out) or
FILO(First In Last Out). In stack, all insertion and deletion are permitted at only one end
of the list.
d) Queues : Queues are linear data structure which follows a particular order in which
the operations are performed wherein the order may be FIFO(First In First Out). In
Queue, items are inserted in one end and deleted in other end.

4. Non-Linear Types :
a) Trees :
 A tree is a non-linear data structure with a hierarchy-based structure.
 The tree data structure starts from a single node called a root node and has
subtrees connected to the root node.
 Eg ,
where Parent node is E and Child nodes are H , I , J

b) Graphs :
 A graph is an abstract non-linear data structure that is used to implement the
mathematical concept of graphs.
 It is basically a collection of vertices (also called nodes) and edges that connect
these vertices.
 Eg,

XVIII. Classify the different types of Linked lists (or) Summarize the various types of
Linked Lists.
Linked list can be categorized into three types namely,
1. Singly Linked List - the nodes only point to the address of the next node in the list.
2. Doubly Linked List - the nodes point to the addresses of both previous and next
nodes.
3. Circular Linked List - the last node in the list will point to the first node in the list. It
can either be singly linked or doubly linked.

1. Singly linked list :


 In this type of linked list, each node points at the successive node.
 Thus, the list can only be traversed in the forward direction.

2. Doubly linked list :


 In this type of linked list, a node points at both its preceding as well as succeeding
nodes.
 Thus, the list can be traversed in both forward as well as backward directions
3. Circular list :
 In this type of Linked List, the first and the last node are logically connected with
each other, thus giving the impression of a circular list formation.
 The NEXT part of the last node contains the address of the FIRST node, thus
connecting the rear of the list to its front.
 Circular linked lists can exist in both singly linked list and doubly linked list.

XIX. Illustrate Push and Pop operations of a Stack with suitable Algorithm.

PUSH Operation :
i. Push() inserts an element at one end of the stack called top.
ii. The push operation involves checking whether or not the stack pointer is
pointing at the upper bound of the array.
iii. If it is not, the stack pointer is incremented by 1 and the new item is pushed
(inserted) at the top of the stack.

iv. Algorithm to implement the push operation


push(stack[MAX],element)
Step 1: Start
Step 2: If top = MAX-1
Display message ”Stack Full” and
Stop
Step 3: top = top + 1
Step 4: stack[top] = element
Step 5: Stop

POP OPERATION:
i. Pop operation removes and returns the element at the top of the stack, if it is
not empty.
ii. The pop operation involves checking whether or not the stack pointer is already
pointing at NULL (empty stack).
iii. If it is not, the item that is being currently pointed is popped (removed) from the
stack (array) and the stack pointer is decremented by 1.

iv. Algorithm to implement the pop operation


pop(stack[MAX],element)
Step 1: Start
Step 2: If top = NULL
Display message ”Stack Empty” and
Stop
Step 3: Return stack[top] and set top = top - 1
Step 4: Stop

Diagramatic representation of PUSH and POP

XX. Write a C Program for Push and Pop operation in a Stack.[Refer Practical Programs]
Sss
DATA STRUCTURES - MODULE 3 - TREES
PART A - 2 Marks
I) Describe Trees in Data structures.
1. A tree is a non-linear data structure with a hierarchy-based structure.
2. The tree data structure starts from a single node called a root node and has subtrees
connected to the root node.
3. Eg ,

where Parent node is E and Child nodes are H , I , J

II) Describe Hashing technique.


1. Hashing is one of the searching technique that finds the location of an element in a
data structure without making any comparisons.
2. Hashing is a technique or process of mapping keys, and values into the hash table by
using a hash function.
3. Hashing uses a mathematical function called Hash function to determine the location
of an element.
4. The Hash function generates hash keys and stores elements corresponding to each
Hash key in the Hash table.

III) State the purpose of Double hashing.


1. In this technique, two hash functions are used to calculate empty slot to store value.
2. In case of collision, the second hash function h2(k) is calculated and free slot assigned.
3. If a collision occurs after applying a hash function h(k), then another hash function is
calculated for finding the next slot.
h(k, i) = (h1(k) + ih2(k)) mod m
where ,
m is the size of the hash table,
h1(k) and h2(k) are two hash functions
i is the probe number that varies from 0 to m–1
4. Double hashing requires more computational time because two hash functions need
to be computed.

IV) Identify the Parent node and Child nodes of the tree ,

Parent Node : E
Child Nodes : H , I , J
V) Define General Trees in Data structures.
1. General trees are unordered tree data structures where the root node has minimum 0
or maximum ‘n’ subtrees.
2. The General trees have no constraint placed on their hierarchy.
3. The root node thus acts like the superset of all the other subtrees.
4. Eg,

VI) Identify the applications of Hash Table.


Hash tables are implemented where
 constant time lookup is required
 constant insertion is required
 cryptographic applications
 indexing data is required

VII) List the basic operations of Trees


(a) Search Operation
(b) Insert Operation
(c) Preorder Traversal
(d) Inorder Traversal
(e) Postorder Traversal

VIII) What are the methods of rotating the sub trees of an AVL tree?
An AVL tree may rotate in one of the following four ways:
1. Left Rotation
2. Right Rotation
3. Left-Right Rotation
4. Right-Left Rotation

IX) What are the different Tree Traversal?


1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal

X) What is the basic order in the different Tree Traversal?


1. Preorder Traversal : ROOT - LEFT - RIGHT
2. Inorder Traversal : LEFT - ROOT - RIGHT
3. Postorder Traversal : LEFT – RIGHT - ROOT
PART B – 6 Marks
XI) Differentiate Full Binary tree and Complete Binary tree.
FULL BINARY TREE :
1. A full binary tree is a binary tree type where every node except leaf node has 2 child
nodes.
2. Eg,

3. In the above structure, all the nodes except leaf nodes(F,G,H,I,C) have 2 child
nodes.
COMPLETE BINARY TREE :
1. A complete binary tree is a binary tree type where all the leaf nodes must be on the
same level.
2. Eg,

3. In the above structure, all the leaf nodes(D,E,F,G) are in the same level.

XII) Illustrate the Applications of AVL trees.


1. It is used to index huge records in a database
2. It is used to efficiently search in large database.
3. It is used for all types of in-memory collections, including sets and dictionaries.
4. Used for Database applications, where insertions and deletions are less common
but frequent data lookups are necessary
5. Used for Software that needs optimized search.
6. It is applied in corporate areas and storyline games.

XIII) Explain briefly the concept of Binary search tree.


1. A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −
a. The left sub-tree of a node has a key less than or equal to its parent node's key.
b. The right sub-tree of a node has a key greater than or equal to its parent node's
key.
2. Thus, BST divides all its sub-trees into two segments;
a. the left sub-tree and
b. the right sub-tree and can be defined as −
c. left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
3. Eg, In the diagram, root node key 20 has all less valued keys in the left sub tree and the
higher valued keys on the right sub tree.

4. Basic Operations of a tree:


a) Search − Searches an element in a tree.
b) Insert − Inserts an element in a tree.
c) Pre-order Traversal − Traverses a tree in a pre-order manner.
d) In-order Traversal − Traverses a tree in an in-order manner.
e) Post-order Traversal − Traverses a tree in a post-order manner
5. Advantage : Since the order of keys is based on just the parent node, searching
operation becomes simpler
6. Disadvantage : BSTs require more memory than other data structures, such as arrays
and linked lists, because each node in the tree must store two pointers (to the left and
right children) in addition to the element itself.

XIV) Illustrate the basic operations of Trees.[REFER NOTES]


XV)Classify the various types of Binary trees.[REFER NOTES]
XVI) State the Preorder , Inorder and Postorder for the following tree

Inorder Traversal : 4 2 5 1 6 3 7
Preorder Traversal : 1 2 4 5 3 6 7
Postorder Traversal : 4 5 2 6 7 3 1
PART C – 10 MARKS
XVII) Demonstrate the methods of rotating the sub trees of an AVL tree.
An AVL tree may rotate in one of the following four ways to keep itself balanced:
a. Left Rotation
b. Right Rotation
c. Left-Right Rotation
d. Right-Left Rotation
Left Rotation: When a node is added into the right subtree of the right subtree, if the tree
gets out of balance, we do a single left rotation.

Right Rotation: If a node is added to the left subtree of the left subtree, the AVL tree may get
out of balance, we do a single right rotation.

Left-Right Rotation : A left-right rotation is a combination in which first left rotation takes
place after that right rotation executes.

Right-Left Rotation: A right-left rotation is a combination in which first right rotation takes
place after that left rotation executes.
XVIII) Classify the Different Types of Trees in Data Structures
There are three types of trees −
(i) General Trees
(ii) Binary Trees
(iii) Binary Search Trees

(i) General Trees :


1. General trees are unordered tree data structures
where the root node has minimum 0 or maximum
‘n’ subtrees.
2. The General trees have no constraint placed on
their hierarchy.
3. The root node thus acts like the superset of all the
other subtrees.

(ii) Binary Trees :


1. Binary Trees are restricted form of general trees in which the root node can only
hold up to maximum 2 subtrees: left subtree and right subtree.
2. Based on the number of children, binary trees are divided into three types.
(a) Full Binary Tree:
(b) Complete Binary Tree
(c) Perfect Binary Tree

Types Description Example

A full binary tree is a binary tree


A Full Binary Tree type where every node except
leaf node has 2 child nodes.

1. A complete binary tree is a


binary tree type where all the
leaf nodes must be on the same
Complete Binary level.
B
Tree 2. However, root and internal
nodes in a complete binary tree
can either have 0, 1 or 2 child
nodes.
A perfect binary tree is a binary
tree type where all the leaf
Perfect Binary
C nodes are on the same level and
Tree
every node except leaf nodes
have 2 children.

(iii) Binary Search Trees :


1. A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −
a. The left sub-tree of a node has a key less than or equal to its parent node's key.
b. The right sub-tree of a node has a key greater than or equal to its parent node's
key.
2. Thus, BST divides all its sub-trees into two segments;
a. the left sub-tree and
b. the right sub-tree and can be defined as –
c. left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
3. Eg, In the diagram, root node key 20 has all less valued keys in the left sub tree and the
higher valued keys on the right sub tree.

XIX) Elaborate the concept of Trees and Binary tree traversals. (or)
Demonstrate Pre-Order, Post-Order and In-order tree traversals.
TREE :
1. A tree is a non-linear data structure with a hierarchy-based structure.
2. The tree data structure starts from a single node called a root node and has subtrees
connected to the root node.
BINARTY TREE TRAVERSAL :
1. A binary tree is traversed when one needs to access or display its elements.
2. There are 3 methods for traversing a binary tree:
(i) Pre-order Traversal − Traverses a tree in a pre-order manner.
(ii) In-order Traversal − Traverses a tree in an in-order manner.
(iii) Post-order Traversal − Traverses a tree in a post-order manner.
3. Pre Order Traversal :
PREORDER [ROOT – LEFT – RIGHT]
(i) In this traversal, the root node is visited first, then its left child and later its
right child.
(ii) This is repeated recursively until all the elements of the tree are traversed.
(iii) Algorithm Steps
Step 1: Visit the root.
Step 2: Traverse the left subtree, recursively call preorder(left-subtree)
Step 3: Traverse the right subtree, recursively call preorder(right-subtree).
(iv) Example :

Pre-Order Traversal : 1 2 3

4. In Order Traversal :
INORDER [LEFT – ROOT – RIGHT]
(i) In this traversal, the left child node is visited first, it’s root node is visited next
and then the right child node.
(ii) This is repeated recursively until all the elements of the tree are traversed.
(iii) Algorithm Steps
Step 1: Traverse the left subtree, recursively call inorder(left-subtree)
Step 2: Visit the root.
Step 3: Traverse the right subtree, recursively call inorder(right-subtree)
(iv) Example :

In-Order Traversal : 2 1 3

5. Post Order Traversal :


POSTORDER [LEFT – RIGHT – ROOT]
(i) In this traversal, the left child node is visited first, then its right child and then
its root node.
(ii) This is repeated recursively until all the elements of the tree are traversed.
(iii) Algorithm Steps:
Step 1: Traverse the left subtree, recursively call postorder(left-subtree)
Step 2: Traverse the right subtree, recursively call postorder(right-subtree)
Step 3: Visit the root.
(iv) Example :

Post-Order Traversal : 2 3 1

XX) Give the algorithm for Preorder, Inorder and Postorder Tree traversal [ Refer above
answer]
Sss
DATA STRUCTURES - MODULE 4 - SORTING
PART A - 2 Marks
I) Define Sorting.
1. Sorting refers to the operation of arranging data in some given order, such as
increasing or decreasing with numerical data or alphabetically with character data.
2. The techniques of sorting can be divided into two categories. These are:
 Internal Sorting
 External Sorting

II) Outline the process of Quick sort.


Quick sort is a faster and highly efficient algorithm that follows divide-and-conquer approach,
1. Divide: In Divide, first pick a pivot element and partition into two sub-arrays such
that each element in the left sub-array is less than or equal to the pivot element
and each element in the right sub-array is larger than the pivot element.
2. Conquer: Recursively, sort two subarrays with Quicksort
3. Combine: Combine the already sorted array.
III) List the different Sorting techniques.
i. Insertion Sort
ii. Shell Sort
iii. Heap Sort
iv. Merge Sort
v. Quick Sort
vi. Topological Sort

IV) Define Heap.


1. A heap is a complete binary tree in which all the levels except the last level, i.e., leaf
node, should be completely filled, and all the nodes should be left-justified.
2. There are two types of Heap,
a. Min Heap - the value of the root node must be the smallest among all its child
nodes
b. Max Heap - the value of the root node must be the largest among all its child
nodes
3. Example,
V) Define Merge sort.
1. Merge sort is a sorting technique that is based on divide-and-conquer approach.
2. This sorting technique divides a list into several sub lists of equal sizes and sorts them
individually and then merges the various sub lists to form the sorted list.

VI) What is the efficiency of Merge Sort?


Efficiency :
 Best Case : O(n log n)
 Average Case : O(n log n)
 Worst Case : O(n log n)

VII) What is the efficiency of Insertion Sort?


Efficiency of Insertion Sort:
o Best Case : O(n)
o Average Case : O(n2)
o Worst Case : O(n2)
VIII) What is the efficiency of Quick Sort?
Efficiency :
o Best Case : O(n log n)
o Average Case : O(n log n)
o Worst Case : O(n2)

PART B – 6 Marks
IX) Discuss briefly the types of Sorting (OR) Compare Internal and External sorting (OR)
Discuss in detail External Sorting
1. Sorting is of two types namely,
a. Internal Sorting
b. External Sorting
2. Both Internal and External sorting is used to rearrange the data in a particular order.
3. Internal Sorting:
(i) If all the data that is to be sorted can be accommodates at a time in the main
memory, the internal sorting method is being performed.
(ii) Internal sorting is generally faster because main memory can be accessed faster
(iii) Random access to data is feasible as data is stored in main memory.
(iv) Internal sorting algorithms maintain stability
(v) Internal sorting is limited to the size of the main memory and may not be
efficient for large datasets.
(vi) Internal sorting uses simple sort strategy.
(vii) Examples : Heap sort , Insertion sort, Quick sort, Shell sort

4. External Sorting:
(i) When the data that is to be sorted cannot be accommodated in the memory at
the same time and some has to be kept in auxiliary memory such as hard disk,
floppy disk, magnetic tapes etc, then external sorting methods are performed.
(ii) External sorting is slower due to relatively slower access time of external storage
devices.
(iii) External sorting involves sequential access of data.
(iv) External Algorithms may not maintain stability.
(v) External sorting can handle dataset larger than the available memory.
(vi) External sorting typically uses a hybrid sort-merge strategy.
 In the sorting phase, chunks of data small enough to fit in the main memory
are read, sorted, and written out to a temporary file.
 In the merge phase, the sorted sub-files are combined into a single larger
file.
(vii) Examples: Merge sort, Tape sort , Polyphase sort , External radix

X) Discuss Merge sort with its efficiency.


1. Merge sort is a sorting technique that is based on divide-and-conquer approach.
2. Working :
a. According to the merge sort, first divide the given array into two equal halves.
b. Merge sort keeps dividing the list into equal parts until it cannot be further
divided.
c. Now, combine them in the same manner they were broken.
d. In combining, first compare the element of each array and then combine
them into another array in sorted order.
3. Efficiency :
o Best Case : O(n log n)
o Average Case : O(n log n)
o Worst Case : O(n log n)
4. Example :
XI) Compare Insertion sort and Shell sort.

INSERTION SORT SHELL SORT


In insertion sort, at a time, elements can It allows the movement and swapping of
1
be moved ahead by one position only. far-away elements as well.
To move an element to a far-away Elements can be moved to a far-away
2
position, many movements are required position depending on th intervals set.
shell sort uses a gap to compare items far
Insertion sort compares every single item apart from each other, and then recursively
3
with all the rest elements of the list. closes in the gap until all elements are
sorted.

Time Complexity : Time Complexity :


4 Best Case is O(n), Best Case and Average case is O(n*log n)
Average Case and Worst Case is O(n2) and Worst Case is O(n2)
5 Stable algorithm Not a Stable algorithm
6 Simple Implementation More complex implementationn
7 This is the Base for Shell sort This is a variation of Insertion sort
Insertion sort is efficient when array is Shell sort is effective when array is shuffled
8
almost sorted far apart from original order

XII) Summarise the process of Quick sort. [Refer Notes]


XIII) Demonstrate External Sorting Technique in detail.[Refer Q.No:]
XIV) Write the algorithm for Insertion Sort [Refer Notes]
XV) Write the algorithm for Quick Sort[Refer Notes]
Part C - 10 Marks
XVI) Demonstrate Insertion Sort with example.
1. Insertion Sort is a simple sorting algorithm that works by inserting each element of
the array (or list) into its correct position.
2. Working:
a. This sorting technique starts at the beginning of the array, takes the
second element, and then compares it with the first element.
b. If it is smaller than the first element, then they are swapped.
c. Same process is repeated for all other elements until the array is sorted.
3. Example :
Consider an array A containing n elements which needs to be sorted.
Pass 1 - A[2] is compared with A[1] and inserted either before or after A[1].
This makes A[1], A[2] a sorted sub array.
Pass 2 - A[3] is compared with both A[1] and A[2] and inserted at an
appropriate place. This makes A[1], A[2], A[3] a sorted sub array.
Pass n–1 - A[n] is compared with each element in the sub array A[1], A[2],
A[3], … A[n-1] and inserted at an appropriate position.
This eventually makes the entire array A sorted.

4. Algorithm :
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then
move to the next element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
5. Efficiency of Insertion Sort:
o Best Case : O(n)
o Average Case : O(n2)
o Worst Case : O(n2)
XVII) Elaborate the process of Quick Sort with an example.
1. Quick sort is a faster and highly efficient algorithm that follows divide-and-
conquer approach,
2. Working :
a. Divide: In Divide, first pick a pivot element and partition into two sub-arrays
such that each element in the left sub-array is less than or equal to the
pivot element and each element in the right sub-array is larger than the
pivot element.
b. Conquer: Recursively, sort two subarrays with Quicksort
c. Combine: Combine the already sorted array.
3. Example :
Let the elements of unsorted array be –
24 9 29 14 19 27
 In the given array, we consider the leftmost element as pivot. So, in this case, a[left] =
24, a[right] = 27 and a[pivot] = 24.
 Since, pivot is at left, so algorithm starts from right and move towards left.

 Now, a[pivot] < a[right], so algorithm moves forward one position towards left, i.e.
-

 Now, a[left] = 24, a[right] = 19, and a[pivot] = 24.


 Because, a[pivot] > a[right], so, algorithm will swap a[pivot] with a[right], and pivot
moves to right, as -

 Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm
starts from left and moves to right.
 As a[pivot] > a[left], so algorithm moves one position to right as -

 Now, a[left] = 9, a[right] = 24, and a[pivot] = 24. As a[pivot] > a[left], so algorithm
moves one position to right as -

Similarly,
 Now, in a similar manner, quick sort algorithm is separately applied to the left and right
sub-arrays.
 After sorting gets done, the array will be -

4. Algorithm :
Step 1 − Choose the highest index value as pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot

5. Efficiency :
o Best Case : O(n log n)
o Average Case : O(n log n)
o Worst Case : O(n2)

XVIII) Illustrate working of Shell Sort with an example.[Refer Notes]


XIX) Illustrate the working of Heap Sort with an example.[Refer Notes]
XX) Explain Merge Sort in detail with an example [Refer Notes]
Sss
DATA STRUCTURES - MODULE 5 – GRAPHS

PART A - 2 Marks

I) Describe Graph in Data structures.


1. A graph is an abstract data structure that is used to
implement the mathematical concept of graphs.
2. It is basically a collection of vertices (also called
nodes) and edges that connect these vertices.

II) Express the meaning of Cycle in Graph with an example.


1. Cycle is a path that starts and ends at the same vertex.
2. A simple cycle has no repeated edges or vertices (except the first and last vertices).
3. Eg, In the below diagram,

Cycle : A->B->C->A

III) What are vertices and edges of a Graph?


1. Every Graph has a set of points or nodes called as Vertices
2. Vertices are connected by a set of lines called as Edges
3. Eg In the below diagram,

Vertices : A,B,C,D

Edges : (A,B) (B,C) (C,A) (A,D) (D,C)

IV) State the types of Graphs in Data Structures.


1. Graphs are mainly classified as
a. Directed graph
b. Undirected graph
2. Other types of Graphs are
o Simple Graph
o Multi Graph
o Weighted Graph or Labelled Graph
o Cyclic Graph
o Acyclic Graph
o Connected Graph
o Complete Graph

V) Identify the vertices and edges in the graph,


Vertices – A,B,C,D,E
Edges – (A,B) (A,D) (B,D) (C,B) (D,E) (E,C)

VI) What are Weighted graph?


1. In a weighted graph, the edges of the graph are assigned some weight or length.
2. The weight of an edge denoted by w(e) is a positive value which indicates the cost of
traversing the edge
3. Eg,

VII) What are the methods of Graph traversasl?


There are two standard methods of graph traversal ,
1. Breadth-first search
2. Depth-first search

VIII) List the various methods to find the Shortest path in a Graph.
There are three different algorithms to calculate the shortest path between the
vertices of a graph namely,
 Minimum spanning tree
 Dijkstra’s algorithm
 Warshall’s algorithm

Part B – 6 MARKS

IX) Explain briefly the types of Graphs. (or) Elaborate Directed and Undirected Graphs with
an example.
1. A graph can be
a. Directed graph
b. Undirected graph.
2. Directed Graph :
a. In a directed graph, edges form an ordered pair.
b. The edges have direction.
c. For Eg,
d. The Edges are (A, B) (B,C) (A,D) (D,C) and (C,A)
e. In the above example edge (A,B) , the traversal can be only from A to B and not B
to A as it is directed graph.
3. Undirected Graph :
a. In an undirected graph, edges for an unordered pair
b. The edges do not have any direction associated with them.
c. For Eg,

d. The Edges are (A, B) (B,C) (A,D) (D,C) (C,A) and (B,A) (C,B) (D,A) (C,D) (A,C)
e. In the above example edge (A,B) , the traversal can be from A to B and B to A as it
is undirected graph.

4. Other types of Graphs are


o Simple Graph
o Multi Graph
o Weighted Graph or Labelled Graph
o Cyclic Graph
o Acyclic Graph
o Connected Graph
o Complete Graph

X) Construct an Adjacency Matrix for the graph,

Adjacency Matrix
XI) Compare Trees and Graphs in Data structures.
Trees :
1. Tree is a non−linear data structure, which has only one path between two vertices.
2. Loops are not allowed in a tree structure.
3. Trees have exactly one root node.
4. Trees have three traversal techniques namely, pre−order, in−order, and post−order.
5. Trees follow the hierarchical model.
6. Trees are less complex structure.
7. Tree data structure will always have directed edges.
8. It is used for inserting, deleting or searching any element in tree.
9. Eg,

Graph :
1. Graph is also a non−linear data structure that can have more than one path between
vertices
2. Graphs can have loops
3. Graphs do not have a root node.
4. Graphs have two traversal techniques namely, breadth−first search and depth−first
search.
5. Graphs follow the network model.
6. Graphs are relatively more complex.
7. In graph data structure, all the edges can either be directed edges, undirected edges, or
both.
8. It is mainly used for finding the shortest path in the network.
9. Eg,

XII) Discuss the Applications of Graphs.


1. Social networks :
 Graphs are used to represent social networks such as Facebook, where users are
considered to be the vertices and if they are friends then there is an edge
running between them.
2. Transportation systems:
 Google Maps uses graphs for building transportation systems, where
intersection of two (or more) roads are considered to be a vertex and the road
connecting two vertices is considered to be an edge
3. Web crawling:
 Web pages are considered to be the vertices and there is an edge from a page u
to other page v if there is a link of page v on page u. This is an example of directed
graph.
4. Resource allocation:
 In operating systems, each process and resources are considered to be vertices.
Edges are drawn from resources to the allocated process
5. Bio chemical applications :
 Graphs are used in biochemical applications such as structuring of protein,DNA
etc
6. Computer networks:
 Directed graphs are basically a way to represent the internet as a bunch of
devices connected.
7. Telecommunication:
 Graphs are used to model telecommunication networks, such as telephone and
computer networks, and to analyze traffic and routing.
8. Circuit Design:
 Graphs are used in the design of electronic circuits, such as logic circuits and
circuit diagrams.

XIII. Illustrate how graphs are represented as Adjacency Matrix with an example.
1. An adjacency matrix is used to represent which nodes are adjacent to one another
2. Two nodes are said to be adjacent if there is an edge connecting them.
3. In an adjacency matrix, the rows and columns are labelled by graph vertices.
4. An entry aij in the adjacency matrix will contain 1, if vertices vi and vj are adjacent to
each other.
5. However, if the nodes are not adjacent, aij will be set to zero.
6. Eg,

7. Since an adjacency matrix contains only 0s and 1s, it is called a bit matrix or a Boolean
matrix.
8. The entries in the matrix depend on the ordering of the nodes in G. Therefore, a change
in the order of nodes will result in a different adjacency matrix.
9. General conclusions:
 For a simple graph (that has no loops), the adjacency matrix has 0s on the diagonal.
 The memory use of an adjacency matrix is O(n2), where n is the number of nodes
 Number of 1s (or non-zero entries) in an adjacency matrix is equal to the number of
edges in the graph.
 The adjacency matrix for a weighted graph contains the weights of the edges
connecting the nodes.

PART C – 10 Marks

XIV. Elucidate Breadth-first search with an example. (Or) Elucidate the methods of Graph
Traversals explaining any one method with an example
i. There are two standard methods of graph traversal namely ,
 Breadth-first search
 Depth-first search.
ii. During the execution of the algorithm, every node in the graph will have a variable
STATUS set to 1 or 2 or 3, depending on its current state.
 1 – Ready state(Initial State)
 2 – Waiting State (Node is placed in the queue or stack and waiting to be
processed)
 3 – Processed state (Node has been completely processed)
iii. Breadth-first search (BFS) :
1. Breadth-first search is a graph search algorithm that begins at the root node
and explores all the neighbouring nodes.
2. Then for each of those nearest nodes, the algorithm explores their unexplored
neighbour nodes, and so on, until it finds the goal.
3. This is accomplished by using QUEUES that will hold the nodes that are waiting
for further processing ,
 QUEUE1 holds all the nodes that are to be processed,
 QUEUE2 holds all the nodes that are processed and deleted from
QUEUE1.
4. A variable STATUS to represent the current state of the node.
5. Example :
Adjacency List
A : B,D
B: C
C: A
D: C

Step 1 : Add A to Queue1 and NULL to Queue2


QUEUE 1 = {A}
QUEUE 2 = {NULL}

Step 2 : Delete the node A from the Queue1 and add it to Queue2 . Insert all
neighbors to node A to Queue2
QUEUE 1 = {B,D}
QUEUE 2 = {A}
Step 3 : Delete the node B from the Queue1 and add it to Queue2 . Insert all
neighbors to node B to Queue2
QUEUE 1 = {D,C}
QUEUE 2 = {A,B}

Step 4 : Delete the node D from the Queue1 and add it to Queue2 . Insert all
neighbors to node D to Queue2. The only neighbor of D is C and since C is
already inserted , it will not be inserted again.
QUEUE 1 = {C}
QUEUE 2 = {A,B,D}
Step 5 : Delete the node C from the Queue1 and add it to Queue2 . Insert all
neighbors to node C to Queue2. The only neighbor of C is A and since A is
already inserted , it will not be inserted again.
QUEUE 1 = {}
QUEUE 2 = {A,B,D,C}

Step 6 : All nodes are visited in the order {A,B,D,C}

6. ALGORITHM :
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state
(whose STATUS = 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT

7. Time Complexity : O(V+E) where


V - is the number of Vertices and
E - is the number of Edges

XV. Elucidate Depth-first search with an example. . (Or) Elucidate the methods of Graph
Traversals explaining any one method with an example
i. There are two standard methods of graph traversal namely ,
 Breadth-first search
 Depth-first search.
ii. During the execution of the algorithm, every node in the graph will have a variable
STATUS set to 1 or 2 or 3, depending on its current state.
 1 – Ready state(Initial State)
 2 – Waiting State (Node is placed in the queue or stack and waiting to be
processed)
 3 – Processed state (Node has been completely processed)
iii. Depth-first search :
1. Depth-first search is a graph search algorithm that begins at the root node and
explores all the neighbouring nodes.
2. a.The depth-first search algorithm progresses by expanding the root node and
then going deeper and deeper until the goal node is found, or until a node that
has no children is encountered.
b. When a dead-end is reached, the algorithm backtracks, returning to the most
recent node that has not been completely explored.
3. This is accomplished by using STACKS that will hold the nodes that are waiting
for further processing
4. A variable STATUS to represent the current state of the node.
5. Example :
Adjacency List
A : B,D
B: C
C: A
D: C

Step 1 : First push A to Stack


STACK = {A}

Step 2 : Pop the node A from the Stack and Print it. Now, push all neighbors to
node A to Stack
STACK = {B,D}
PRINT A

Step 3 : Pop the node D from the Stack and print it. Push all neighbors to node
D to Stack.
STACK = {B,C}
PRINT A , D

Step 4 : Pop the node C from the Stack and print it. Push all neighbors to node C
to Stack. The only neighbor of C is A and since A is already inserted , it will not
be inserted again.
STACK = {B}
PRINT A,D,C

Step 5 : Pop the node B from the Stack and print it. Insert all neighbors to node
B to Stack. The only neighbor of B is C and since C is already inserted , it will not
be inserted again.
STACK= {}
PRINT A,D,C,B

Step 6 : All nodes are visited in the order {A,D,C,B}


6. Algorithm :
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its
STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state
(whose STATUS = 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
7. Time Complexity : O(V+E) where
V is the number of Vertices and
E is the number of Edges

XVI) Elucidate the methods of Graph Traversals with an example.


There are two standard methods of graph traversal ,
1. Breadth-first search
2. Depth-first search
[Refer Previous Question No. XV and XIV and explain briefly Breadth-first and Depth-first
search]

XVII) Elaborate on Minimum Spanning Trees. [Refer Notes]

XVIII) Discover the various methods to find the Shortest path in a Graph. Explain in detail
any one of the methods.
There are three different algorithms to calculate the shortest path between the vertices
of a graph. These algorithms include:
 Minimum spanning tree
 Dijkstra’s algorithm
 Warshall’s algorithm
[Refer Notes]

XIX) Detail Adjacency Matrix and Adjacency List representations of a Graph with an
example.
[Refer Notes]

XX) Discuss the algorithm for Breadth-first and Depth-first algorithm [Refer Point 6 of
Previous Questions No. XV and XIV and write the algorithm of Breadth-first and Depth-first
search]

You might also like