Data Structures Notes-Merged
Data Structures Notes-Merged
Data Structures
Computer Science
12th Standard
Total 17 marks
Total
= 17 MArks
Questions :
▪ (c) What is Data structure ? Explain Linear Data Structure and Non-linear
Data Structure. (Ch. 2/Q. 2/Pg. 2-2)
▪ (b) What is Data Structure ? Define the following terms of data structure:
(i) Record (Ch. 2/Q. 1(vi)/Pg. No. 2-2)
(ii) File (Ch. 2/Q. 1(vii)/Pg. No. 2-2)
(iii) Field (Ch. 2/Q. 1(v)/Pg. No. 2-1)
▪
3
Introduction to Data Structures
● Data
● Data Item
● Group Item
● Elementary Item
● Entity
● Attributes/Properties
● Entity Set
● Information
● Field
● Record
● File
● Primary Key
● Data structure
Data : Data are values or sets of values.
Eg. Student Name and id
Data Item : Data items refers to a single unit of values.
Eg. Student full name (First name,middle name , Last name)
Data Item
7
8
Data Structures:
9
10
Questions :
(b) Explain any six data structure operations. (Ch. 2/Q.3/Pg. 2-2)
(b) Explain any three data structure operations.
(Ch. 2/Q. 3/Pg. No. 2-2)
11
Data StRUCTURE Operations
● Traversing
● Searching
● Inserting
● Deleting
● Sorting
● Merging 12
Algorithmic notation
Algorithm
An algorithm is a finite step-by-step list of well defined instructions
for solving a particular problem.
Step 1 : …………….
Step 2 : …………….
Step n : …………….
13
Algorithmic notation
Example : Q. Write an algorithm for sum of two numbers.
Step 1: Start
Step 3: Sum=A+B
Step 5 : stop 14
Questions :
Q. (a) Explain with flowchart the following control structure:
(i) Sequence logic (ii) Selection logic (iii) Iteration logic (Ch. 2/Q. 10/Pg. 2-7)
(b) Explain Conditional Flow and Repetitive Flow used in data structure with
diagram. (Ch. 2/Q. 7 and 8 OR Q. 10 /Pg. No. 2-5, 2-6 and 2-7)
▪
15
Cntrol Structures
Sequential Flow Conditional Flow Repetitive Flow
(Sequential Logic) (Selection Logic) (Iteration Logic)
16
Flowchart Symbols
17
Sequential Flow (Sequential Logic)
• Algorithms consists of modules.
• Each modules is a set of steps.
• In Sequential flow the modules are executed
one after the other.
Module A
Module B
Module C
18
Conditional Flow (Selection Logic)
• In conditional flow, one or other module is
selected depending on condition.
Conditional Flow
(Selection Logic)
19
Single alternative
IF condition, then :
[Module A]
[End of IF structure]
No
Cinditition
?
Yes
Module A
20
double alternative
IF condition, then :
[Module A]
ELSE :
[MODULE B]
[End of IF structure]
No
Condition
?
Yes
Module A Module B
21
Multiple alternative
IF condition (1), then :
[Module A1]
ELSE IF condition (2), then :
[MODULE A2]
:
:
ELSE IF condition (N), then :
[MODULE AN]
ELSE :
[Module B]
[End of IF structure]
22
Repetitive Flow (ITERATION Logic)
Here certain module is executed repeatedly
until condition satisfies. K=R
For Loop :
Repeat for K=R to S by T No
[Module] Is K<=S
?
[End of loop]
Yes
Module A
K=K+T
23
While Loop :
Repeat While condition :
[Module]
[End of loop]
No
Condition
?
Yes
Module A
24
Questions :
▪ (a) What is an Algorithm ? Write an algorithm to find the roots of a
quadratic equation ax² + bx + c = 0, where a is not equals to 0.
(Ch. 2/Q. 4 & Q. 11/ Pg. 2-3 & 2-8)
▪
25
Problem : Write an algorithm to find roots of quadratic equation ax2+bx+c=0
±
Where a≠0 and roots are given by, X=
Solutions:
Algorithm : This algorithm inputs coefficients a, b and c of quadratic equation and
finds real roots, if any.
Step 1: Start Else :
Step 2 : Read a,b,c Print “No real roots”
Step 3 : Calculate d=b2-4ac Step 5 : Stop
Step 4 : If d>0 then
1. X1= , X2=
2. Print X1 ,X2
Else if d=0 then
1. X1=X2= -b/2a
2 Print “Real and Same roots”
26
Problem : Write an algorithm to print numbers from 1 to 100
Solutions:
Algorithm : This algorithm will print numbers from 1 to 100
28
ARRAYs
• A linear array is a list of finite number n homogeneous data elements.
i.e. the data elements are of same type.
• The elements of the array are referenced respectively by an index set
consisting of n consecutive numbers.
• The elements of array are stored respectively in successive memory
locations.
• Length=UB-LB+1
• UB = Upper bound (largest index)
• LB = Lower bound (smallest index)
• The elements of array A may be denoted by subscript notation
A0, A1, A2, A3,……., An, or A[0],A[1],A[2],…….,A[n]
29
Marks ARRAYs
0 75
1 87 Marks[0]=75
Marks[1]=87
2 91 Marks[2]=91
Marks[3]=92
3 92 Marks[4]=95
Marks[5]=99
4 95
5 99
30
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 elements of array.
• If LA is linear array then address of first elements is denoted by base
(LA) and called base address of LA.
• Using this address, computer calculates the address of any
elements of LA by using the formula-
LOC (LA[k]) = Base (LA) + W (K-LB)
K : Index of array element
LB : Lowest index
31
Representation of linear arrays in memory
Memory Address
1000
1001
Array elements are
stored sequentially
1002
1003
.
.
.
.
.
32
Traversing linear arrays
• Traversing array means accessing each element of the array only once.
• We need to performs this operation several times in a program.
• The algorithm for this operation is described below.
Step 1 : Start
Step 2 : Initialize counter
1. Set K=LB
2. Step 3 : Repeat steps 4 and 5 while K <= UB
Step 4 : Visit element
Apply PROCESS to LA [K]
Step 5 : Increase counter
Set K= K+1
[End of loop]
Step 6 : Stop 33
Inserting And deleting
• Inserting refers to the operation of adding another element to the existing elements.
• Deleting refers to the operation of removing one of the element from existing
elements.
• The element can be inserted at the end of array or in the middle of the array.
• insertion at end can be easily done provided memory space is available .
• But when we want to insert element in the middle of array, then we need to create
space for the element ,by moving down the other element.
• Similarly deleting an element at end of array is easy .
• But when element in the middle of array is deleted, then we have to move other
element upward to fill the gap.
34
Algorithm for inserting into linear array is given below:
Algorithm – Inserting element into liner array.
INSERT (LA, N, K, ITEM)
Here LA is linear array with N element and K is position integer such that K<= N. This
algorithm inserts an element ITEM into the Kth position in LA.
Step 1 : Start
Step 2 : Intialize counter.
Set J=N-1
Step 3 : Reapeat steps 4 and 5 while J>=K.
Step 4 : Move Jth element downward.
Set LA[J+1] = LA [J]
Step 5 : Decrease the counter.
Set J=J-1.
Step 6 : Insert the element
Set LA[K]=ITEM
Step 7 : Reset N.
N=N+1.
Step 8 : Stop 35
Questions :
▪ (b) Explain bubble sort algorithm with suitable example. (Ch. 2/Q. 19/Pg.
2-13)
▪ (b) Explain Bubble sort algorithm with suitable examples. (Ch. 2/ Q. 19/
Pg. No. 2-13)
▪
▪
36
The algorithm for deleting element from array is given below :
Here LA is linear array with N element and K is a positive integer such that K<N . This algorithm
deletes the Kth element from LA.
Using while loop :
Step 1 : Start
Step 2 : Set ITEM =LA [K]
Step 3 :Initialize Counter
J=K
Step 4 : Repeat steps 5 and 6 while J<=N – 2
Step 5 : set LA [J] = LA [J+1]
Step 6 : Increase the counter.
Set J=J+1.
Step 7 : Reset N
N=N-1.
Step 8 : Stop 37
The algorithm for deleting element from array is given below :
Here LA is linear array with N element and K is a positive integer such that K<N . This algorithm
deletes the Kth element from LA.
40
Searching
• Searching refers to the operation of finding the location of given element in the list.
• The search is said to be successful if item is found.
• There are many searching algorithms.
• The most commonly used algorithms are linear search and binary search.
41
Algorithm - Linear search
linear search
LINEAR (DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements, and ITEM is given elements. This algorithm finds the location LOC of
ITEM in DATA
Step 1: Start
Step 2 : Initialize counter.
Set LOC=0
Step 3 : Repeat step 4 and 5 while LOC<N
Step 4 : IF(ITEM==DATA[LOC])
Print : ITEM found at LOC
[break loop]
Step 5 : Set LOC = LOC + 1
[ End of loop]
Step 6 : IF LOC = N, then :
Print : ITEM not found
42
Step 5 : Stop
Algorithm – Binary search
Binary search
Binary (DATA, LB,UB,ITEM)
Here DATA is sorted array with lower bound LB and upper bound UB. ITEM is given element. BEG denote beginning,
MID demotes middle and END denotes end locations of segment of DATA. This algorithm finds the location of ITEM in
DATA
Step 1: Start ELSE :
Step 2 : Set BEG=LB, Set BEG=MID+1
END=UB [End of If structure]
[End of loop]
Step 3 : Repeat steps 4 and 5 while BEG<=END
Step 6 : IF Data[MID]!= ITEM then :
Step 4 : Set MID=INT((BEG+END)/2)
Print : Item not found
Step 5 :IF ITEM==DATA[MID] then :
Step 7: Stop
Print: Item found at MID
[Break Loop and go to step 7 ]
ELSE IF ITEM <DATA[MID] Then :
Set END=MID-1
43
Pointer arrays
• A variable is called pointer if it points to an element in list.
• The pointer variable contain the address of an element in the list.
• An array is called pointer array if each element of array is a pointer.
• The use of pointer array can be Discussed as below :
• Suppose we have four groups 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 another pointer array group
containing the starting location of different groups.
• It is shown in figure
44
Pointer arrays
45
Pointer arrays
46
Questions :
▪ (a) What is Record ? Explain how records are represented in
memory using array ? (Ch. 2/Q. 26 & Q. 28/ Pg. 2-19 & 2-21)
▪ (a) Write four difference point between Record and Linear Array.
(Ch. 2/Q. 27/Pg. No. 2-20)
▪
47
records
• Records is a collection of field.
• A record may contain non homogenous data. i.e. data items in a record may have different data
types.
• In records, the natural ordering of element is not possible.
• The elements in records can be described by level numbers.
• For example, a hospital keeps a record of new born baby.
• It contains following data items : Name, gender, birthday, father, mother.
• Birthday is a group item consisting of month, day and year.
• Father and mother are also group item with subitems name and age.
• The structure of this record is shown below :
48
records
1. New born
2. Name
2. Gender
2. Birthday
3. Month
3. Day
3. Year
2. Father
3. Name
3. Age
2. Mother
3. Name
3. Age
49
The number to left of each variable is called a level number.
Each group item is followed by its subitem. The level of subitem is 1 more than the level of group item.
To indicate number of records in a file we may write
1 Newborn ( 20 ).
If there are 20 records and we want to refer to gender of sixth newborn then it can be done by writing
Newborn.gender[6].
50
Representation of records in memory
• Records contains non homogenous data.
• To store record in memory we may use linear arrays.
• For above example of new-born record, we requires nine linear arrays such as Name, gender,
month, day, year, father name, father age, mother name, mother age.
• One array for each element of data item.
• All the arrays should be parallel that is for subscript K the elements Name[K], gender[K], ……..,
mother age[K]
• Must belong to same record.
51
Questions :
▪ How linked list are represented in memory ? ( ch.2 / Q.32 / Pg.2-23)
▪ ( c ) Write two features of each of data structure:
(i) Record (Ch. 2/Q. 26/Pg. No. 2-19)
(ii) Array (Ch. 2/Q. 12/Pg. No. 2-9)
(iii) linked list
▪ (a) Define linked list. Draw and explain labelled diagram of linked
list with 5 nodes. (Ch. 2/Q. 30/Pg. No. 2-21)
52
Linked Lists
• A linked list is a linear collection of data elements, called nodes.
• Where linear order is given by means of pointers.
• Each node is divided into two parts.
• The first part contains the information of the element and second part is link field which
contains the address of the next node in the list.
• The left part represents the information part of the node.
• The right pointer is next pointer field of node.
• The arrow is drawn from this field to next node.
• The pointer field of last node contains null pointer.
• The list pointer variable is start or name.
• This contains address of first node in list.
• We need only this address to trace the list.
53
Linked Lists - Node
54
Linked Lists
55
Array Vs Linked List
Array Linked List
Arrays can not be easily extended. Linked list can be easily extended.
Insertion and deletion of array element is One can easily insert and delete array
expensive. element.
56
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.
• Start points to 5th element.
• INFO[5] = A and LINK[5] = 2.
• So the next node is 2.
• INFO[2] = B and LINK[2] = 9.
• The next node is 9.
• INFO[9] = C and LINK[9] = 10.
• Where INFO[10] = 0 i.e. null.
• The list is ended here.
57
Representaion of linked list in memory
58
Questions :
▪ (a) Define the following terms with reference to tree:
(i) Root (Ch. 2/Q. 38/Pg. 2-30)
(ii) Leaf (Ch. 2/Q. 38/Pg. 2-30)
(iii) Sibling (Ch. 2/Q. 38/Pg. 2-30)
(iv) Depth (Ch. 2/Q. 39(2)/Pg. 2-30)
▪ (a) Define the following terms with respect to data structures :
(i) Array (Ch * .2 / Q * .12 / P g * .2 - 9)
(ii) Stack (Ch * .2 / Q * .37 / P g * .2 - 29)
(iii) Queue (Ch * .2 / Q * .37 / P g * .2 - 29)
▪ (c) Draw a binary tree structure for the following expression : F = (a
- b) / ((P* m) + h) (Ch. 2/Q. 60/Pg. 2-40)
▪ ( c ) What is Binary Tree? With suitable example show the
relationship between Total Number Nodes and Depth of Binary
Tree. (Ch. 2/Q. 42/Pg. 2-32) 59
Questions :
▪ (b) Explain following terms used in Binary Tree:
(i) Sibling (Ch. 2/Q. 38/Pg. No. 2-29, 2-30)
(ii) Successor (Ch. 2/Q. 41/Pg. No. 2-31)
(iii) Leaf (Ch. 2/Q. 38/Pg. No. 2-29, 2-30)
▪ (b) What is Binary Tree? Draw the Tree diagram for the expression.
B = (3R/5T)²-(R+Q³)
(Ch. 2/Q. 40 and Q. 62/Pg. No. 2-31 and 2-41)
▪ (a) Explain the memory representation of Binary Tree with suitable example.
(Ch. 2/Q. 44/Pg. No. 2-32)
▪ (b) Explain linked representation of binary tree in memory with suitable
example. (Ch. 2/Q. 44/Pg. No. 2-32)
60
Questions :
▪ (b) Define Binary tree. Draw a Tree diagram for following expression.
Y= [(a-b-c) + (a+b-c)]³/2
(Ch. 2/Q. 40 and Q. 63/Pg. No. 2-31 and 2-41)
▪
61
OPT
3. Exit
Tree
Q. 38 What is a tree ? What do
you mean by root, leaf, siblings and child
about tree.
Ans.: Tree: Oct. 2006, 2010
Tree is a non-linear
hierarchical data structure which consists of
nodes (i.e. collected data finite set of one
items) such that: or more
a) There is specially
designated node called the root. (A)
b) The remaining nodes are
partitioned into n2 0 finite disjoint sets B
T1, T2,..., Tn where each of these set is tree.
T1, T2, Tn are called 'subtrees' of the
..,
D) F G) Level 2
2 Depth/Height: (March2017
Depth of a tree is defined as maximum level of any node in a tree. If root is level 0 then
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.
The degree of a tree is the maximum degree of the node in tree.
e.g. the degree of each node in figure is as
Node Degree
A 2
B C B 2
C
D, E, E G, H 0
= 7 depth
4 The tree with depth n having 2"-1 number of total nodes.
Q. 44 How binary trees are represented in memory ? OR
With suitable
(Mar.2015, 20, July 2016,19
example and labelled diagram, show the representation of binary tree
in memory.
Ans. (March2003, 2009
A binary tree T can be represented in memory by two types of
(i) Linked representation
representation:
(ii) Sequential representation.
(i) Linked representation:
Linked
(Oct. 2008,15)
representation three parallel arrays INFO and, LEFT and RIGHT and a
uses
pointer variable ROOT such that for an index K, INFO [K] contains actual
LEFT [K] contains address of left child and RIGHT element
[K] contains address of right child.
e.g. Consider a binary tree as below
The above tree can be represented in memory as,
INFO LEFT RIGHT
1 B 14
2 G 12 13
3 0
ROOT 4 E 11 0
5 A 9
6 3
AVAIL 7 D 0 0
10 8 6
9 C 14 2
10
11 H 0 0
12 0 0
13 0
14 F
The ROOT stores address of first node of tree T.
another node to tree T, it is inserted at
AVAIL stores address of first null node. To insert
memory location pointed by AVAIL
then it will be inserted at INFO [10]. After
Note In above example, to insert an element K,
will contain and AVAIL will
(null pointer)
insertion, LEFT [10] and RIGHT [10]
zero
inserted at 8.
contain 8 i.e. next element is to be
(ii) Sequential representation:
linear array is used. This array is generally
sequential representation, only
one
For
known as TREE such that:
Ans.
0.62 What is Binary Tree ? Draw the Tree diagram for the
expression.
B (3R/5T)? - (R+ Q) (Ch. 2/Q. 40/Pg. No. 2-31)
Ans.
March 2019
3
Q.63 Define Binary tree. Draw a Tree diagram for following expression.
Y= [(a
March 2020
-b - c) + (a +b-c)]32 (Ch. 2/Q. 40 and Q. 63/Pg. No. 2-31 and 2-41)
Ans.
2. Data items that are divided into subitems are called as -
(March 2018
) Group items (ii)Elementary items
(ii) Nodes iv) Arrays
Ans.: (i) Group items
3. In LINKED LIST, Link field contains-
i) Value of next node (ii) Address of next node
(ii) Value of previous node (iv) None of these
Ans.: (i) Address of next node