0% found this document useful (0 votes)
31 views

Chapter2 DataStructure Notes

Uploaded by

sagarawal66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Chapter2 DataStructure Notes

Uploaded by

sagarawal66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter 2

Introduction to Data Structures


Q.1) Define the following terms:
i. Data:
Data are simply values or sets of values.

ii. Data Item:


A data item refers to a single unit of values.

iii. Group items:


Data items that are divided into subitems are called group items.
For example, name can be divided into three subitems-first name, middle
name and last name. So name is a group item.
Date may be divided into – day, month and year. So date becomes group
item.
iv. Elementary items:
Data items which are not divided into subitems are called elementary items.
Ex: The pincode number cannot be divided into subitems. So it is
elementary item.

v. Entity:
An entity is something that has certain attributes or properties which may
be assigned values. The values can be numeric or non-numeric.
For example, employee of an organisation is entity. The attributes
associated with this entity are employee number name, age, sex, address
etc. Every attribute has certain value. All the employees in an organisation
forms entity set.
vi. Field

Field is single elementary unit of information representing an attribute of


entity.

For example, employee number, name, age, sex are all fields.
vii. Record

A record is a collection of field values of a given entity. i.e. we can have one
record for one employee.

Field
s

Record

viii. File

A file is a collection of records of the entities. i.e. collection of all records of


all employees forms a file.

ix. Primary Key or Key field

A field having unique value is called primary key or key field. For example
employee number can be a primary key.

Q.2) What is Data Structure? Explain linear data structure and non-linear
data structure. (2018)

A Data Structure is a logical way of organization of data that considers not


only the items stored, but also their relationship to each other.

The logical or mathematical model of a particular organisation is called a


Data Structure. The model should reflect the actual relationships of data in
real world and it should be simple enough so that data can be processed
whenever required.

There are two types of data structures.

a) Linear data structure

b) Non-linear data structure


a) Linear Data Structure

Data is stored in memory locations in some sequential order or by


means of pointers. The elements stored in memory locations of a sequential
order are called Arrays. The elements stored in memory locations by
means of pointer are called Linked Lists. E.g. Array, Linked Lists, Stacks
and Queues.

b) Non-Linear Data Structure

Non-Linear Data Structure is one that represents the hierarchical


relationship between individual data items. E.g. Trees and Graphs.

Q.3) Explain in brief any six data structure operations. (2015)

Following are operations that can be performed on a data structure.

Traversing: Traversing means accessing each record (element) only once


so that it can be processed.

Searching: Searching means finding the location of record (element) with


given key value or finding all records that satisfies condition.

Inserting: Inserting means adding new record (element) to the structure.

Deleting: Deleting means removing the record (element) from the structure.

Sorting: Sorting means arranging records (elements) in some logical order.


For example, arranging names in alphabetical order or arranging numbers
in ascending order.

Merging: Merging means combining the records in two different files into a
single file.

(2024)
Define data structure. Explain any four operations of Data Stucture.
Q.4) List any six data structure operations. (2020)

Six data structure operations are:

1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging

Q5.) Explain the following control structures and their used in data
structure

i) Selection ii) Loop (2014)


OR

Explain with flowchart the following data structures: (2017)

i) Sequential logic ii) Selection logic iii) Iteration logic

Answer: Refer Textbook page no. 35 to 37

Q6.) What is Array? How are they represented in memory with example.
A linear array is a list of finite number of n homogeneous data elements.
i.e. the data elements are of same type.
The elements of array are referenced respectively by an index set consisting
of n consecutive numbers. The elements of array are stored respectively in
successive memory locations.
The number n of elements is called the length or size of the array. In general,
the length or number of elements in array can be obtained by using formula
Length = UB - LB + 1
Representation of linear arrays in memory
The elements of linear array are stored in successive memory cells. The
number of memory cells required depends on type of data elements.
Computer keep track of address of the first element of array. If LA is linear
array then address of first element is denoted by Base (LA) and called base
address of LA. Using this base address computer calculates the address any
element of LA by using the formula –
LOC (LA [K]) = Base (LA) + W (K – LB)
where W = number of words per memory cell for LA.

