Summer 2020 Solution - Ds
Summer 2020 Solution - Ds
ANS
Data Types Data Structures
Data type is a kind of variable or Data structure is the collection of
form of a variable which is being different kind of data. The entire
used throughout the program. It data can be represented using an
defines that the particular variable object and can be used throughout
will assign the values of the given the entire program
data type only
Implementation through data Implementation through data
z
types is a form of abstract structures is called concrete
implementation
Can hold values and not data so it
aa
implementation
Can hold a different kind and types
Aw
is data less of data within one single project
Values can directly be assigned to The data is assigned to the data
data type variables structure object using some set of
ut
ANS
a) Give examples of Linear & Non – Linear Data Structures
Linear Data Structure : Stack & Queue
Non Linear Data Structure : Tree & Graph
z
aa
Aw
ut
gr
Ja
Example : int x, y, z ;
x = 13;
constant 13 is converted to 2’s complement & then stored in X.
Representation is handled by the complier
X=Y+Z
ANS
z
#define MAX 50 aa
void insert( );
Aw
void delete( ) ;
void display( ) ;
int queue_array[MAX];
ut
int rear = - 1;
gr
int front = - 1;
main( )
Ja
{
int choice;
while(i)
{
printf(“1.Insert element to queue\n”);
printf(“2.Delete element to queue \n”);
printf(“3. Display all elements of queue\n”);
printf(“4. Quit \n”);
printf(“Enter your choice :”);
scanf(“%d”, &choice);
switch(choice)
{
case 1 :
insert( );
z
{ aa
int add_item
if(rear = = MAX – 1)
Aw
if(front = = -1)
gr
z
printf(“\n”); aa
}
}
Aw
ANS
gr
Stack Queue
Ja
ANS
z
last or most recently inserted element
Syntax : stack_name.top( );
aa
Stack is basic data structure where insertion & deletion of data takes
Aw
place at one end called top of stack. It follow LIFO (Last In First Out)
principle where element can be added or removed from top end of
ut
stack
gr
Q2 (C) What is circular queue? How do you check queue full condition?
Ja
ANS
Circular Queue
A more suitable method of representing excessive use of memory is
to average elements Q[1], Q[2] …. Q[n] in circular fashion with Q[I]
following Q[n] this is called Circular Queue
In standard queue data structure re – buffering problem occurs for
each dequeue operation. To solve this problem by joining front and
rear ends of queue to make queue as circular queue
Circular queue is linear data structure. It follows FIFO principle
In circular queue last node is connected back to first node to make
queue
z
To check whether queue is FULL
aa
Check ((rear = = SIZE – 1 && front = 0) ||(rear = = front – 1))
If it is full then display Queue is full. If queue is not full then check if
Aw
(rear = = SIZE – 1 && front! = 0). If it is true then set rear = 0 & insert
element
ut
{
node * temp = head;
int count = 0;
if (temp = = NULL)
count < < “List is Empty”;
do
{
count ++;
temp = temp → next;
}
while(temp ! = head)
count << “The total number of nodes” << count;
}
ANS
z
aa
Aw
ut
z
last → next = new;
new → previous = last;
new → next = NULL;
aa
Aw
}
If you want to add node at nth position, point next pointer of new
node to (n + 1) node & point to previous pointer of new node to (n -
gr
void DeleteAtFirst( )
{
Node * temp = *head;
head = temp → next ;
head → previous = NULL;
z
free(temp);
}
aa
Aw
2. Delete node at end of Doubly Linked List
Point next pointer of second last node & previous pointer of last node
to null
ut
gr
Ja
void DeleteAtLast( )
{
Node * ptr, *ptr1;
ptr = head;
while (ptr → next ! = NULL)
{
ptr1 = ptr;
ptr = ptr → next;
}
ptr1 → next = NULL;
free ptr;
}
z
{
struct node *ptr, *ptr1;
ptr = head;
aa
Aw
for(i = 0; i < pos; i ++)
{
ptr1 = ptr;
ut
Q3(A) What are binary trees? Mention different types of binary trees with
example (3M)
ANS
A binary tree is a tree – type non linear data structure with maximum
of two children for each parent.
Every node in binary tree has a left & right reference along with data
element
The node at top of hierarchy of tree is called Root Node
The nodes that hold other sub – nodes are parent nodes
z
aa
Types of Binary Tree
Aw
There are various types of binary trees & each of these binary tree
types has unique characteristics. Here are each of binary tree types in
detail
ut
gr
two children. It means that all nodes in that binary tree should
either have two child nodes of its parent node or parent node is
itself leaf node or external node
In other words, a full binary tree is unique binary tree where
every node expect external node has two children. When it
holds single child, such binary tree will not be full binary tree.
Here quantity of leaf nodes is equal to number of internal
nodes plus one. Equation : L = l + 1, where L is number of leaf
node and l is number of internal nodes
z
strictly two children & every external or leaf node is at same
aa
level or same depth within tree. A perfect binary tree having
height ‘h’ has 2h – 1 node. Here is structure of perfect binary
Aw
tree
ut
gr
Ja
z
aa
Aw
ut
ANS
Ja
Graph
Graph is data structure that consists of following two components
1. A finite set of vertices also called as Nodes
2. A finite set of ordered pair of form(u, v) called as edge. The pair
is ordered because (u, v) is not same as (v, u) in case of a
graph(di – graph). The pair of form (u, v) indicates that there is
edge from vertex u to vertex v. The edges may contain weight/
value
Graphs are used to represent many – real life applications : Graphs are
used to represent networks. The networks may include paths in a city
or telephone network or circuit network. Graphs are also used in social
networks like LinkedIn, Facebook. For eg : in Facebook, each person is
Representation of Graphs
The following two are most commonly used representations of graph
1. Adjacency Matrix
2. Adjacency List
z
There are other representations also like Incidence Matrix &
aa
Incidence List. The choice of graph representation is situation –
specific. It totally depends on type of operations to be performed and
Aw
ease of use
Adjacency Matrix
ut
Adjacency List
An array of lists is used. The size of array is equal to number of
vertices. Let array be an array[ ]. An entry array [i] represents list of
vertices adjacent to ith vertex. This representation can also be used to
represent weighted graph. The weights of edges can be represented
as lists of pairs. Following is adjacency list representation of above
graph
z
aa
Aw
ut
gr
ANS
Algorithm
1) Create new binary search tree node & assign values to it
2) Insert (node, key)
i) If root = NULL
return the new node to calling function
ii) if root → data < key
OR
Q3(A) What is B – Tree of order m? Draw a B – tree of order 3 (3M)
ANS
B – Tree or Multiway Search Tree is M – way tree which can have
maximum of M children
z
M – way tree contains multiple keys in node
aa
This leads to reduction in overall height of tree
If node of M – way tree holds k keys then it will have k + 1 children
Aw
Following properties
Root can have 1 to M – 1 keys
ut
keys
Keys of nodes are stored in ascending order
ANS
z
aa
Aw
ut
gr
Ja
ANS
z
stored in queue
Initially queue contains starting vertex aa
In every iteration a vertex is removed from queue & its adjacent
Aw
vertices which are not visited as yet are added to queue
Algorithm terminates when queue becomes empty
ut
gr
Ja
Q4(A) Explain Sequential File Organization & list its advantages &
disadvantages (3M)
ANS
It is the most common type of file
A fixed format is used for record
All records are of the same length
Position of each file in record and length of field is fixed
z
aa
Records are physically ordered on the value of one of the fields called
ordering field
Aw
ut
gr
Ja
z
Q4(B) How access of record is performed in Multi – Key File Organization
(4M) aa
ANS
Aw
access through account no, while loan officer needs account records
with the given value of overdue limit. So we need to provide to
access path to record based on different need. Generally these files
are index sequential file in which file is stored sequentially based on
primary key & more than one index table are provided based on
different keys. Basically there are 2 approaches for implementing
multi key organization
1. Inverted File Organization
In this file organization keys inversion index contains all of the
values that the key presently has in records of data file. Each
key – value entry in inversion index points to all of the data
records that have corresponding value. The data file is said to
be inverted on that key. Inverted files are sorted on inversion
index so that binary search can be applied to find out index of
z
aa
Aw
Inverted Index File for secondary key A/C value
ut
gr
ANS
Collision Resolution Technique
When one or more hash values, compete with single hash table slots,
collisions occur. To resolve this, next available empty slot is assigned
to current hash value. The most common methods are open
addressing chaining, probabilistic hashing, perfect hashing &
coalesced hashing technique
Chaining
This technique implements linked list & is most popular collision
resolution techniques. Below is an example of chaining process
z
aa
Aw
ut
gr
Here, since one slot has 3 elements {50, 85, 92}, a linked list is
assigned to include other 2 items {85, 92}. When you use chaining
Ja
Open Addressing
This technique depends on space usage and can be done with Linear
or Quadratic probing techniques. As name says, their technique tries
to find on available slot to store record. It can be done in 3 ways
1) Linear Probing : Here, next probe interval is fixed to 1. It supports
best caching but miserably fails at clustering
2) Quadratic Probing : The probe distance is calculated based on
quadratic equation. This is considerably better option as it balances
clustering & caching
z
This technique is a combo of open addressing & chaining methods. A
aa
chain of items are stored in table when there is collision. The next
Aw
available items to prevent collision
ut
OR
Q4(A) Explain indexed sequential file structure (3M)
gr
Ja
ANS
ANS
Minimum Spanning Tree
A minimum spanning tree is special kind of tree that minimizes
z
lengths or weights of edges of tree. An example is cable company
aa
wanting to lay line to multiple neighbourhoods by minimizing amount
of cable laid, cable company will save money
Aw
z
aa
Continue selecting lowest edges until all nodes in same tree
2) Be careful not to complete cycle(route one node back to itself). If
Aw
your choice completes a cycle, discard your choice move onto next
largest weight
ut
gr
Ja
OR
Q4(C) What is Hashing? What are qualities of good hash function? Explain
any two hash functions in short (7M)
ANS
Hashing
A function that converts a given big phone number to small practical
integer value. The mapped integer value is used as an index in hash
table. In simple terms, hash function maps big number or string to
small integer that can be used as index in hash table
Hash Function
The two heuristic methods are Hashing by Division & Hashing by
Multiplication
1) Hashing by Division (The mod method) :
In this method for creating hash functions table by taking
remainder of key divided by table – size. That is, hash function is
h(key) = key mod table_size
i.e key % table = size
Since, it requires only single decision operation, hashing by
division is quite fast
z
When using division method, we usually avoid certain values of
aa
table size should not be power of number. Suppose r since if
table_size = r ^ p, then h(key) is just p lowest order bits of key.
Aw
Unless we know that all low – order p bit patterns are equally
likely, we are better off designing hash functions to depend on
all bits of key
ut
It has been found that best results with division method are
gr
z
Suppose that word size of machine is W bits & that key fits into
aa
single word
We restrict c to be function of form : S/(2w), where S is integer
Aw
r1 * 2w+r0
gr
ANS
Topological Sort
Topological Sorting for Directed Acyclic Graph (DAG) is linear ordering
of vertices such that for every directed edge u, v vertex u comes
before v in ordering. Topological Sorting for graph is not possible if
graph is not DAG
For example a topological sorting of following graph is “5 42310”.
There can be more than one topological sorting for graph. For
example, another topological sorting of following graph is “4 5 2 3 1
0”.The first vertex in topological sorting is always vertex with in
degree as 0 (a vertex with no incoming edges)
z
aa
Aw
ut
ANS
z
Algorithm is easy to implement &
aa
& Conquer
Algorithm is slightly complex.
Aw
requires less amount of code It takes more amount of code
to implement
N number of comparison are log n number of comparison
ut
Q5(C) Examine algorithm for Insertion Sort & sort following array :
Ja
ANS
z
aa
Aw
Therefore 11, 22, 33, 44, 55, 66, 77, 88 is sorted
OR Q5(A) What do you mean by internal & external sorting (3M)
ut
ANS
Internal Sorting
gr
collection of data is small enough that sorting can take place within
main memory. There is no need for external memory for execution of
sorting program
It is used when size of input is small
Example : Bubble Sort, Insertion Sort, Quick Sort, Heap Sort
External Sorting
External Sorting refers to sorting algorithms that are suitable for large
files of records stored on disk that do not fit entirely in main memory
such as most database files
Also called External Merge Sort Algorithm which is k – way merge
algorithm. It sorts chunks that each fit in RAM, then merges sorted
chunks together. Algorithm first sorts M items at time & puts sorted
list back into internal memory
z
Algorithm : Partition (A [low … high])
aa
/* Problem Description : This algorithm partition subarray using first
Aw
Output : The partitioning of array A is done & pivot occupies its proper
gr
i ← low
j ← high + 1
while(i < = j) do
{
while(A[i] < = pivot) do
i←i+1
while(A[j] > = pivot) do
j←j+1
if(i < = j) then
swap(A[i] , A[j])
}
swap(A[low], A[j])
//when i crosses j swap A[low] and A[j] return j
// rightmost index of list
ANS
z
aa
In above figure, we can observe that root node is 40 & all nodes of
Aw
left subtree are smaller than root node & all nodes of right subtree
are greater than root node
Similarly, we can see left child of node is greater than its left child &
ut
smaller than its right child. So also satisfies property of binary search
gr
left side. 25 is smaller than it. So satisfy but 55 on right side it does
not satisfy property of binary search tree
z
aa
Aw
ut
gr
Ja