DS Akash

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

STEP TOWARDS SUCCESS

AKASH'S
Guru Gobind Singh Indra Prastha University Series

SOLVED PAPERS
TPREVIOUS YEARS SOLVED QUESTION PAPERS]

|B.Tech)
THIRD SEMESTER
Data Structure
(CIC-209)

Rs.81.00/

AKASH BOOKS
NEW DELHI
NEW TOPICS ADDED FROM ACADEMIC SESSION 2022-23
THIRD SEMESTER (B.TECH]
DATASTRUCTURE (CIC-209)
Q.1. What is the Time - Space Trade Off in Algorithm & also explain its
type.
Ans. Time-Space Trade-Of in Algorithms
A trade off is a situation where one thing increases and another thing decreases. It
is a way to solve a problem in:
• Either in less time and by using more space, or

In very little space by spending a long amount of timne.
The best Algorithm is that which helps to solve a problem that requires less space
in memory and also takes less time to generate the output. But in general, it is not
always possible to achieve both of these conditions at the same time. The most common
condition is an algorithm using a lookup table. This means that the answers to some
questions for every possible value can be written down. One way of solving this problem
is to write down the entire lookup table, which will let you find answers very quickly but
will use a lot of space. Another way is to calculate the answers without writing down
more
anything, which uses very little space, but might takea long time. Therefore, the
time-efficient algorithms you have, that would be less space-eficient.
Types of Space-Time Trade-off
(a) Comnpressed or Uncompressed data: A space-time trade-off can be applied
to the problem of data storage. If data stored is uncompressed, it takes more space but
less time. But if the data is stored compressed, it takes less space but more time to run
the decompression algorithm. There are many instances where it is possible to directly
work with compressed data. In that case of compressed bitmap indices, where it is faster
to work with compression than without compression.
(b) Re-Rendering or Stored images: In this case, storing only the source and
rendering it as an image would take more space but less time i.e., storing an image in
the cache is faster than re-rendering but requires more space in memory.
(c) Smaller code or Loop Unrolling: Smaller code occupies less space in memory
but it requires high computation time that is required for jumping back to the beginning
of the loop at the end of each iteration. Loop unrolling can optimize execution speed at
the cost of increased binary size. It occupies more space in memory but requires less
computation time.
(d) Lookup tables or Recalculation: In a lookup table, an implementation can
include the entire table which reduces computing time but increases the amount of
memory needed. It can recalculate i.e., compute table entries as needed, increasing
computing time but reducing memory requirements.
Q.2. What is algorithm and why analysis of it is important?
Ans. In the analysis of the algorithm, it generally focused on CPU (time) usage,
Memory usage, Disk usage, and Network usage. All are important, but the most concern
is about the CPU time. Be careful to differentiate between:

Performance: How much time/memoryldisk/etc. is used when a program is run.
2021-3
I.P. University-(B.TechJ-Akash Books
Structure
Semester, Data we write. Asymptotic analysis refers to computing the
running time of any operation in
one
Third as the code mathematical units of computation. For example, the running time of as operation
As well program or algorithm scale, it is computed gn). This
2-2021 compiler, etc. as and may be for another operation
machine. a
larger is computed (n)
time will increase línearly with the increase n
in
code gets
requirements of
on the operation running
resource solved by the first
s
This depends means the
of the second operation will increase
exponentially when
Complexity: How do theof the
problem being
vice-versa. n and the running the same if n is
size operations will be nearly
happens as the performance but
not increases. Similarly, the running time of both
i.e. what
Complexity affects significantly small.
Note: complexity theory, which three types -
computational
Algorithm Analysis an important part of resources of an algorithm to solve
a Usually, the time required by an algorithmn falls under
is required determination of the • Best Case - Minimum time required for
program execution.
analysis the
Algorithm estimation for the algorithms is program execution.
provides theoretical
Analysis of Average time required for
computational resources required to execute
prablem. it. •
Average Case-
Worst Case - Maximum time required for
speciic space program execution.
amount oftime and important?
ofAlgorithms is implementing it on a specific
Why Analysis algorithm without Asymptotic Notations
of an to calculate the running
To predict the
behavior
of Following are the commonly used asymptotic notations
measures for the efficiency time complexity of an algorithm.
efficiency every time certoi
Computer. simple a
much more convenient to have test the
and •0 Notation
It is implement the algorithm
algorithm than to computer systemn changes. manv • QNotation
parameter in the underlying There are too
behavior of an algorithm.
impossible to predict the exact •0 Notation
• It is
infuencing factors.
:
O(n) is the formal way to express the upper
Big Oh Notation, O The notationmeasures
only an
approximation; it is not perfect. the worst case time complexity or the
• The analysis is thus we can compare them to bound of an algorithm's running time. It
analyzing different algorithms, longest amount of time an algorithm can possibly take to comnplete.
• More importantly, by purpose.
one for our
determine the best analysis?
Algorithm
Q.3. Explain the different types of
Algorithmn Analysis are as follows:
Ans. Types of less time or minimum
(a) Best case: Define the
input for which algorithm takes Example: In the linear
lower bound of an algorithm. case
case calculate the
time. In the best
present at the first location of large data then the best
search when search data is
OCcurS. or maximum
(b) Worst Case: Define the input for
which algorithm takes a long time
upper bound of an algorithm. Example: In the linear
time. In the worst calculate the
worst case occurs.
search when search data is not present at all then the
(c) Average case: In the
average case take all random inputs and calculate the
computation time for all inputs.
And then we divide it by the total number of inputs. k
Average case = all random case time /total no of case For example, for a function f(n)
Q4. What is the Asymptotic Analysis of Algorithms? Explain the Diferent O(fn)) = {gn): there exists c > 0 and n, such that fn) s egn) for all n > n. }
types ofAsymptotic Notations? Omega Notation, 2 : The notation n) is the formal way to express the lower
bound of an algorithm's running time. It measures the best case time complexity or
-
Ans. AsymptoticAnalysis Asymptotic analysis of an algorithm refers to defining the
the mathematical boundation/traming of its rua-time performance. Using asymptotic
best amount of time an algorithm can possibly talke te onplete.
analysis, we can very well conclude the best case, average case, and worst case scenario
of an algorithm.
Asymptotic analysis is input bound i.e., if there's no
input to the algorithm, it
is concluded to work in a constant time. Other than
the input" all other factors are
considered constant.
Structure
Semester, Data I.P. University-(B.Tech]-Akash Books
2021-5
Third
4-2021 Q.6. What is the Sparse Matrix? Explain the Array & Linked List
representation of Sparse Matrix.
Ans. A matrix is a two-dimensional data object made of m rows and n columns,
therefore having total m Xn values. If most of the elements of the matrix
have 0 value,
then it is called a sparse matrix.
Why to use Sparse Matrix instead of simple matrix?
• Storage: There are lesser non-zero
elements than zeros and thus lesserT memory
can be used to store only those elements,
• Computing time: Computing time can
be saved by logically designing a data
structure traversing only non-zero elements.
Example:
00304
k
00570
for all n >n.
)

For example. for a function fn)


n, such that g(n) S c.f(n)
there exists c> and 00000
0
Iga): way to express both the lrwer
2fn)) 2 notation (n) is the formal as follows -
Theta Notation, 0: The analgorithm's running time. It is represented 02600
the upper bound of
Representing a sparse matrix by a 2D array leads to wastage
bound and
as zeroes in the matrix are of no use most of lots of memory
in of the cases. So, of storing zeroes
with non-zero elements, we only store non-zero elements. T'hisinstead
means storing non-zero
elements with triples- (Row, Columnn,
value).
Sparse Matrix Representations can be done in many ways
following are two common
representations:
1. Array representation 2. Linked list representation
Method 1: Using Arrays: 2D array is used to represent a sparse
there are three rows named as matrix in which
•Row: Index of row, where non-zero element located
is
•Column: Index of column, where non-zero element
is located
•Value: Value of the non zero element located at
index -(row, column)

{gn) if and only if gn) = 0(fn) and g(n) fn)) for all n >n,.
(f(n) =
} 00 3 0 4
Row 0 1 3
Common Asymptotic Notations 0
05 70
Following is a list of some common asymptotic notations
Column 2 4 2
constant O(1) 0 0
0 0 0
logarithmic O(log n) Value 4 5 7 6
linear 0 2 6 0 0
O(n)
n log n
On log n) Method 2: Using Linked Lists : In linked
quadratic our fields are defined as: list, each node has four fields. These
O(n)
cubic
O(n3) Row: Index of row, where non-zero
polynomial element is located
no) • Column: Index of
column, where non-zero element
exponential is located
90n) •Value:
Value of the non zero element located
at index - (row,column)
• Next node: Address
of the next node
Structure
Semester, Data
Third
I.P. University-(B.Tech]-Akash Books 2021-7
6-2021 START

o 4
Ans. Polynomial can be represented in the various ways. These are:
o03 • By the use of arrays • By the use of Linked List
006 70 Representation of Polynomials Using Arrays
0000C There may arise some situation where you need to evaluate many polynomial
o 2
60 expressions and perform basic arithmetic operations like addition and subtraction with
those numbers. For this, you will have to get a way to represent those polynomials. The
Addreas af
ROW|coLUMN/VALUE Dext node
simple way is to represent a polynomial with degree 'n' and store the coefficient of n+1
NODE
STRUCTURE
structure &
also explain its operations? terms of the polynomial in the array. So every array element will consist of two values:
a Disjoint set data don't have
any elementtin common,
the