Memory
Address
1000 Array elements are stored
sequentially
1001
1002
:
:
:
Memory representation of array

Q7.) What is array? Write an algorithm for traversing linear array. (2018)
A linear array is a list of finite number of n homogeneous data
elements. i.e. the data elements are of same type.
Traversing array means accessing each element of the array only once.
We need to perform this operation several times in a program.
The algorithm for traversing operation is described below.
Algorithm - Traversing a linear array,
Suppose LA is linear array with lower bound LB and upper bond
UB.
1. Initialize counter
Set K = LB
2. Repeat steps 3 and 4 while K <= UB
3. Visit element LA [K]
4. Increase counter
Set K = K+1
[End of step 2 loop]
5. EXIT.
Q8.) State algorithm for inserting an element in an array. (2016, 2024)
Inserting refers to the operation of adding another element to the existing
elements.
Algorithm for inserting into linear array is given below:
Algorithm - Inserting element into linear array.
INSERT (LA, N, K, ITEM)
Here LA is linear array with N elements and K is positive integer
such that K<= N. This algorithm inserts an element ITEM into the
Kth position in LA.
1. Initialize counter
Set J = N – 1.
2. Repeat steps 3 and 4 while J >= K
3. Move Jth element downwards
Set LA [J+1] = LA [J]
4. Decrease the counter
Set J = J – 1.
[End of Loop]
5. Insert the element
Set LA [K] = ITEM.
6. Reset N
N=N+1
7. EXIT.
Q9.) State algorithm for deleting an element in an array.
Deleting refers to the operation of removing one of the elements from existing
elements.
Algorithm for deleting element from linear array is given below:
Algorithm - Deleting element from linear array
DELETE (LA, N, K, ITEM)
Here LA is linear array with N elements and K is a positive integer
such that K <= N. This algorithm deletes the Kth element from
LA.
1. Set ITEM = LA [K]
2. Repeat for J = K to N – 1
Move (J + 1)st element upward
Set LA [J] = LA [J+1]
[End of loop]
3. Reset N
N = N - 1.
4. EXIT
Q10.) State algorithm to find smallest element in an array. (2015)
Algorithm to find smallest element in an array is given below:
Algorithm – Find smallest element in a linear array
Smallest (LA, N, Small)
Here LA is linear array with N elements. This algorithm finds
smallest element in array LA and stores in variable Small.
OR
1. Initialize Small = LA [0]
1. Initialize counter
2. Repeat step 3 for J = 1 to N – 1 Set J = 1 and Small = LA[0]
3. If Small > LA [J] then: 2. Repeat steps 3 and 4 while J <= N-1
Small = LA [J] 3. IF LA[J] < Small THEN:
[End of IF Structure] Set Small = LA [J]
[End of loop] [End of IF structure]

3. Print Small 4. Increase the counter

4. EXIT Set J = J + 1.
5. Print Small
6. EXIT.
Q11.) Explain Bubble sort algorithm with suitable example. (2020, 2017,
2020, 2023)
Ans:
Algorithm - Bubble sort.
Sort (DATA, N)
Here DATA is a linear array with N elements. This algorithm
sorts elements of DATA in ascending order.
1. Repeat steps 2 and 3 for K=1 to N-1
2. Set J = 0
3. Repeat while J <= N - K:
(a) If DATA [J] > DATA [J+1] then:
Interchange DATA [J] and DATA [J+1]
[End of IF structure]
(b) Set J = J +1
[End of Step 3 inner loop]
[End of step 1 outer loop]
4. EXIT
Example:
Consider a linear array A, consisting of 5 elements

A[0] A[1] A[2] A[3] A[4]


8 4 19 2 7

Step 1:
i) Compare A[0] and A[1] i.e. 8 and 4 and arrange them in desired order,
so that A[0] < A[1].
New list is 4 8 19 2 7
ii) Compare A[1] and A[2] i.e. 8 and 19 and arrange them so that
A[1] < A[2]. No exchange.
New list is 4 8 19 2 7
iii) Now compare A[2] and A[3] i.e. 19 and 2 since 19 > 2 interchange
New list is 4 8 2 19 7
Continue this process until we compare A[n-2] with A[n-1], so that
A[N-2] < A[N-1].
iv) Compare A[3] and A[4] Since 19 > 7 interchange
New list is 4 8 2 7 19
Step 1 involves n-1 comparisons. During this step, the largest element
is coming like a bubble to the nth position. When step 1 is completed,
A[N-1] will be the largest element.

