Chp 2 Data Structure TPS
Chp 2 Data Structure TPS
INTRODUCTION TO DATASTRUCTURES
Q.1 Define the following terms:
(i) Data:
Ans. Data are simply values or set of values.
(i) Group items: m23 (March 2018)
Ans. Data items which are divided into subitems are called as group items. e.g. Date may be
divided into three subitemns - day, month and year. So Date becomes group item.
(iii) Elementary items: M23 (March 2018
Ans. Data items which are not divided into subitems are called as elementary items. e.g.
pincode number cannot be divided into subitems. So it is elementary item.
(iv) Entity: M23(March 2018
Ans. An entity is something that has certain attributes or properties which may be assigned
values.
The values themselves mnay be numeric or nonnumeric.
e.g. A Bio-data sheet mainly contains :
Attributes Name Age Sex Education
(vi) Record:
(Mareh 2005, 2009,2016, Oct. 2007,2014; July 2019
Bocord is a colletion of field values of a
given entity.
(vii) File:
ile is the collection of recOrds of the
(March 2005,2009,2016, Oct. 2007 2014 July 2019
entities in a givenentity set.
R# Serial Number Name Address Telephone
001 ABC Pune 5671922
002
XYZ Mumbai 2259649
Record
Field
No
condition
(b) Double alternative:
Yes
Module A Module B
Yes
K>S No
Condition
Module
K+ K+T
Q. 12 What are linear arrays ? (Oct. 2006,i3,14; Mar.2015, July 2016, 17, 19, March 18,
Ans. : Adata structure is said to be linear if its elements forma sequence. J22
A linear array is the data structure which consists of finite, ordered set of homogeneous
data elements such that:
1
The elements of the array are referenced respectively by an index set (subscript
Consisting of 'n' consecutive numbers.
2
The elements of the array are stored respectively in successive memory locations.
The number 'n' of the elements is called length or size of array.
In general, the size or length of the array can be obtained from the index set by the
formula:
Length =UB-LB + 1
where UB- the largest index called Upper Bound.
LB - the smallest index called Lower Bound.
e.g. Let DATA be 5 element linear array as follows :
DATA
1 247
2 500
3 600
4 399
499
The element of an array may bedenoted by the subscript notation such that:
DATA (3] = 600
In C++, array is declared as -
int data [100]; which specify an array data of 100 integers.
Q. 13 How arrays are represented in memory ? (Oct. 2014
Ans. :
i) The elements of linear array are stored in consecutive memory locations.
i) Computer does not need to keep track of the address of every element of array. It jus
requires the address of first element of array, LA, denoted by Base (LA) and called te
base address of linear array LA.
iii) Using this base address, the computer calculates address of any element of array y
using the formula.
LOC (LA[K]) = Base (LA) + W (K-LB)
Q. 19 Explain Bubble sort algorithm with suitable example.
(Match. 2002, 05, 08,.12; 17,; 20; Oct.-2005, 20n
Ans. : Algorithm: M 23
Bubble Sort (DATA, N)
Here DATA is a linear array with N elements. This algorithm sorts elements of DATA ;
ascending order.
Step 1: Repeat steps 2 and 3for K:=1 To N-1:
Step 2: Set Ptr:=1
Step 3: Repeat While Ptr <N-K:
(a) IfDATA [Ptr]> DATA (Ptr + 1], then interchange
DATA [Ptr] and DATA (Ptr +1]
[End of If structure]
(b) [increment pointer]
Set ptr := ptr +1
[End of inner loopl
[End of outer loop]
Step 4: Exit
Explanation :
Suppose DATA is an array of N elements. Sorting these elenents in ascending orde
means arranging the elements such that:
DATA [1<= DATA (2] <=...<= DATA .N]
In Bubble sort, compare DATA(1] with DATA[2] and exchange them
DATA[1] > DATA2J.
Next DATA[2] is compare with DATA(3|. They are exchanged if necessary. This proc
is repeated till DATAJN - 1] is compared with DATAN..
One makes N - 1comparisons, this is called a pass.
After the first pass the largest element is sink to the last position. and second large
During the next pass, compare elements upto the last but one
element moves to the (N-1)" position.
After N-1 passes, all elements are sorted.
TPS Computer Science - 1 2-14 Data Structures
At the end of first pass, the largest element 55, has moved to the last position.
Pass 2: In this pass,only three comparisons since K= 2.
(a) (43) (5) 6 55 Since 43 >5.. exchanged
.:. New list is 5 43 6 55
At the end of second pass, the second largest element 43 has maked to its proper
position.
Pass 3:
9 43 55 Since 5<6.. No exchange
5 6 9 43 55 Since 6 <9.:. No exchange
:. New list is 5 6 9 43 55
TPS Conmputer Science -1 2-15
Data Structure
Pass 4:
array gets sorted in
In this way after complete execution of this algorithm, the
order as followS ascending
DATA||] 05
DATA|2] 06
DATA|3] 09
DATA[4] 43
DATA[5] 55
Q. 20 What do you understand by the term searching ? Which are the different types
searching algorithms ? Explain the linear searching algorithm.
HO3 (March 2004, Oct. 2004, 2010, 202
Ans. : Searching :Searching means to find out particular element from a given list of elenent
or check whether required element is present or not in an array. There are two types o
searching algorithmsas follows:
(1) Linear search (2) Binary search
Linear searching algorithm :
In linear search the given element is compared with each element of list one by on
For algorithm, refer to Q. No. 21.
Q. 21 Write an algorithm for linear search technique with suitable example.
(March 2003, 07, 09;Oct. 2007, 10, 21, July 2016, Dec. 201
Ans. :
Algorithm :Linear Search
LINEAR(DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements and ITEM is given element. This algorithm finc
the location LOCof ITEM in DATA or sets LOC = 0, if search is unsuccessful.
Step 1: [Insert ITEM at the end of DATA]
Set DATAN +1] :=ITEM
Step 2: [Initialize counter]
Set LOC:=1
Step 3: [Search for item]
Repeat While DATA (LOC]= ITEM :
Set LOC:=LOC +1
(End of loop]
Step 4: If LOC =N+1, then:
Set LOC:=0
Step 5: Exit
For example: Given DATA array with following 5 elements
11 22 33 44 55
Suppose ITEM =33
TPS Computer Science - I 2-16 Data Structures
Omprakash
iv) II these groups are to be represented in memory, the most efficient way is to use
2arrays. The first is Employee array, which contains list of enmployees in ll four groups
sequentialy, while the second array is Group array, which is a pointer array, which
contains the starting address of each group inthe Employee array, respectively.
TPS Computer Science - I
2-19
Data Structure
It is shown in figure: TI
v)
Employee
Preyas
Group 1
Nitin
3 Swapnil
Group Pointer array
4 Amit
5 Vivek 1 1
Group 2
6 Ravi 3 2
7 Omprakash 8 3
8 Rajdeep 12 4
Amol
Group 3 10 Yogesh
11 Shekhar
p12 Yeshwant
13 Chintamani
Group 4 Kishore
14
15 Rohit
vi) Each element of Group array is apointer, which holds the starting addresses of differe
groups. Hence, it is called as pointer array.
considering suitable
Q. 29 Show representation of records in memory Mar. 2003,11,13; Ot. 2011
records and three fields.
Ans. :
array.
1) Records contain non-homogeneous data, so it cannot be stored in
But in entire file of records, all data elements belonging
to the same identifier will be o
2) collection of arrays.
same type. So a file may be stored in memory as
should be parallel.
3) One array for each of data item. Allthe arrays
4) For e.g records and three fields.
A student file consisting three
Address Phone
Name
11,J.M. Road 5662000
Lokesh
24,M.G. Road 4240020
Jayesh 4261900
Anushka 10, Sahkarnagar
figure shows representation of above file in three parallel arrays Name
Following
Address and Phone -
Address Phone
Name
11, J.M. Road 5662000
Lokesh
24, M.G. Road 4240020
Jayesh
Anushka 10, Sahkarnagar 4261900
Q. 30 What are linked lists ? Show a liked list with suitable example having six nod
with a properly labelled diagram.
(Mar. 2002,04,05,06,07,08,13,14,15; Oct. 2003,07,14, Dec. 2
OR
J22
nodes havi
With suitable example, show labelled diagram for link between two
the information part and next pointer field.
five
What are linked lists ? Show aliked list with suitable example having
with a properly labelled diagram. (Mar. 2013,March20
TPS Conmputer Science - I 2-22 DataStructures
Áns. :
i) A linked list is a linear collection of data elements, called nodes, where the linear order
is maintained with the help of pointers,
ii) Linked list is also called as one-way list.
ii) Each node in the linked list is divided into two parts. First part is called as INFOpart,
which contains the information about the element or actual element and second part is
called as LINK part, which isnext pointers field i.e. it contains the address of next node
in the list.
Start
i)
10
Info Link (Pointer to next node)
10
13B9
13
c14D12
19 14
E
12
18
FX
18
+A|13
10
B 19 C4 D|12 EX
13 19 14 12
5
9
- 9
D X
5 A
8
9 B 4
Above figure shows linked list. It indicates that the node of alist need not occup!
adjacent elements in the array INFO and LINK.
Q. 33 Explain insertion and deletion from linked list with example.
Ans. :
It iseasier to insert an element into or delete an element from a linked list than arrays.
5. Set Ptr:= LINK [Ptr)
End of step 3 loop]
6. Write:N
7. Exit
1. Set Ptr:=START
(iii)
2. Repeat While Pr NULIL:
INFO[Pr]:=1NFO|Pr] +K
Set
Set Ptr :=LINK |Pr]
[End of loopl
3. Exit Stack and Queue
OR J22 (Mar.2013, July2015
with suitable examples.
Queue
Q.37 Explain Stack and Systems with suitable examples.
(Oct2005,201
Explain LIFOand FIFO
Ans.: LIFOSystem :
In this type of system, the element which
system.
(i) LIFO Svstem is last-in-first-outdeleted first.
inserted at last, will be system in which insertion and deleti
LIFO system. It isa linear
(ii) Stack is an example of endiie. top of thelist.
takes place only at one as push and deletion operationstack as pop.
operation is referred to then it is add.
(ii) The insertion we want to add a new dish to this
stack of dishes. If
e.g. consider a takes place from thetop.
at the top of stack also deletion
FIFOSys tem : In this type of system, the element which
first-in-first-out system.
(i) A FIFO system is list will also be deleted first.
inserted first in the in which insertion tak
FiFO systenm. A queue is a linear list,
(ü) Queue is an example of list known as 'rear' of the list and deletion takes place at
place only at one end of the
other end, called as 'front of the list.
cinema hall.
e.g. A queue for tickets ina
Tree
siblings and child about tree.
Q. 38 What isa tree ? What do you mean by root, leaf, (Oct. 2006, 10,2
J22
Ans. : Tree:
set of one or mo
Tree is a non-linear hierarchical data structure which consists of finite
nodes (i.e. collected data items) such that: A
Root :
Anode which has no parent. Generally first node is called as
'root node'. In figure, a the
node A is the root of the tree.
Leaf:
(July 2018)
The node which has no child or children. Such nodes have degree
zero. In figure a D, I,
E.J, Kare the leaf nodes. Alsocalled as terminal node.
Child:
The nodes which are reachable from a node, say u, through a
single edge are called the
children of u. e.g In figure a, the children of node Care F, G, and H.
Sibling : (July 2018)
Children of the same parent are said to be siblings. e.g, The nodes D and E are both
children of node B. SoD and E are siblings.
Q. 39 Explain the following terms : (Marçh 2022)
Ans. : 1. Level of tree:
Each node in a tree is assigned a level number. Generally, the level number of root 'R of
the tree is zero and every other node is assigned to level number which is one more than
the level number of its parent.
It is the distance from the root.
For e-g
Level 0
B Level 1
Level 2
2. Depth/Height : (March 2017
Depth ofatree is defined as maximum level of any node in atree. If root is level 0then
depth or height of tree is equal to 1 + largest level number.
e.g. Depth of above tree is 3.
3. Degree :
The number of subtrees of a node is called degree of a node.
Thedegree of a tree is the maximum degree of the node in tree.
e.g. the degree of each node in figure is as
A
Node Degree
A 2
2
3
H
D, E, E, G, H 0
(a) there is a
single left
subsets are themselves binary tree called
(b) other two original tree.
subtree and right subtree of the
be empty.
A left and right subtree can D
binary tree, there isno node with degree greater than two.
In
e.g
internal node
external node
Q.44 How binary trees are represented in memory ? OR (Mar.2015, 20, July 2016, 19
With suitable example and labelled diagram, show the representation of binary tree
in memory. (March 2003, 2009)
Ans. :
R 5
(March 202
Q. 63 Define Binary tree. Draw a Tree diagram for following expression.
Y= [(a-b-c) + (a +b-c) (Ch. 2/Q. 40and Q. 63/Pg. No. 2-31 and 2-41)
Ans. :
Q. 64 If symmetric binary tree contains 31 nodes then calculate in level of tree 202
(March
depth.
Ans. :
2-1 31