Coefficient and • Exponent
Q.6. What
tis
sets are called
disjoint sets if they
Polynomial representation using Linked List
Ans. TwO of

intersection of sets is a null


set. subset felements is called The linked list can be used to represent a polynomial of any degree. Simply the
overlapping or digjoint
stores non supports following operations; information field is changed according to the number of variablesused in the polynomial.
A

data structure that


structure. The disjoint set
data structure
disjoint set data set.
Ifa single variable is used in the polynomial the information field of the node contains
to the disjoint operation, two parts: one for coefficient of variable and the other for degree of variable. Let us
• Adding new sets disjoint set using Union
sets to a single consider an example to represent a polynomial using linked list as follow8:
• Merging disjoint set using Find
operation,
representative of a disjoint Polynomial: 34-4x2+ 2x-9
• Finding
are disjoint or not. following taskg to
Linked List:
• Checkiftwo sets
a number of persons and the
Consider a situation with ROOT

performed on them: of another Deroo


relation, i.e. a person z beconos tbe riend
•Add a new friendship
newelement to a set. or indirect friend)
yie adding x is a friend ofindividualy (direct

Find whether individual In the above linked ist, the external pointer ROOT point to the frst node of the
an algorithm that performs two useful operations on such linked list. The first node of the linked list contains the information about the variable
A union-ind algorithm is
can be u8ed for with the highest degre. The first node points the next node with
a data structure: to next lowest degree
a particular element is in. This of the variable.
•Find: Determine which subset same subset.
determining if two elements
are in the Representation of a polynomial using the linked list is beneficial when the
Tlainne Join two subsets into a single subset. Here irst we nave to check u the operations on the polynomial like addition and subtractions are performed. The resulting
two subsets belong to same set. If
no, then we cannot perform union. polynomial can also be traversed very easily to display the polynomial.
Q.7. What is the Polynomial? ROOT 1

Ans. A polynomial plr) is the expression in variable x which is in the form


..., k fall in the category of real numbers and n'
(ar + brat ...s +jx+ k), where a, b,
c

is non negative integer, which is called the degree of polynomial.


An essential characteristic of the polynomial is that each term in the polynomial
expression consists of two parts: ROOT 2