Each step is called pass.


Step 2/ Pass 2: Repeat step1 with one less comparison i.e. here 3
comparisons. Step 2 involves N-2 comparisons. When step 2 is
completed we get second largest element at A[N-2].

i) 4 8 2 7 19 Since 4 < 8 no interchange

ii) 4 8 2 7 19 Since 8 > 2 interchange

New list is 4 2 8 7 19

iii) 4 2 8 7 19 Since 8 > 7 interchange

New list is 4 2 7 8 19

At the end of second pass, the second largest element 8 has moved to

A[N - 2]th position i.e. here A[3]

Pass 3: Repeat step1 with two less comparisons. Pass 3 involves N-3

comparisons. i.e. here 2 comparisons

i) 4 2 7 8 19 Since 4 > 2 interchange

New list is 2 4 7 8 19

ii) 2 4 7 8 19 since 4 < 7 no interchange

New list is 2 4 7 8 19
Pass 4: Compare A[0] and A[1] and arrange them so that A[0] < A[1]

2 4 7 8 19 since 2 < 4 no interchange. The list is sorted


after 4 passes.

After n-1 steps or passes, the list will be sorted in increasing order.
So, bubble sort has n-1 passes.

Q12.) Write an algorithm for linear search.

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 finds the location LOC of ITEM in DATA
or sets LOC=0 if search is unsuccessful.
1. Initialize counter.
Set LOC = 1
2. Search for item
Repeat while DATA [LOC] ≠ ITEM:
Set LOC = LOC +1
[End of loop]
3. If LOC = N+1, then set LOC = 0
4. EXIT.

Q13.) What is Searching? Explain binary search algorithm with example.


(2023)
Explain Binary Search algorithm with a suitable example. (2019, 2015,
2014, 2013, 2012)
OR
Explain binary search algorithm. (2022)
Ans:
Algorithm - Binary search
Binary (DATA, LB, UB, ITEM, LOC)
Here DATA is sorted array with lower bound LB and upper bound
UB. ITEM is given element. BEG denotes beginning, MID denotes
middle and END denotes end locations of segment of DATA. This
algorithm finds the location LOC of ITEM in DATA or set LOC =
NULL.
1. Set BEG = LB, END = UB and MID = INT ((BEG +
END)/2)
2. Repeat steps 3 and 4 while BEG<= END and DATA
[MID] ≠ ITEM.
3. IF ITEM < DATA [MID], then:
Set END = MID - 1.
ELSE:
Set BEG=MID + 1
[End of IF structure]
4. Set MID = INT ((BEG + END) / 2)
[End of step 2 loop]
5. If DATA [MID] = ITEM, then:
Set LOC = MID
ELSE:
Set LOC = NULL
[End of IF structure]
6. EXIT
Example:
Let DATA be the linear array sorted in ascending order with 10 elements.
21 28 34 45 55 67 72 80 83 90
Suppose ITEM = 72 be the element to searched.
Step 1: Beg = 0 and End = 9
Hence Mid = Int [(Beg + End)/2]
Mid = Int [(0+9)/2] = 4
So, DATA [Mid] = DATA [4] = 55
Step 2: Since ITEM i.e. 72 > 55, Beg has to be changed
Beg = Mid + 1 = 4 + 1 = 5
Set Mid = Int [(5 + 9)/2] = 7
DATA [Mid] = DATA [7] = 80
Step 3: Since 72 < 80, End has to be changed
End = Mid – 1 = 7 – 1 = 6
So, DATA [Mid] = DATA [6] = 72
Therefore, ITEM found at location Mid i.e. 6
Set LOC = Mid = 6

Q14.) Write difference between Linear Search and Binary Search. (2017)
Linear Search Binary Search
In linear search the given element is In binary search the given element is
compared with each element of list compared with middle element, after
one by one. finding the midpoint of the array. If the
value is less than midpoint value, the
first half is checked, otherwise second
half is checked until search is
successful.
Linear search can be performed on For binary search, array has to be
unsorted as well as sorted list. sorted in ascending order.
For large size array, time required for For large size array, time required is
this search is very large. comparatively less.
In worst case, the running time of The number of comparisons required
algorithm is proportional to n. here are approximately equal to log2n
which is less than that of linear search.

Q15.) Explain Pointer Array with example. (2019)


Ans:

An array is called pointer array if each element of array is a pointer.

The pointer variable contains the address of an element in the list.

The use pointer array can be discussed as below:


Suppose we have four groups. Each group consist of list of members.
The membership list is to be stored in memory. The most efficient
method is to form two arrays. One is member consisting of list of all
members one after the other and another pointer array group containing
the starting locations of different groups.

Group Member

The elements of pointer array are starting location addresses of each


groups.

Q16.) What is a Record? Write any two distinguishing points between a


Record and Linear Array. (2022)
OR
What is record? How it differs from an array? (2014)
Ans.
A record is a collection of related fields or attributes.
Difference between records and linear array.
Record Linear array
A record is a collection of fields An array is list of finite number of n
homogeneous data elements
A record may contain non- An array always contain
homogeneous data i.e. data elements homogeneous data.
may be of different data types.
In a record, natural ordering of Array elements can be naturally
elements is not possible. ordered
Elements of record are referenced by Elements of array can be referenced
level number by an index set consisting of n
consecutive numbers

Q17.) What is a record? How it is represented in memory? (2016, 2018)


Ans.
A record is a collection of related fields or attributes. A record may contain
non homogeneous data i.e. data items in a record may have different data
types. In records, the natural ordering of element is not possible. A file is
a collection of records.

Representation of records in memory


Consider a record, whose structure is given below.
1. Employee
2. Name
3. FirstName
3. LastName
2. Gender
2. Address
3. City
3. Pincode
2. Phone no
To store record in memory linear arrays are used.
One array for each element of data item.
All the arrays should be parallel that is for subscript K the elements
FirstName [K], LastName [K], Gender [K],………
must belong to same record in a file.
First Name Last Name Gender City Pincode Phone no


Parallel arrays for records

Q18.) What are Linked lists? How linked list are represented in memory? (2014, 2015,
2017)
Ans:
A linked list is a linear collection of data elements, called nodes pointing to
next nodes by means of pointers.

ii) Each node is divided into two parts:

-the first part contains the information of the element

-and the second part called the link or next pointer containing the
address of the next node in the list.

iii) The pointer START is a special pointer, which stores address of first
node in the list.

iv) The left part represents the information part of the node.

v) The right part is the next pointer field of node.

vi) The next pointer of last node stores NULL value, which means this node
is not pointing to any other i.e. it is the last node in the list.

vii) When list do not have any node then it is called null list or empty list
and START variable contains Null value.

Representation of linked list in memory


Linked list can be represented by two linear arrays.
One is INFO, containing information part and other is LINK, containing next
pointer field.
The name of linked list, start contains beginning of the list.

In the diagram given, Start points to 5th element.


INFO [5] = A and LINK [5] = 9.
So, the next node is 9.
INFO [9] = B and LINK [9] = 4.
The next node is 4.
INFO [4] = C and LINK [4] = 1.
The next node is 1
INFO [1] = D and LINK [1] = 7.
Where INFO [7] = 0 i.e. null.
The list is ended here.

Q19.) Explain memory representation of linked list with a suitable


example. (2022)

How is linked list represented in memory? (2017)


Q20.) Define Linked List. Draw and explain labelled diagram of linked list
with 5 nodes. (2020, 2013)

Ans:

A linked list is a linear collection of data elements, called nodes pointing to


next nodes by means of pointers.

ii) Each node is divided into two parts:

-the first part contains the information of the element

-and the second part called the link or next pointer containing the
address of the next node in the list.

a) The above figure shows a linked list having five nodes.