one is the coefficient • other is the exponent
Example:
10:*² +26z, here 10 and 26 are coefficients and 2, 1 is its exponential value. --aa-AH-DX
The above two linked lists represent the polynomials,
Points to keep in Mind while working with Polynomials: 34- 4x + 2x-9 and
• The sign of each coefficient and exponent is stored within 5x- 2*+ 6x + 3 respectively. If both the polynomials are added then the resulting
the coefficient and the linked will be:
exponent itself
• Additional terms having equal exponent ROOT
is possible one
• The storage allocation for each term in
the polynomial must be done in ascenaing
and descending order of their exponent
8
Q.8. How to represent a Polynomial?
The linked list pointer ROOT gives the representation for polynomial,
8-6x2+ 8x-6.
EXAMINATION 2-2014 Third Senester, Data Structures
FIRST TERM (B.TECH) (ETCS-209] Types of linked list
SEMESTER (1) Singly linked llet
THIRD STRUCTURES-SEPT. 2014 (2) Doubly linked list
DATA (3) Circular linked list
M.M.:30 (4) Circular doubly linked list
Question No. l which iscompulsory. Q.1.(c) Reverse a linked list without using the pointer?
Time: 1.30 hrs. questions in total including type?
Note:Attempt any three What are its Ans. To reverse a linear linked lizt, three pointer fields are used. These are tpt, ptr,
a Double ended queue? can iinserted or deleted at either cpt which hold the address of the previous node, current node and next node. To begin
Q.1.(a) Define element be end.
can be added to or removed with the address of the first node, which is held in pointer variable FIRST, is assigned
which the
a list in
Ans. A deque is head-tail linked list,
because elements to PTR and typt is assigned value NULL.
known as a or back (tail) end. Algorithm:
It is also front (head)
from either the shown in fig. Step 1 PTR FIRST
Consider the deque Step 2 TPT = NULL
|37 45 54
63
29
8 9 Step 3 Repeat step 4 while PTR! = NULL
6 Right=7
Left=3 4 5 Step 4 (a) CPT= LINK(PTR)
2
01 (b) LINK (PTR) = TPT
63 |27 18 (c) TPT = PTR
42 56 8 (d) PTR = CPT
4 5 6 Left=7
Right=0 1 2 Left=3
Step 5 STOP.
Double-ended queues Q.1.(d)(t) Convert the following in fix expression into apostfix using a stack.
double-ended queue. {a + (6-c)}* {(d-e)/(8 +g- h}
variants of
Basically, there are two dequeue, insertions
can be done at one of
the
deque: In this Ans.
(1) Input Restrictedcan be done from both ends.
dequeue, while deletion can be done only atons Symbol Scanned Stack Express (Post fix)
In this dequeue, deletions
(2)Output Restricted deque: can bé done on both ends.
of thedequeue, while insertion 1

Q.1.(6) What are linear list?


What are it types? 2
collection of data items called
nodes. These
An, linear list is non-sequential
a 3. (+
A
structures containing fields, each node in a linked list has basicallv a
nodes in principle are 4. ab
two field. 5
ab
(1) Data field 6. abc
(2) Link field. link 7. (+( abc
The data field contains
an actual value to be stored and processed. And the
8. ) (+ abc
next data item in the linked list.
abc - +
field contain the address of the 9

|START
10 abe +
11 *{ abc – +
NFO 4INFO X
12 *{( abc – +
13 abe - +d
Single linked list 14 *{( abc -+ d
15 *{( abc + de -
START 16 abc + de -
17 abc -+ de
PREV INFoNEXT Prev 18. abc - + de -
Infonext Prev info next
19. abc - + de - f

Nodes +
20. *V(+ abc de-f
21. *W+ abc – + de -fg
Doubly linked list
University-(B.Tech)-Akash Books 2014-3
LP. 4-2014 Third Semester, Data Structures
abc- + de - Sg
22 abc - + de-fgh
*/+ Void print_str (char*S)
23.
*1/ abc- + de- fgh -+
24 abe -+ de - fgh -+/*
int i;
25
linked for (i = 0; S [i); i ++)
If youare using C language to implement the heterogenous
Q1.((ü) putchar (SiD; putchar (\n);
list, what pointer type will you t
heterogeneous linked list. The
Ans. We use the void pointer to implement the we need a link, Q.2.(6) What is Abstract Data type and its implementation through data
heterogenous linked list contain different data types in its nodes and we use structures?
pointer, to connect them. Since we can't use ordinary pointer for this, the void
to any type. Ans. Abstract Data Type: Abstract data type is the specifications of the data type
pointer. Void pointer is a generic pointer type, and capable of storing pointer which specifies the logical and mathematical model of the delta type.
Q.1.(e) What are threaded binary Trees? How are threads different from To implement a fraction data type:
pointers? Andhow we can determine the next node to whicha thread points?
Ans. In a linked representation of any binary tree, half of the entries in the pointer Type day strict ( int numerator, denomination; fraction;)
field LEFT and RIGH will contain null entries. This space may be more efficiehtly main ()
used by replacing the null entries by special pointers called threads. Which points to
the nodes higher in the trees. Such trees are called Threaded Tree. fraction f;
Difference between threads and pointer: Threads in a binary tree must be f. numerator = 1;
distinguished from normal pointer. In a graphical representation of a threaded binary f. denomination=2;
tree, threads are shown by dotted lines. In a computer memory, an extra field called tag
or flag is used to distinguisha thread from a normal pointer.
Tree can be threaded using one way threading or two way threading.
Ina one way
threading, a thread will appear in the right field of the node and will point to the
sucoessor node in the in order traversal of tree: and in two way threading tree a Application-use the ADT
of thread
will also appear in the left field of a node and will point to the preceding node in
the in
order traversal of tree.
|Specification -define the ADT
Q2.(a) Write an algorithm or pseudo code to extract characters from a given
string from a given position for n characters and store it another string which is
initialized at run time. Implementation-Implement ADT
Ans.
#include <stdio.h> When we use abstract data type, our program divide into two:
# define SIZE 100 The Application: The part that use Abstract data type.
void print_str (char S [); The Implementation: The part that implement the Abstract data type.
void read_str (char S); Q2.(e) How will you check the validity of an expression containing nested
main ( ) parenthesis?
{
Ans. To check the validity of an expression containing nested parenthesis we use
char str [SIZE); the stack. Stack works fine for this. The steps are:
do {
(1) Maintain a stack of characters.
read _str (Str): (2) Whenever you find opening braces ?,
print_str (Str);
} while
(Str [0];
(3) Whenever you find closing traces ,
or push it on the stack.
P, T check if top of stack is corresponding
opening bracket, if yes then pop the stack, else break the loop and return false.
(4) Repeat step 2-3 until end of the string.
void read str (char *S) Q.3.(a) Write à pseudo code/algorithm for
(1) insertion at the end
ofa circular linked list.
int i; (iü) Deletion of a node with a given data
char c; ina doubly linked list.
Ans.
for (i = 0; (c = get char () ) != "\n';i++)
S (i] = c; FIRST

Si] = NULL; 10 18

PTR
CPT
University-(B.Tech-Akash Books 2014-5 Third Semester, Data Structures
I.P. 6-2014
FIRST node **colhead; / Head element of columns
(CPT) till link (CPT)
Repeat CPT = Link node ** rowhead; // the head element of rows
i.e int rows cols; // number of rows & columns of matrix;
=
LINK (CPT) PTR operations of the class;
LINK (PTR) = FIRST
PTR
FIRST
CPT Q.3.(e) Write a pseudo codelalgorithm to remove duplicate elernent froma
25 list?
10 18 Ans. Transverse the list from the hed (or start) node. While Traversing, compare
each node with its next node. If data of next node is same as current node then delete the
next node. Before we delete a node, we need to store next pointer of the node.
At the end overflow and STOP. Program:
1. IfAvail = NULL then linked list is
# include < stdio.h>
2. PTR+AVAIL # include < stdlib.h>
AVAII +LINK (AVAIL) Struct node
Read INFO (PTR) (int data;
3. CPT + FIRST struct node * next;
4. Repeat step 5 while LINK (CPT) != FIRST
5. CPT + LINK (CPT) Void remove Duplicates (Struct node * head)
6. LINK (CPT) PTR
LINK (PTR) + FIRST Struct node *current = head;
Struct node* next - next;
7.

8. STOP ==
if (Current NULL)
(ii) Ifa specific node is given. return;
L ifFIRST = NULL then write UNDERFLOW and STOP.
While (Current > next! = NULL)
2. Read DATA as information of node to be deleted.
3.
4.
PTR+ FIRST
=
Repeat step (5) while INFO-(PTR) DATA
if (Currnet - 7 data = = current -> next-> data)
5. PTR c
RPT (PTR) next-next = current -> next-> next;
6. CPT cLPT (PTR) free (current_ next);
TPT+RPT (PTR)
RPT (CPT) + TPT else
LPT (TPT)+CPT
7. RPT (PTR) +AVAIL
current current-> next;
=

AVAIL PTR
8. STOP.
Q.3.(6) How can we use linked list to store a sparse matrix? void Push (Struct node **
Ans. The most advantage of linked list-is manageability of memory and it is very
head-ref, int new-data)
imporant for large programs which are used to process a lot of data.
To represent a sparse matrix we use a inked list for
Struct node * new_node = (Struct node*) malloc (size of (Struct node));
each row and each column. So new_node-> next= (*head-ref);
its needs two pointers : Next in this row and next in this column. (*head-ref) = new-node;
Struct node
node * next (01;
Void printlist (Struct node * node)
node * next row;
int row;
While (node! = NULL)
int col;
int value;
printf ("/d", node -> data);
; node = node - > next;
Class matrix
University-{B.Tech)-Akash Boolke
LP 2014-7

8-2014 Third Semester, Data Structures


(iü) A is deleted
int main )
NULL;
*head=
struct node
20); B
Push (& head,
13);
Push (& head,
13);
Push (& head,
11);
Push (& head, FRONT REAR
11);
Push (& head, are inserted.
11); (iüü) D & E
Push (& head, removal"):
before duplicate
print f(An linked list
List (head); B D w
E
print
remove Duplicates (head);
duplicate Removal);
printf(in linked list after
printlist (head); FRONT REAR
getchar O;
(iv) B &C deleted.
9.4.(a) Consider
a
float array APR (2 :8,-4:1), 6:10) whose base add.
200. Find the address ofAPR (5, 1, 8]
-
D E
Ans. APR (2:8, -4:1), 6: 10)
length of three dimensional ofAPR are
4 =8 -2+l =7
1
=6 L,= 10-6+1 =5
L, =1-(-4) +

FRONT REAR
:L,: Ly =7x6x6= 210 elements.
Accordingly APRcontains L,
Suppose the programming language store APR in memory in row major order and (U) F inserted
Base (APR) = 200 w =4 words per memory cell.
The effective indices of the subscripts are:
E, =5-2 =3 E, =-1-(4) =3 E, =8-6=2. F D E
For row major order, we have:
E,L, = 3 x6= 18
EL, +E, = 18 +3 = 21 REAR FRONT
(E,L, +E,)L, = 21 x5= 105
(E,L, +E,)L, +E, = 105 + 2 = 107 (vi) D is deleted.
Add (APR (5, - 1, 8]= 200 + 4 (107) =
200 + 428= 628.
Q.4.(6) Consider a circular queue
Front & REAR when following with fixed 5 position. Find the value of
operations are performed F E
(i) A, B, C, inserted.
(i) A is deleted.
(iüü)D &E are inserted. (iv) B &C deleted.
(v) Finserted. (vi) D deleted. REAR FRONT
(oii) G & Hinserted.
Ans. () A, B, C, inserted.
(vii) G &H inserted.

H E
Circular queue

A B C
REAR FRONT

FRONT REAR
Structures
Third Semester, Data
24-2014
on any two of the following: FIRST TERM EXAMINATION [SEPT.-2015]
Q.8. Write short notes
(a) Bucket Hasing records directly in the hash table.
Each record is) THIRD SEMESTER (B. TECH|
Ans. Closed hashing stores all position
h
h (K). T'he slot computed
by
a home that is
the key value value K, has DATA STRUCTURE ETCS-209]
function. bucket to which it is mapped has
available spaco.
When a record is inserted, the space, however it
store the record. Ifthe bucket does not have enough occur done to following reasons.
err
indicates an Time: 1½ Hrs.
MM:30
overflow Note: Attempt Q.No.I which is compulsory any two more questions.
condition called Bucket Overflow. Bucket
which we denote by nb, must he
Insufficient Buckets: The number of bucketnumber of record. Q.1. (a) How the resulting BST will look if the input is already sorted
nn
in
>
choosen such that nb nrfr. Where nr denote total
decreasing order. (1)
fr denote the record that fit in the bucket. more frequent than that Ans. Ifall nodes are already sorted in non decreasing order then all nodes are inserted
Skewness: If the selection of a bucket, during insertion is a
bucket is said to be skewed as against being symmetrical. into the right side of binary search tree, for example
of others, the
• Bucket methods are good for implementing hash table stored on disk, because nodes are 1, 2, 3, 4, 5
the bucket size can be set to the size ofa disk block.
(6) Collision Resolution.
the BST will look like -
suppose
Ans. Suppose we want to add a new record R with the keyK to our file F, but
the memory location address Hk) is already occupied. This situation is called collision.
(1) Collision resolution by open addressing.
(2) Collision resolution by separate chaining.
Open Addressing: In this, all elements are stored in the hash table itself. That is
each table entry contains either an element of the dynamic set or NEL.
Advantage: The advantage of open addressing is that it avoids pointers together.
Instead offollowing pointers, we compute the sequence of slot to be examined. The extra
memory freed by not storing pointers provides the hash table with a large number of
slots for the same amount of memory.
9.1. (6) Construct Binary tree T from following sequence.
Hashing with Changing: This method maintain a chain of elements which has (2)
the same hash address. We can take the hash table as an array of pointers. Size of hash PREORDER: ABDGCEHIF
table can be number of records. Here each pointer will point to one linked list and the
elements which have the same hash address will be maintained in the linked list. We INORDER: D GBAHEICF
can maintained the linked list in sorted order and each elements of linked list wil] Ans.
contain the whole record with key.
Adyantage: The main advantage of this hashing is saving of memory space and PREORDER:ABD GCEHIF
perfect collision resolution. INORDER:D GBAHEICF
• Here hash table contain only pointer which
point to linked
list-containing records.
Disadvantage: The main disadyantage is also wastage of memory space. In case
where records are very small or contains only key.
(e) Complexity of program.
Ans. The complexity of an algorithm is a function fn) which measure
for spaçe used by an algorithm in terms of input size n.
the time and
The choice of particular algorithm depends on following
performance analysis and D

measurements.
(1)Space complexity: Analysis of space complexity of an algorithm or program is
the amount of memory it needs to run to complete.
The space needed by the program
consist of following components.
(1) Instruction space (2) Data space (3) Environment stack space. Q.1. (c) Number of nodes in complete trce is 1000000. Find its depth.
(2) Time complexity: Time complexity an (1)
needs to run to completion. The exact of algorithm is the amount of time it
time will depend on the implementation Ans. The depth of complete binary tree is log(n) where no. of nodes is n. So the depth
algorithm programming language, of the of complete binary tree which has the 1000000 nodes is.
CPUspeed, other hardware optimising the capability of the compiler
and so on. used, the

To measure the time complexity log (1000000) = 6
accurately, we have to count Q.1. (d) Write algorithm/programto perform POP operation cn stack using
performed in an algorithm. all sorts of operations
The time complexity also singly linked list. (2)
depend on the amount of data input to ar
algorithm.
Structure
Semester, Data
Third I.P. University-(B.Tech.-Akash Books 2015-3
2-2015
Ans. Void pop (c) Insert a

-1
Struct node ptr;
NULL)
If (top = = Insert z

(printf ("Underflow")
return;

ptr = top;
top = ptr link: Insert 2
free (ptr);

ground header linked1list and circular


between
Q.1. (e) What is the difference
(1) There is Balance factor of a is -2 so after R-L rotation the tree is.
header linked list? m

node contains the


Ans. A grounded header list is header list where the last
a

pointer.
points back to the bee.3. Insert y
A circular header list is a header list where the last node
node.
Q.1. () What is the condition that a circular queue is full if the queue i
implemented using arrays? (1)

Ans. If (front ==0) && (rear == Max- 1) (front == rear + 1)) o)


that means queue is overflow or full.
Q1. (g) Binary tree with Nnodes has exactly ss
Null branches (1)
Ans, Binary tree with N nodes has exactly n + 1 null branches. Insert c

Q.1. (h) What is Time Space Trade off?


Ans. Time-Space tradeoft: Is a way of solving a problem calculation -2
by using more space (or memory), or by solving a problem very in less time
a long time. Most computers have a
in little space by spending
large amount of space, but not infinite space. Also, a
most people are willing to wait a little while
for a big calculation, but not forever. So
your problem is taking a long if
time but not much memory, a space-time
let you use more memory and solve tradeoff would +1
the problem more quickly or could be
quickly but requires more memory it solved very
than you have, you can try to spend more
the problem in the limited memory. time solving Co
Q.2. (a) Why height
, balancing tree is required?
4,z, b, y, *, d, w, e, v, f.
CreateAVL tree of following. There is Balance factor of z
is + 2 so after L-Lrotation the tree is -
Ans. The first balanced (8)
tree with has an additional binary search tree or height balancing tree was
left and right subtrees balancing condition. The simplest idea the AVL
have the same height, so is to require that the
AVL tree of height balancing tree
nodes-a, z, b, y, c, x, w,
e,
is required. 0(a)
d, v,f.
Creation of AVL
Tree
Nodes: a, z, b, y, c, x,
d, w, e, v,
f
Structure
Semester, Data
Third
4-2015 2015-5
I.P University-[B.Tech.-Akash Books
Insert x After Insertion of w the Balance factor is unbalanced so after applying the L-R
-2 rotation the tree is

0(w)

o)
after L-R rotation the tree io
There is the Balance factor
of b
is-2 So Insert e

Inset d

After Insertion of node e the tree is unbalanced so after applying the L-L rotation
the tree is

Insert L

Insert u
-2

+2
+1(6
0(a)
o) -2(0
-10
(w)0
o
Structure
Semester, Data
Third
aner apply the LP. University-(B.Tech,-Akash Books
unbalanced go
R.R
rotation 2015-7
6-2015 tree is he
node the
v
(b) LINK (PTR) -TPT
Aner InBert the
(e) TPT PTR
tree iP
m
(d) PTR CPT
Step 6 Stop.
Q.3. (a) Considor the following in fiz expression and convert into reverse
polish notation using stack. (5)
Ans, (A + (B*C-D/E^F)"G)H)
Character Stack Reverse polish Notatic
o
D0 Scannod Postfix Notation
( (

A ( A

Insert f A
(+( A
B (+( AB
W (+(* AB
(+(* ABC
(+( ABC*
( (+(-( ABC*
D (+(=( ABC* D
(+(-(/ ABC * D
(+(-(/ ABC * DE
o
(+(-(/A ABC* DE
F (+(-(/^ ABC * DEF
Afer Insert the node fthe tree is unbalanced so after apply the R-L rotation the ) (+( ABC * DEF A
final tree is (+(-* ABC * DEF ^/
G (+(-* ABC * DEF ^/G
(+ ABC* DEF ^/G*
ABC* DEF ^/G*
H ABC* DEF ^/G* -
ABC * DEF ^/G*-H*+
o Reverse polish notation
+1

Q.3. (6) What is an algorithmprogram for inserting and deleting a node at a


given location in circular linked list. (5)
Ans. Inserting a node at given location.
Q2. (6) Write a procedure/programto reverse a Step 1. IfAVAIL = NULL then write overflow and stop
any more memory? singly linked without using
(2)
Step 2. Read DATA as information of node after which insertion will be made.
Ans. Reverse a singly
linked list. Step 3. PTR -AVAIL
Step 1 PTR = PIRST
AVAIL = LINK (AVAIL)
Step 2 TT = NULL
Read Info (PTR)
Step 3 Repeat step
4 while PTR! = NULL Step 4. CPT = FIRST
Step 4 (a) CPT = LINk (PTR)
Step 5. Repeat Step 6 while Info (CPT) ! = DATA
Structure
Semester, Data
Third
8-2015 I.P. University-(B.Tech.J-Akash Books
LINK (CPT) 2015-9
CPT=
Step 6. = LINK (CPT) o.4. (c) Write algorithm/program for insertion
operation on queue using
LINK (PTR)
Step 7.
= PTR Singly linked list,
LINK (CPT)
Ans.
Stop.
Step 8. ()
given Location. void insert
Deletion of node at then write
underflow and stop.
IFIRST NULL
= deleted.
Step 1. node to be
as information of
Read DATA struct node * ptr;
Step 2.
Dtr = (struct node *) malloc (size of (struct node));
Step 3. PTR = FIRST DATA
info (PTR)*
Step 4. Repeat step 5 while int item;
CPT = PTR prinf ("Input the element for Inserting");
Step 5.
PTR = LINK (PTR) scanf ("/d", and item);
LINK (CPT) LINK PTR)
=
Step 6. ptr info = item;
Step 7. LINK (PTR) = AVAIL ptr Link = NULL;
AVAIL = PTR
If(front ==NULL)
Step 8. Stop.
are declared. as A(-2:2,2:22)
B
front = Ptr;
Q4 (a) Suppose multidimensional arraysA and else
and B (1:8, -5:5, -10:5) storcd in column major order.
rear link = ptr;
() Find the length of each dimensións ofA andB. (2.5)
A rear ptr;
(ii) The number of elements in andB.
(1
(iüi) Considerthe elements B(3, 3, 3) in B. Find the effective indices
E1,E2,
E3, and the address of the element assuming Base(b) =400 and there are w
words per memory location. (2.5)
Ans. (i) The length of a imension is obtained by:
Length =upper bound -Lower bound +1
Hence the length Li of the dimensions ofA are:
+ 1
Ll =2-(-2) =5 and L2 = 22-2 +1=21
Ll=8-1 +l =8, L2 = 5-(-5) + 1+ 11, L35-(-10) + 1
=16
(it) Accordingly A has 5*21 = 105 elements
and B has 8*11*16 = 1408 elements.
(ii) The effective index Ei is obtained from Ei = ki-LB,
and LB is the lower bound. where ki is the given index
Hence El=3-1
=2, E2
=3-(-5)=E3 =3-(-10) = 13
Considering B is stored
in column-major order,
calculated as. the address of B [3,3,3] will be
E3L2 = 13*11 = 143,
E2L2 + E2 = 143+8 =
151
(E3L2 + E2) L1 = 151*8 =
1208, (E3L2+ E2)
Therfore address L1+ El = 1208 + 2 =
of B (3,3,3] = 400 + 1210
Q.4 (6) What 4(1210) = 400 +
is Garbage Collections? 4840 =5240.
Ans. Gabage Collection:
automaticmemory management Garbage collection (GC)
memory is a dynamic approach
block and reallocates and heap allocation that pr0cesses
collection is to storage for reuse. and identifies dead
reduce Luemory The primary
leaks. purpose
of garbas
EXAMINATION [NOV-201s]
ILP. University-(B.Tech.-Akash Books
TERM TECHI 2015-11
SECOND SEMESTER (B. Ans.
THIRD (ETCS-2091
STRUCTURE Name Best Average
DATA Worst
Quick Sort N logN N logN
MM: Merge Sort N logN N logN N logN
more questions Heap Sort
Time: 1% Hrs. compulsoryany two N logN NlogN N logN
Q.No. which is B+ tree? Selection Sort
Note: Atempt l B-tree and
N² N2
Difference betwecn pointersin all ofthe: index nodes; B+ tree 2 Q.1. (e) What is pseudorandom hashing method?
Q.1. (a) record only
Original B-tree had in Ans. Pscudorandom Hashing
Ans.
andR around it
leafnodes. node pointers L A common random-number generator is shown below.
Given a key K
and the two pointer L and R around it
two node
key K and the to by Lare <K.
All values pointed To use the pseudorandom-number generator as a hasing method, we set x to
1. Al key to by R and > =K. the
torm a sequential fle
values pointed key, multiply it by the coefficient a, and then add the constant c. The result is then
2. All key are linked together to 1
data pages divided by the list size, with the remainder being the hashed address.
B+ treeConstruct 3-waysearch tree for the following list of keys.
(6)
L
Example:
15, 25, 30, 45
List: 20, 35, 40, 10, 45 Y = (17 x 122267) modulo 307
List: 20, 35, 40, 10, 15, 25, 30,
Ans. (2061539 + 7) modulo 307
3 way Search tree.
Y=
Y = 2061546
20 35
Y = 41
Q.2. (a) Construct B-TREE of order 5 by inserting the following elements.
3, 14,7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25 and 19.And also delete
40 45
10 15 values 6, 23, 3 from above constructed tree.
Ans. B-Tree of order 5:
Elements: 3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25, 19
25 30
Insert 3
3
Q1. (e) Consider the following specification of a graph.
vG) = (1, 2, 3, 4, 5) and E(G) = {(1, 2), (1, 3),
Draw its adjacency matrix.
(3, 3), (3, 4), (4, 1), (4, 5), (%,2)
iiiiInsert 14
Insert 7
314
3714
Insert 1
13714|
Ans. V(G) = {1, 2, 3, 4, 5}
E(G) =(1, 2), (1, 3), (3, 3), (3, 4), (4, 1), (4, 5), (5, 2)}
Adjacency Matrix Representation.
Insert 8
1|0
1 2

1
3

1
4

0
5
0 i |13 |814
20 0
000 Here node was already full 50 after insert 8, is splitted
it into 2 node and 7 is the
300 1
10 median key.
4|1 0-0 0 1

Q1. (d) Compare


50 1
00 0
the complexities in average case
and worst case
thr
of
following sorting:
Insert 5
Merge sort, Heap sort,
Quick sort, Selection sort -8 14
Structure
Semester, Data
Third
ILP.
University-(B.Tech.-Akash Books
12-2015 2015-13
7 Here node was already full so after Insert 19 it is splitted, into 2 nodes. 17 is the
17
median key so it will go into parent node. Here perent node is already full, so it is
17 81114 splitted in 2 nodes and 13 is the medían key and it will become the new root.
Insert 1l and After delete the node 6

7 13
13
14 17 48 17 20
Insert 13 5 811
13 splitted into 2 nodes
was already full
so after insert 13, It is and 131a
|7 112 1416 18 19 23 24 25 26
Here node After delete the node 23
the median key.
13
Insert 6, 23, 12, 20
7
13 4 8 17 20

811 12 |14 1720 23 14 16 18 19 24 25 26


|13 56 After delete the node 3
8 13 17 20
Insert 26
|71320|

12
14 16
2526| 1819 | 24
|1|35 |6 811 14| 17 23 26
Q.2. (6) Write algorithm/program of depth first search for traversal of graph?
L
Here node was already full, so it is splitted into 2 nodes and 20 is the median Ans. Step 1: Set status = 1 (ready state) for each node in G.
Insert 4 Step 2 :Push the starting node on the stack and set its status =2 (writing state).
A

Step 3 : Repeat steps 4 and 5 until stack is empty.


4713|20 Step 4: Pop the top Node N. Process it and set its status = 3 (processed state).
Step 5: Push on to the stack all the neighbour of that are in the ready state N

17 (whose status = 1) and set their status = 2 (waiting state).


35|611 1214| 23 26
Here node was already full so after Insert 4, It is splitted into 2 nodes and 4 is the
Step 6: Exit.
median key. Q.3. (a) Suppose the table T has 1l memeory locations T (1}, T{2),... [11)
Insert 16, 18, 24, 25
and suppose the FileF consist of 8 records A, B, C, D, E, X, y,z Record: B, CD, A,

E, X, Y, Z and its hash address U(K): 4, 8, 2, 11, 4, 11, 5, 1 resp. How file F will
appear in memory when 8records are entercd into table T in above order using
47 13 2o
linear probing and quadratic probing collision resolution technique? Find
average successful search probes (S) and average unsuccessful search probes
1|3|5|6 8 11 12 14 16
17 18 23 24 25 26 (S) and average unsucccssful search probes (U) also for both? (6)
Insert 19 Ans.
Record A, B. C. D. X, Y. Z E

13
H () 4, 8, 2, 11, 4. 11, 5, 1

Suppose the 8 records are entered into the table T in above order using the liner
|47
17 20 probing and Quadratic probing collision resolution technique then the file F will appear
in memory as follows.
1 3
|56 |8 11 12 14 16
Table T: X, C, A. E. Y B. D
18| 19 23|
24 25 26 Address: 1 2, 3. 4. 5. 6. 7. 8, 9, 10, 11
Structure
Semester, Data
Third
address Hk))=5, the record not
14-2015 with hash is
of a previous collisionaBsigned
record E because
only filled by I.P. University-(B.Tech.-Akash Books
Although Yis the already been at T4,. 2015-15
has
since T I5] appear in T(1). followe: Ans. () Division Method: Choose a prime no. M close to 99, such as M= 97 Then.
to T{5), not successful search
SimilarlyZ does probesfor a H (9614) = 11, H(5882) = 62 H (1825) - 79
average no. S of 1+1+1+1+2+2+2+3
The That is dividing 9614 by 97 gives a remainder 11. dividing 5882 by 97 gives a remainder
8 62 and so on. In the case that the memory address begin with 01l rather than 00, we
choose that the function H(k) = k (mod M) + 1 to obtain:
(9614) = 11 +1= 12, H (5882) = 62 + 1 = 63 H (1825) - 79 + 1 = 80
H

1-1.6
8 (it) Midsquare Method:
folloure
unsuccessful search
probes for àn K 9614 5882 1825
average no. U
of

The
7+6+5+4+3+2+l+2+1+l+8 92428996 34597924 3330625
11 H(%) 28 97 06
Observe 4th and 5th digit are choosen for Hash address.
40 =3.6
= (iiü) Folding Method: Choping the keys Kinto 1, 2, 1 digits and add then. So the
11
'address will be:
no. of probes to find each of he 8 records, and thesecondsum
The first sum adds the 11 locatione (9614) = 9 + 61 +4 = 74
H

as empty location for each of the


adds the no. of probes to find H (5882) = 5+ 88 +2 = 95
Difference between internal sorting andlexternal sorting? Sort the
Q3. (6)
11, 88, 22, 77, 33, 66, 44, 55.
H
(1825)=1+82 +5= 88
following elements using shell sort: (1+3)
Ans. Internal sorting:
Which deals with sorting the data stored incomputers Q4. (6) Write algorithm/program of anyone sorting technique which is used
selection ete. sorting. when input elements are small? Difference between comparison based sorting
main memory. Example: Bubble,
and non-comparison based sorting? (3+1)
External sorting: Which deals with sorting the data stored in files, External
sorting is applied when data can not be stored in the
memory. Example: Files en Ans. When Input elements are small then many sorting techniques may be used
organization.
such as, bubble, sort, insertion sort, selection sort etc.
Shell sort: 11, 88, 22,77, 33, 66, 44, 55 Algorithm of Selection Sort
= 1. n =length [AJ
Input to pass 1 with distance 2
=
Forj 1to n-1
11 88 22 77 33 66 44 55 smallest =j
4. for i =j+1 ton
5. IfAE]<A smalest]
Output of pass 1 is input to pass 2 and distance=1
6. then smallest =i
11
7. exchange (A ], A smallest])
55 66 33 77 44 88
Comparison based and non comparison based sorting
Output of pass 2 is Comparison based sorting is a type of soring that only reads the list of elements
through a single abstract comparison operation that determines which of 2 elements
11 should occur ffrst in the final sorted list. While in nn comparison based sorting algorithm
22 33 44 66 77 88 there are no need of comparison between elements. Example of comparison based sorting
44. (a) Consider a company with 68 employee. are bubble, insertion, selection etc and radix sort is the non comparison based sorting
digit employee number which Each has been assignea a* algorithm.
has been assigneda 4
which is used as the primary
key
digit employee numbe
of memory in
addresses of the location)
the company's employee file. Suppose
--99. Then apply the division consist of 100 two
method and folding method using prime no, digit addresses U0
method closest to 99, mid sq
following employee no. to find out 2-digit
9614, 5882, 1825. hash address for eách of the
(2+2+2)
Third Semester,
Data Structure
a circular
chain The circular
32-2015
way which
torms linked
memory can be allocated when it
each other
a circular
in which means the isrequired.
dynamic size
FIRST TERM EXAMINATION [SEPT. 2016]
data next data next
data next 2
THIRD SEMESTER (B.TECH)
10
3 DATA STRUCTURE (ETCS-209]
Time: 1.30 hrs.
Last element points back to
firts
M.M. :30
Head
Note: Attempt any three questions including Q.no. 1 which is compulsory.
preferable to
non-circular ones, for two
reasons:
Circular lists are usually Q.1. (a) What are two main factors for measuring
S

access to both ends of a list: notice that we do


not the performance of
easy algorithm? Explain in bricf.
• They provide
because we can easily reach
the first node with needthe (2)
pointer in the header,
L->last>nerti Ans. Space and Time complexities are the parameters by which we can
s often
processing a circular list is simpler than the code analyze the
• The code for for performance of a program or algorithm.
you don't have to constantly checkifthe processine
a non-circular
list-mainly because next nodei
Space Complexity: It is the amount of memory it needed to run to completion.
defined-it is always defined. It
has a fixed pact that includes space for the cock, space for simple
variable and fixed size
component variables, constants etc. and a variable
part that includes the space needed
by component variable whose size is dependent on the
particular problem instance
being solved and the stack space.
Time Complexity: It is the amount of computer time needs to run to
completion.
It is the sum of the compile time & the run time. The compile time does not
the instance characteristics major concern is depend on
the run time of a program.
Q.1. (b) What do you mean by garbage
collection? (2)
Ans. Garbage Collection: Garbage collection (GC) a
is dynamic approach to
automatic memory management and heap allocation
that processes and identifies
dead memory block and reallocates storage
for reuse. The primary purpose garbage
collection is to reduce memory leaks. of
Q.1. (c) Define time-space
trade off? (2)
Ans. Time-Space tradeoff: Is a way of solving a
problem calculation in less time
by using more space (or memory), or by solving a
problem in very little space by spending
a long time. Most computers
have a large amount of space, but not
most people are willing to wait a infinite space. Also,
little while for a big calculation, but not forever. So
your problem is taking a
long time but not much memory, a space-time
if
let you use more memory and solve the problem more tradeoff would
quickly or it could be solved very
quickly but requires more memory
than you have, you can try to spend more time
solving the problem in the
limited memory.
Q.1. (d) How the resulting
binary search trce will look like if the input is
already sorted in non-decreasing order.
(2)
Ans. If all nodes are already sorted in non
decreasing order then all nodes are
inserted into the right side of a binary
nodes are 1, 2, 3, 4, 5
search tree, for example -
the BST will look like ->
Third Semester, Data Structure I.P. University-(B.Tech)-Akash
2-2016 Books
2016-3
(+(-(/A
ABC DE
(+(-(/A ABC DEF
)
(+( ABC DEF A

(+(-* ABC
DEF^/
G (+(-* ABC DEF
(2 ^/G
Q.1. (c)Number of nodes in a complete tree is 1000000. Find its depth? (+
n. ABC * DEF ^/G*
Ans. The depth ofcomplete binary tree is log(n) where no. of nodes is So the depth
^G -
(+ *
of complete binary tree which has the 1000000 nodes is. ABC *
DEF *

H (+*
log (1000000) = 6 ABC* DEF ^/G
Q.2. (a) Write a program/algorithm
to evaluatea postix expression using ABC DEF
^/G-H*+
stack? Consider the following infix expression and convert into postfix Reverse polish notation
+ (2+3)
expression using stack. (A (B"C-(D\E ^F)*G)*H)
Ans. Algorithm for Evaluation of Postfix Expression Q.2. (b) Write a proccdure/program to reversc a
singly linked list without
using any more memory?
Create an empty stack and start scanning the postfix expression from left to right. (5)
Ans. Refer Q.2. (a) End Term Examination 2016.
•If the element is an operand, push it into the stack.
Q.3. (a) Suppose multidimensional arrays A are dcclared as A (-2.2,
•If the element is an operator 0, pop tyice and getA and B respectively. Calculate 2.22) and B(1:8, -5:5,-10:5) stored in column andB
BOA and push it back to the stack. major order. (2+2+3)
(i) Find the length of each dimcnsional ofA and B
• When the expression is ended, the value in the stack is the final answer.
(iü) The number of elements in A und B.

Character Stack Reverse polish Notatic Consider the clements B[3, 3, 3] in B. Find the effective indiccs E1, E2,
(iüi)
Scanned Postfix Notation E3 and the address of the elcment ussuming Busc (b) =400 and
there are w=4
words per memory location.
A
Ans. () The length of a dimension is obtained by:
( A
Length = upper bound- Lower bound + 1

+ (+ A
Hence the length Li of the dimensions ofA are:
(+( A
Ll = 2-(-2) + l=5 and L2 = 22-2+l=21
B (+( AB = = = 16
Ll 8-1+l 8, L2 =5-(-5) + 1+11, L35 -(-10) + 1

(+(* AB (ii) Accordingly A has 5*21 = 105 elements and B has 8*11*16 = 140S elements,
(+ (* ABC (iüi) The effective index Ei is obtained from Ei = ki-LB, where ki is the given index
and LB is the lower bound.
(+( ABC*
Hence El =3-1= 2, E2 = 3 -(-5) = E3 = 3-(-10) = 13
(+(-( ABC*
Considering B is stored in column-major order, the address of B [3,3,3] will be
D
(+(-( ABC* D caleulated as.
(+(-0 ABC * D E3L2 = 13*11 = 143, E2L2 + E2 = 143+8 = 151

E (E3L2 + E2) Ll = 151*8 = 1208, (E3L2 + E2) Ll + 1208 +2 = 1210


(+(-/ ABC * DE El=
= + 4(1210) = 400 + 4840 =5240.
Therfore address of B
(3, 3, 3] 400
Third Semester,
Data Structure
a at a given Iocntt I.P. Uníversity-(B.Tech)-Akash Books
4-2016
an algorithmprogram for deleting node 2016-5
Q3. (b) What is (8) There is Balance factor of a is -2 8o after
list. RL rotation the tree is.
in circular linked

Ans.
Location.
Deletion ofnode at given
underflow and stop. Insert y
ICFIRST NULL then write o
Step 1

as information of node to be deleted.


Read DATA
Step 2.
Step S. PTR FIRST
DATA
Step 4. Repeat step 5 while info (PTR)

Step 5. CPT- PTR


PTR = LINK (PTR) oa)
Step 6. LINK (CPT) = LINK (PTR)

Step 7. LINK (PTR) = AVAIL


AVAIL = PTR Insert c
Step &. Stop.
QeaWhy height balancing of treeis requirod? Create AVL, tree offollowing:

azb,ge,x,d, e, v,f
w,
(2+6) o(a) 2)+2
Ans. The first balanced binary search tree or height balancing tree was the AVL
tree with has an additional balancing condition. The simplest idea is to require that the
left and right subtrees have the same height, so height balancing tree is required.
AVL tree of nodes- a, z, 5, y, c, z, d, u, e, t,f.

Creation ofAVL Tree There is Balance factor of z is +2 so after L-L rotation the tree is -
Nodes: a,z, b,y. c, z, d, u, e, ,f

Insert c
o

Insert z
Insertx

Insert z
Structure
Semester, Data
Third
So after L-R Totation the tree is-
I.P. University-(B.Tech)-Akash
6-2016
Balance factor
ofbis-2 Books
2016-7
There is the Insert e
C)

+1 (b O-2
o3)
+1

Insert d

After Insertion of.node e the tree is unbalanced so after applying


the L-L rotation
the tree is

Insert U

o
+2

Insert u

+16) (w
After Insertion of w the Balance factor is unbalanced so after applying the R
rotation the tree is
o -20

20
+1

After Insert the node v the tree is unbalanced so after apply the R.Rrotation the
tree is
Structuro
Semester, Data
Third

8-2016 LP. University-(B.Tech-Akash


Pooks 2016-9
INORDER:DGBAHEICP
*10

Insert/

W*1
+10

o)
o

so after
After Insert the node f the tree is unbalanced apply the. R-L rotation
te
final tree is

a o
Q4. (b) Following sequence gives
the preorder and inorder of the Binar
Tree T.

PREORDER :
ABDGCEHIF
INORDER : DGBAHEICE
Construct a binary
tree for the above sequence.
Ans. PREORDER:ABDGCEHIF
Structura
LData
Semester,
Third
circular
queue with N= 5.
following
=3. Front
Insert 20, Rear
=
e 0.
Considerthef
4.
32-2016
Example: Front=0.
FIRST TERM EXAMINATION [SEPT. 2017]
Initially,
Rear=0, Front
THIRD SEMESTER (B.TECH]
10 50
1.
DATA STRUCTURES (ETCS-209]
20 I3 Rear
Time: 1% hrs. MM.:30
Note:Q.l is compulsory. Attmept any two questions from the remaining:
Q1.Attempt all parts:
5. Insert 70, Rear =4, Front Q.1. (a) Convert the given infix into post-fix expression.
Demonstrate the
= 1,
Front =1. steps of conversion.
Rear 2
Insert 10. Front (2)
2.
2 10 50 a+b*- (de^)*g)*h a
+ (b*
Rear Ans. Infix into Postfix expression of c-(d/e^f)*g)*h:
10
Front 20 Character Scanned Stack Postfix Notation
70
Rear 4 +
+( a
6 Delete front, Rear = 4, Front = 2.
= 1. b +( ab
=2, Front
50, Rear
3. Insert +(* ab
Front
1 2 Rear 50 +(* abc
50
Front 10
+( abc*
20
( abc*
70 abc*d
Rear 4 abc*d
e +(/ abc*de
abc*de
f +^ abc*def
) +( abe*defN
+(* abc*def
+(-* abc*def^g
abc*def^g*.
abc*def^g*
abc*def^g*-h
abcdef/g*-h*+
Q1. (6) What is the maximum height of any AVLtrees with 7 nodes? Assuming
height is 0 for a tree with single node. (2)
Ans. The maximum height of any AVL-tree with 7 nodes is 3.
AVL trees are binary trees with the following restrictions.
1) the height difference of the children is at most 1.

2) both children are AVL trees


Structures LP University-B.Tech.J-Akash Books 2017-3
Semester, Data tree, height of the root node is gaid to be height of the tree. In a tree, height
Third of all leat
2-2017 nodes is 0.
Q2Attempt all parts:
Q2. (a) Caleulate the address of element at X[4,3] in a 2D array XL5J(14
b
stored in a roW major order. ASsume the base address to be 1000 and each
element requires 4 wordsof storage. 5)

/ Ans. Row Major Order


eg LOC(AJýJ – Base (A)4 wn
i-Lomer boand for row inde) fG–omer toanálor
(2) column indez)]
Base (X)=1000, w=4,n=4
What is a doubly linked list?
eL (c) navigation is possible t So the address of [4.3] = 1000 +4 [4 (4–1) + (3-1]
X
:
Doubly Linked List Linkedlist in which
Ans.
Linked List is a variation of as compared to Singie Línked Jict = 1000 + 4{4 x3+2] = 1000
+4x14 = 1056.
Doubly
ways, either forward and backward
easily
the concept of doubly línked ist Q2. (6) Write an algorithm to delete the last node of the linked ist. (5)
understand
both important terms to called an element. Ans. Step 1: If linked list is empty (STAPT NULL)
Following are the a
list can store data Next
Link-Each link of a linked contains a link to the next link called
a
Write "Sorry Buddy there is no node
- Each link of linked list previous link calledPre
•Next linked list containsa link
to the Step 2: If linkedlist have only one node (START >NEXT–NULL)
to the first link called
a
• Prey- Fach link of contains the connection línk free(START)
• LinkedList -A Linked List
called Last. START = NULL
Pirst and to the last linkRepresentation Step 3: If the list have more than one rode
Doubly Linked List
PTR =START
NULL –
while(PTR>NEXT NULL)
Next
Nes
PREPTB= PTR
NULL PTR= PTR->NEXT
in C?
used to implement a heterogneous list
QL d) What pointer type is 2) PREPTR>NEXT = NULL
Explainn brief? contains differet data types in its
nodes and free( PTR)
Ans. The heterogeneous nked list not possible to use ordinary poiters for
we needa link, pointer to Connect them. It is as Step 4 :EXIT
this. So we go for void pointer Void pointer is
capable of storing poiter to any type
Q3.Attempt all parts: (3+7=10)
itisa gnerie ponter type. termns used in tree Q3. (a) Consider the following circular queue with a storage size 6.
QLe) Explain the following
Terminal Node (i) Siblings FRONT = 2, REAR=4,QUEUB:„LM,N
i
iii) Degree of Node iv) Height of tree Describe the queue as following operations takes place:
Ans. (i) Terminal Node: Ina tree ata structure, the node which oes
not have a LAdd O 2.Add P 3. Delete two elements 4 Add QR,8 5.Delete one. (3)
ctáld is called as LEAP Node. In siaple words, a leaf is a node with no child. Ans. Initially
(ii) Siblings : In a tree data structure, nodes which belong to same Parent are called 1 2 3 4 5
as SIBLINGS. In sinple words, the nodes with sane parent are called as Sibling nodes.
(Gii) Degree of Node : In a tree ata structure, the total number of chíldren of a
L
node is called as DEGBEE f that Noe. In sinple words, the Degree of a node is total
number of chílåren it has.
iv) Height of tree iln a tree data structure, the total number of egdes fron leaf
rnode to a particular node in the longest path ís called as HEIGHT of
hat Node. In a Front Rear
Structures LP. University-B.Tech.-Akash
Data Books 2017-6
Semester,
Third
5
Ans, Insert J
4-2017
()Add 0
N
M
Insert R
R
Rear

Fron
5 6
4 Insert D
(ü)AddP R
2
1
N
M

Rear
Front
Insert G
two elements
(ii) Delete 3
4 5 6

1
D
Rear Insert T
Front

(iv) Add Q, R, S
3 4 5 6
1 2

R S P
R

Rear Front Insert E


(v) Delete one letter

1
m
2 3 4 5 6

Q R
P

Rear
Q.3. (6) Insert Front
the following elements Insert M
binary search tree: in alphabetical order
into an e
J, R, D, G, T,E,M, H, P, AF,Q
1.Find the final
treeT
Z.
Find the post-order traversal of l
Data Structures
Thind Semester,

62017 LP.University-(B
Toch.-Akash
Benka
2017-7

Insert H

Insert Q

This is final tree T.


) Post order traversal of tree
T is :
AFEHGDQPMTRJ
M
Q.4.Attempt all parts:
Insert P
o4.(a) binarytree has 9 nodes.
A

ofT vield the folowing sequences The inorder and preorder


E of nodes: traversals are
INORDER: EACKFHDBG (3)
PREORDER: FAEKCDHGB.Draw
the tree T.

Ans. INORDER: EACKFHDBG


PREORDER: FAEKCDHGB

Insert A EACK HDBG

(i)

HDBG HDBG

E
CK'

(iv)

(M)

Insert F
BG

This is final tree T.


Structures LP. University-(B.Tech.-Akash
Semester, Data Books 2017-9
Third following elements are jnserted
the Insert 6
AVLtree if
8-2017 Constructthe
4. (6)
order:
3214,56,16,15,14
of3,2,1,4,56,16,15,14
AVLtree
Ans.
Insert 3

Insert 2 Rebalancing
After

Insert
2

Rebalancing
Ater

Insert 16

Insert 4

After Rebalancing
Insert 5

16)

Insert 15

After Rebalancing

16)

(15)0
Structures
Semester, Data
Third END TERM EXAMINATION [DEC. 2017]

10-2017
THIRD SEMESTER [B.TECH)
Insert 14 DATA STRUCTURES [ETCS-2091
2 Time:3hrs. M.M.:75
16
Note:Attempt any five questions including Q.No. 1. Which
is compulsory. Select one
each unit
questionfrom
Whatiis an Abstract Data Type (ADYT)? (2.5)
Q.1. (a)
Data Types
Ans. Abstract
DData type (ADT) is a
type (or class) for objects whOse behayior is defined
Abstract
a set
of value and of operations.
Tis: set
finaltree by a
Rebalancingthe dofnition of ADT only mentions what operations are to be performed but not
After operations will be implemented. It does notspecify how data will be organized
how these called
memory andl algorithms will be used for implementing the operations..Itis
what
process of providing
in " because it gives. an implementation independent view. The
"abstract
2 and hiding the details is known
essentials
as abstraction.
onlythe
The user of data type need not know that
data type is implemented, for example,
bave been using int, float, char data types only with the knowledge with values that
on them without any idea of how these
n take and operations that can be performedto
16) So a user only needs know what a data type can do but not
types are
implemented.
how it will do it.
We can think of ADT as a black box which hides the
inner structure and design of
three ADTs namely List ADT, Stack ADT, Queue ADT.
the type. Nowwe'll
data define
(2.5)
Q.1. (6)What is a self-referential structure?
to create data structures like linked lists,
Ans. A self referential structure is used
of this kind of structure:
stacks, etc. Following is an example
struct struct _name

datatype datatypename;
struct_name * pointer_name;

to
one of the data structures which refer to the pointer
A self-referential structure is to
same type. For example, a linked list is supposed
(points) to another structure of the a is being pointed, which is of
structure. The next node of node
be a self-referential data
the same struct type. For example,
(
typedef struct listnode
void *data;
struct listnode *next;
linked_list;
)

structure -because the *next


listnode is a self-referential
In the above example. the
is of the type struct listnode.
Structures
Semester,
Data
Third

32-2017
Thble
Final
6 FIRST TERM EXAMINATION [SEP. 2018]
Probing 3 4234
(2)Linear 1
2
2341
|2839/430 THIRD SEMESTER [B.TECHJ
/3920
397 22 DATA STRUCTURES [ETCS-209]
Time:1.5 hrs. M.M. :30

collide at 3 mod 7 Note: Q. No. l is compulsory. Attempt any two more Questions from the rest.
Value (430)is combuted 8s: 7+ 1J
probe is = (430mod mod 7=4 Q1.Attempt all parts:
so the next H430, 1) mod7 =4 steps. (2)
= (3+l] (a) Calculate the postfix expression using stack also demonstrate the
5, 3, +8,2, ,*
so again: mod 7+ 2]
mod 7=5 mod 7
=5
Dot free = [430 Ans.
But the TI4]is
H(430, 2)
Symbol Scanned Stack Operation (B opA)
collide at 5. 5 5
value (397) is
Now ihe 3 5,3
Bo the next
probe is: 1) = 6 [5 +3] (A =3, B
= 5)
H(397, 8
8 8.8
next probe is:
not free so 2
But six is (397, 2) =0
H
8, 8, 2
2, B= 8)
collide at 0.
8,6 [8-2] (A=
(3920)is [8*6] (A=6, B 8]
=

Now the value 48


So the next probe is: =1 Q.1. (6) What do you understand by the term space-time tradeoff? (2)
H (3920, 1) Ans. Time-Space tradeoff: Is a way of solving a problem calculation in less time
probe is:
free so next by using more space (or memory), or by solving a problem in very little space by spending
But T(1] is not = 2.
H (3920, 2) a long time. Most computers have a large amount of space, but not infinite space. Als0,
Final Table most people are willing to wait a little while for a big calculation, but not forever. So if
(3) Quadratic probing 5 6 your problem is taking a long time but not much memory, a space-time tradeoff would
4
1 2 -3
2839 397 4234 let you use more memory and solve the problem more quickly or it could be solved very
430 22 3920 2341 quickly but requires more memury than you have, you can try to spend more time solving
computed as: the problem in the limited memory.
3. So the next probe is
Now the(430) is collide at = 7+ 1 mod 7 Q. 1. (c) Differentiate between arrays and linked lists. (2)
H (430, 1) [430
%
Ans.
= (3+ 1) mod 7=4
ARRAY LINKED LIST
so next is: Array is a collection of elements of similarLinked List is an ordered collection of
But the T4] is not free 7
H(430, 2) = (430%7 + 2°] mod elements of same type, which are
data type.
= (3+4) %7=0 connected to each other using pointers.
Now (3920) is collide at 0. So the next probe is:
Array supports Random Access. Linked List supports Sequential
Here T [1] is not free.
Access.
H (3920, 1) = 1
In an array, elements are stored in In a linked list, new elements can be
H(3920, 2) = 4 again T [4] is not free
contiguous memory location or stored anywhere in the memory.
H (3920, 3) = 2 It is free. consecutive manner in the memory.
In array, Insertion and Deletionoperation Insertion and Deletion operations
takes more time, as the memory locations are fast in linked list.
are consecutive and fixed.
Memory is allocated as soon as the array Memory is allocated at runtime, as and
is declared, at compile time. It's also when a new node is added. It's also known
known as Static Memory Allocation. as Dynamic Memory Allocation.
Structure
Semester Data LP. University-(B.Tech)-Akash Books
Third inkedist each 10delelemere 2018-3
In case ofa
2-018 ndeDendent and points to the
next, previous, orTmayoe Doably Linked List: In a doubly linked list, each node contains a data part and
S

each eiement valoe.


both nodes addresses, one for the previous node and one for the next pnde.
nKbeacessed singits inder
0

a Linked list be Linear(Singiy),


can

side
dimensioal to Doubly or Circular linked i Start
ATr C
tidinersion:al
queues
Enensiocal or types of (2
expiain different
QLBrietyqueoe: retained and dataleawes
10 30

Ans Typesof a the order is


FFOÇuece,
igie
in
Prey Data Next Prex Data Next Prer Data Net
Stmpeqese I Circular Linked List: In ireular linked list the last node of the st bolds the
same
e
arderiicitces iwictthe elements aregien a a
predefined ipriority.
address of the first node bence fofning a circular chain
çaese: AaEei back of:the queue
Priocity queue, erept that the is
Cralar Sienie
çaese:
to the simple
queue but can
add
data

3
ret data rext

2
Decble ended
qoeae Degaese Slar te quece. f

renove eiements nhe fuct Te back


sed tree
terns in 2
Defne the folowing
4Le Node Head LstEeest po tad bFrs
Teraal an to
.2 6) Write algorithm delete the second last node ofa oably inked
5)
DBalamce factor st
Ans. ptifrst;
Beigtree stretre te cde wich dces noth while ptrptrpt=NULL)
Ans i Teinal Node
zs IEAFNodeLinie
Iare aa
rs lezfis cdewiti no caili
z a

ilei Tbelcgto same Parent are cale


pre-ptr.
paet aecaied as Szbling re
háie iseoCeswSe ptr-ptrpt;
sNGS Palance Faeto iene baiaree arzeaca cde
e
as:
et suüe-heigcigt stree)
baianceFaar=heiit pre>rptpirpt;
The salzzce
ar atany scce af am
SLesiieieger rane-1,+1ft ptr>rptpt-pre;
freelptr;
9.3.Attempt all parts
(a) Write the algorithms for PUSH and POP operations in stack (0
zs
HIGHTfiaNce heict natree
2jiæ sien ersiszlei Ans. Push Operation: The proess cf putting a nsw data element cnto starsis
fe t nE S s be height ofthe tree hate heitof
al leaf nodes is. knOmn as a Push Operaticn. Push operation inrolves a series of steps
Step 1-Checks if thestack is fall.
2 Ergiain tiferent types ofinked ists
5
Step 2-Ite stackis înl, produces an errüI anderit
Step 3-Ithe stacisnot fall, increnents top to point next empty spae.
• Step 4-Aäds data element to the stack location, where top is pointing.
• Step 5-PeturIs SUcCess.
Pop Operation:Acesing the content while removing it fom the stack, is knowA
saPop Operation. APop operation ray invodve the following steps-
Step 1-Checks ifthestack is empty.
• Step 2-If the stacz is empty, produces an error and erit.
2 Step 3-If the stack is nst empty, acoesses the data element at which top is
pointing
• Step 4- Deereases the value of
tup ty 1.
• Step 5-PeturnS SUCESS.
Structure I.P. University-(B.Tech}-Akash Books
Semester, Data alphabetical orde: 2018-5
Third intoan empy
elementsin
4-2018 following
Insertthe 5
Q.3. 6) traversal of.
tree: P,A,F Z
binaryseArch G, B,E, M,H, in-order
R,D, Findthe
2.
.FindJ, thefinaltree Insert H

Ans. Insert

R
Insert

Insert P
D
Insert

Insert Insert A

B
Insert B G
M

Insert

B
Insert E

B:

Insert

Insert M

In order Transversal of above tree is:


ABEFGHDJMPRZ
Strture LP. University-(B Tech-Akash Books
2018-7
Semester, Data
Thind L-L Rotation: AVL tree may become unbalanced, ifa node is inserted in the left
preorder traversals
ofthe subtree of the left subtree. The tree then needs a right rotation.
6-N18 parts inorder and
4Attempt all nodes The (5)
has 9 nodes:
binary tree
sequences of
(a)A follomingEACKFHDBG Drawthetree.
reeyield the FAERCDHGB.
NORDER:
PREORDER: EACKFHDBG B 8
NORDER FAEKCDHGB
Ans. 0
PREORDER A

HDBG Left unbalanced Tree Right Rotation Balarced Tree


EACK L-R Rotation: Double rotations are slightly complex version of already explained
vèrsions of rotations. To understand then better, we should take note of each action
performed while rotation. Let's first check how to perform Left-Right rotation. A left
HDBG right rotation is a combination of left rotation followed by right rotation.
State Action
HDBG| A node has been inserted into the right
E subtree of the left subtree. This makes C an
unbalanced node. These scenarios cause AVI
CK
tree to perform left-right rotation.

(V)
B

BG
We first perform the left rotation on the left
E subtree of C. This makes A, the left subtree ofB.

heln t
This is final tree T
different types of rotations in AVL tree with the
Q4 b) Explain (5)
suitable examples.
Ans. AVL Rotations four kinds of rotationg
To balance itself, an AVL tree may perform the following
• RR rotation Node C is still unbalanced, however now, it is

Lrotation because of the left-subtree of the left-subtree.
• LR rotation
• RL rotation a
R-R Rotation: Ifa tree becomes unbalanced, when node is inserted
into the right
a
subtree of the right subtree, then we perform single left rotation
2
A C
0 We shall now right-rotate the tree,
B making B the new root node of this
subtree. C now becomes the right subtree of
its own left subtree.

Right unbalanced tree


Left Rotation
Balanced
Structure
Semester, Data
Third
8-2018 END TERM EXAMINATION NOV-DEC 2018]
THIRD SEMESTER (B.TECH]
now balanced.
The tree is DATA STRUCTURES (ETCS-209]
Time:3 hrs. MM:75
c double rotation
is
Right-Left Rotation.
It is a Note: Attempt any five questions including Q. No 1 which is compulsory Select ane question
from each unit.
secondtype
of

left rotation.
R-L
Rotation: The followed by
rotation Action Q1.Attempt all:
combination ofright
(a) What is bucket Hashing? (25)
State insertedinto ttheleft subtree
A node has been This
makes A, an Ans. Hash buckets are used to apportion data items for sorting or lookup purposes
subtree.
of the right The aim of this work is to weaken the linked lists so that searching for a specifc item
node with balance factor 9 can be accessed within a shorter timeframe.
unbalanced
Keys Hash Funcion Bucket

123-9876
Mark BelH

123-7654
First, we perform the
right rotation
subtree ae
along C node, making C the right John Reese
B. Now, B becomes tL
its own left subtree
of A.
right subtree 123-1234
Michelle Curtis -

A hash table that uses buckets is actually a


combination of an array and a linked
ist. Each element in the array (the hash table) is a header for a linked ist. All elements
ot hash into the same location will be stored in the list.
A The hash function assigns each record to the first slot within one
Node A is still unbalanced because of the of the buckets. In
ase the slot is occupied, then the bucket slots will be searched
right subtree ofits right subtree and requires open slot is found.
sequentially until an
a left rotation.
In case a bucket is completely full, the record will get stored in an overflow bucket
ofinfinite capacity at the end of the table. All buckets share the same overflow bucket.
However, a good implementation will use a hash function
that distributes the records
evenly among the buckets so that as few records as possible go into the overflow bucket.
9.1. (b) What is the condition to check overflow and underflowcondition in
cireular queue? (2.5)
A left rotation is performed by making B the Ans. If value of REAR variable is greater then or equal to SIZE – 1l and value of
B new root node of the subtree. A becomes the FRONT variable is equal to 0 then we can an
not insert element into Circular Quene.
left subtree of its right subtree B. This condition is known as “0verflow".

IFCircular Queue is empty then we can not delete an element from Circular Queue.
This condition is known as "Underflow"
Q. 1. (c) What is Header Linked list? (2.5)
The tree is now balanced. Ans. A Header linked list is one more variant of linked list. In Header linked list,
wa have a special
node present at the beginning of the linked list. This special node is
A used to store number ofnodes present in the linked list. In other linked list variant, if
We want to know
the size of the linked list we use traversal method. But in Header
linked list, the size of the linked list is stored in its header itself.

You might also like