b) The left part of the node is Info part, which contains information of the
element, while the right part is Link part, which is next pointer field of
node i.e. it points to next node.
c) An arrow is drawn from Link part of one node to the next node to indicate
link.
d) The address of the list is stored in Start or Name of the list.
e) The Link part of last node is NULL pointer i.e. it contains nothing.
f) To trace the linked list, we just require the address of Start or Name.
Q21.) What are the advantages of linked lists over linear arrays?
Or
Write three distinct differences between array and linked list. (2024)
ARRAY LINKED LIST
Insertion and deletion of array Insertion and deletion are easy.
element is complicated.
Array elements also requires Do not require consecutive memory
consecutive memory locations locations
Number of elements need to be In linked list no. of elements need to
predetermined be predetermined, more memory can
be allocated or released during the
processing as and when required.
Arrays cannot be easily extended Linked list can be easily extended.

Q22.) Define Tree, Binary Tree, Extended Binary Tree. (2015)

Ans:

Tree

Tree is a non-linear data structure used to represent data containing a


hierarchical relationship between its elements.

Binary tree.

A binary tree is a finite set of nodes that is, either empty or consists of a
root and two disjoint binary trees called left and right subtrees.

If every node in a tree can have at the most 2 children or degree 2, the
tree is called a binary tree.
2 TREE or EXTENDED BINARY TREE.

A binary tree is said to be 2 tree or an extended binary tree if each node


has either 0 or 2 children. In such a case, the nodes with 2 children are
called internal nodes and the nodes with 0 children are called external
nodes.

N N LN N LN

Q23.) What is Binary Tree? With suitable example show the relationship
between total number of nodes and depth of Binary tree. (2018)

Ans:

Binary tree.

A binary tree is a finite set of nodes that is, either empty or consists of a
root and two disjoint binary trees called left and right subtrees.

If every node in a tree can have at the most 2 children, the tree is called a
binary tree.

Relationship between total number of nodes and depth of Binary tree.

The depth (or height) of a tree is the maximum number of nodes in a


branch of tree. This is one more than the largest level number of tree.

Maximum number of nodes of binary tree with depth n are 2n – 1

For example:

Consider the following tree with depth 3.


So, with depth 3, the total number of nodes in a given tree is

2n – 1 = 2 3 – 1

= 8–1

= 7

The tree with depth n having 2n – 1 number of total nodes.

Q.) Define the following terms with reference to Tree:

i) Root ii) Leaf iii) Sibling iv) Depth (2017)

OR

Define the following terms with respect to binary tree: (2022)

i) Depth of tree ii) Degree of node iii) Empty or Null tree


Definitions

Basic structure of tree

In a tree each item is called node or leaf. Each item contains data and pointer,
which contain address of the node. The first node in the tree is called root. Each
piece under node is called subtree. A node under which the subtree exists is
called parent node. A node that has no subtree is called terminal node.

Level of root = 0

Level of any node = Level of parent + 1

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 a level number
which is one more than the level number of its parent. It is the distance from the
root.

Define:

ROOT. A

A node which has no parent is called root node. The first node from where the
tree originates is called as a root node. Eg. In figure Node A is the root of the
tree.

LEAF.
The node which has no successors or child or children is called Leaf or
terminal node. E.g. In figure, D, F, G, K, J are leaf nodes.

SIBLING.

Nodes or children of the same parent node are said to be siblings. Eg. The
nodes D and E are children of node B. So, D and E are siblings.

DEPTH.

The depth (or height) of a tree T is the maximum number of nodes in a branch
of T. This is one more than the largest level number of T.

For ex. The tree above has depth 5. (largest level = 4; depth=4+1=5)

The maximum no. of nodes in a symmetric binary tree with depth n is 2n – 1.

EDGE and PATH.

The line drawn from a node N to a successor is called an edge. In a tree with
n number of nodes, there are exactly (n-1) number of edges.

Sequence of consecutive edges is called a path.

BRANCH.

A terminal node is leaf and path ending in a leaf is called a branch.

E.g. A-B-E-F is a branch, A-C-G is a branch.

DEGREE

Degree of a node is the total number of children of that node. In a binary tree,
all nodes have degree 0, 1, or 2. E.g. Degree of node B is 2 and degree of node
E is 1.

Degree of a tree is the highest degree of a node among all the nodes in the
tree. Degree of Binary tree is 2.

INTERNAL NODE

The node which has at least one child is called as an internal node.

Internal nodes are also called as non-terminal nodes.


EMPTY or NULL Tree

A tree with no nodes is called an empty or null tree.

COMPLETE TREE.

The tree is said to be complete if all its levels, except the last, have maximum
no. of possible node.

Q24.) Define Binary Tree. Draw a Tree diagram for following expression:
Ɣ = [ (a – b – c ) + ( a + b – c ) ]3/2
A binary tree is a finite set of nodes that is, either empty or consists of a
root and two disjoint binary trees called left and right subtrees.

If every node in a tree can have at the most 2 children, the tree is called a
binary tree.
Q.) Draw a binary tree structure for expression:
E=(p–q)/[(r*s)+t] (2022)

[Note: For extra tree diagrams of algebraic expressions refer PPT’s PDF.]

Q26.) Explain Linked representation of Binary Tree in memory with


suitable example?

Ans:
Let T be a binary tree.
T can be represented in memory either by
i) linked representation or
ii) sequential representation by using array

The requirement in any representation is that one should have direct


access to root R given any node N of T.

The linked representation uses three parallel arrays,


INFO, LEFT and RIGHT way and a pointer variable ROOT.
Each node N of T will correspond to a location K such that:
1. INFO [K] contains the data at node N.
2. LEFT [K] contains the location of left child of node N.
3. RIGHT [K] contains the location of right child of node N.
Root will contain location of root R of T.
Consider a binary tree given below

The given binary tree can be represented using three parallel arrays as
given below:
The Root stores address of first node of tree T.
In this ex. Root contains address 3.
INFO [3] = contains data i.e. A
LEFT [3] contain address of left node of A - here LEFT [3] = 4 and
RIGHT [3] contain address of right node of A – here RIGHT [3] = 7
So, INFO [4] = B and INFO [7] = C.
LEFT [4] = 2 and LEFT [7] = 0
RIGHT [4] = 6 RIGHT [7] = 5
Like this all the nodes can be obtained by using K of INFO, LEFT and
RIGHT.

Q27.) Explain Sequential representation of Binary Tree in memory with


suitable example?

Ans:
For sequential representation of Binary Tree in memory, a linear array is
used.
The Root R is stored TREE [1].
If a node n occupies TREE [K], then its left child is stored in TREE [ 2*K ]
and right child in TREE [ 2 * K+1]
Consider a binary tree given below
The given binary tree can be represented using one linear array as given
below:

The root R is stored in TREE[1] i.e. A


The left child of A will be at TREE [ 2 * K ] i.e TREE [2*1] = B
And right child in TREE [ 2*K+1 ] i.e. TREE [2*1+1] -> TREE[3] = C
Like this respective left nodes can be found at TREE[2*K] and right nodes
at TREE [2*K+1].

Q28.) Explain Stack and Queue.


Ans:
Stack
➢ A stack is a linear data structure in which items may be added or
removed only at one end.
➢ The common examples of stack are stack of dishes, a stack of books
etc.
➢ An item can be removed or added only from the top of the stack.
➢ “Push" is the term used to insert an element into a stack.
➢ "Pop" is the term used to delete an element from a stack.
➢ A stack is also called as Last-In-First-Out (LIFO) list.
Queue
➢ A queue is a linear list in which items may be added only at one end
and items may be removed only at other end.
➢ The common example of queue is the queue waiting for a bus at bus
stop.
➢ The queue is also called as First-In-First-Out (FIFO) list.
Another example of queue is a batch of jobs waiting to be processed

Q2.) Write two features of each of data structures: (2019)


i) Record
ii) Array
iii) Linked List

Q.) Define: i) File ii) Record iii) Key-field (2016)

Q.) Define following terms:


i) Group Item ii) Elementary Item iii) Entity (2023, 2018)
Q.) The maximum number of nodes in a binary tree of depth 6 is ______.
(2022)
i) 64
ii) 32
iii) 63
iv) 6

*********************

You might also like