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

Data Structure: Computer Science (D9) : XII - Paper - I

Uploaded by

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

Data Structure: Computer Science (D9) : XII - Paper - I

Uploaded by

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

Computer Science ( D9) : XII - Paper –I

DATA STRUCTURE
Scope of the Syllabus:
• Introduction to data structure
• Data structure operations
• Algorithmic notations
• Control structure
• Arrays-representation in memory, Traversing, Deleting Sorting, Binary Search in an array,
Pointer arrays, Records in memory using array.
• Linked List — Representation in memory
• Trees, Binary trees-Representation binary tree in memory

Q1. Define the following term of Data structure: i) Field ii) Record iii) File. Or i)
Group Item ii) Elementary Item iii) EntityMar18(3)
(1) Group items: Data items which are divided into sub-items are called as group items. e.g.
Age can be divided into days, month, years etc.
(2) Elementary item: Data items which are not divided into sub-items are called as
elementary items. Mobile no. 9800000121 cannot be divided into sub items. So it is
elementary item.
(3) Entity: An entity is something that has certain attributes/properties which may be
assigned a value.
(4) Field: Field is a single elementary unit of information representing an attribute of an
entity.
(6) Record: Record is a collection of field values of a given entity.
(7) File: File is the collection of records of the entities in a given entity set.
(8) Key Field: The field in the record which uniquely identifies that record within the record-
set is called Key Field or primary key.

Define: Data may be organized in many different ways; the logical or mathematical model of
a particular organization of data is called a data structure.

Data Structures and its operations


Data Structures Operations
1) Arrays 1. Traversing
2. Inserting
3. Deleting
4. Sorting *(Bubble Sort)
5. Searching (Linear )
6. Searching ( Binary)
2) Linked Lists Introduction
3) Trees Introduction
4) Queues Definition
5) Stacks Definition

P a g e | 24
Computer Science ( D9) : XII - Paper –I

Q3. List any six Data Structure operations. [Mar 02, 06, 12, 20, Oct 02, 04, 06, 12, 15]
Define: Data Structure operations: The Following five operations play a major role:
1. Traversing: Accessing each record once so that certain items in the record may be
processed. (This accessing and processing is sometimes called “Visiting” the record.)
2. Inserting: Adding a new record to the structure.
3. Deleting: Removing a record from the structure.
4. Sorting: Arranging the records in some logical order (Ascending & Descending).
5. Searching: Finding the location of the record with a given key value, or finding the
locations of all records which satisfy one or more conditions.
6. Merging: Merging means combining the records of two different sorted files into
single sorted file.
Q4. What is an Algorithm? Jul 17
Define:An algorithm is a finite step-by-step list of well-defined instructions for solving a
particular problem.
Notations (Rules) used in Algorithms:
1) Identifying Number: Each Algorithm is assigned an identifying numbers as follows:
Algorithm 4.3, Algorithm 1.1 etc.
2) Steps and Exit: The steps of the Algorithm are executed one after the other, beginning
with Step 1, unless indicate otherwise.
Step 1 : --------------------------------
Step 2 : --------------------------------
Step 3 : --------------------------------
Step 4 : --------------------------------
-
Step n: Exit.
The Algorithm is completed when the statement.
Exit.
3) Comments: Each step may contain a comment in brackets [ -----], which indicates the
main purpose of the step (Non execute Statements).
4) Variable Names: Variable names will use capital letters as MAX, DATA etc. Single
capital Letter names of variables used as counters or subscripts.
5) Assignment Statement: The dots-equal notation ( := ).
Example: MAX :=DATA[1]
6) Input and Output: Data may be input assigned to variable by means of Read: statement
with the following form:
Read: Variables names
Similarly, messages, placed in quotation marks, and data in variables may be output by
means of a Write: statement with the following form:
Write: Messages and / or Variable names

P a g e | 25
Computer Science ( D9) : XII - Paper –I

Q4: Explain with Flowchart the following control structure: 1) Sequence Logic 2)
Selection Logic 3) Looping Logic.
7) Control Structure:
Sequence Logic Selection Logic Looping (Iteration) Logic

a) Single Alternative
Module A If condition, then :
Module B Module A
Module C [End of if structure]
a) Repeat-while loop
Module D Module B
Repeat ---- While Condition:
b) Double Modules
Alternative
[End of while loop]
If condition, then:
Module A b) Repeat-for loop
Else: Repeat ---- For K:= R to S by T:
Module B Modules
[End of if structure] [End of for loop]

R (Initial Value), S (Final Value), T (Increment, but it is


optional) i.e. R >S (Increment), R<S (Decrement)

ARRAYS, RECORDS AND POINTERS

Data structures are classified as either linear (Arrays, Linked Lists, queues, Stack) or non-
linear (Trees). A Data Structure is said to be linear if its elements form a sequence.

Q6. What is Linear Array ?Explain Mar 15, 18, Oct 13, 14(3)
LINEAR ARRAYS:A linear array is a list of a finite number n of homogeneous data
elements such that:
a) The elements of the array are referenced respectively by an Index set consisting of n
consecutive numbers.
b) The elements of the array are stored respectively in successive memory locations.

P a g e | 26
Computer Science ( D9) : XII - Paper –I

The number n of elements is called the length or size of the array.


Length = UB – LB +1
Where UB is the largest index, called Upper Bound, and LB is the smallest index, called the
Lower Bound, of the array.
The elements of an array A may be denoted by the subscript notations:
A[1],A[2],A[3],A[4],A[5],A[6]-------,A[N]
The number K in A[k] is called a Subscript or an Index and A[K] is called a Subscripted
Variable.

Q7. What is Array? Write an Algorithm for Traversing Linear Array? Mar 05, 18(5)
Oct 05,12 ,15 Jul 18(5).
TRAVERSING:
Algorithm 1.1: (Traversing a Linear) Here LA is a linear array with lower bound LB and
upper bound UB. This algorithm traverses applying an operation PROCESS to each element
of LA.
Step 1 : [Initialize counter] Set K := LB
Step 2 :Repeat Step 3 & 4 While K<=UB:
Step 3: [Visit Element] Apply PROCESS to LA[K]
Step 4: [Increment Counter] SetK := K + 1
[End of While loop]
Step 5: Exit.

Q8. Write an Algorithm for Insert an Element into Array? Mar 09, 11, 16, Oct 18(5)
INSERTING AND DELETING:
Let A be a collection of data elements in the memory of the computer. “Inserting” refers to
the operation of adding another element to the collection A, B, C, D, E, H and L and
“Deleting” refers to the operation of removing one of the elements from A.
Inserting and deleting n element at the “end” of a linear array can be easily done, but
inserting and deleting an element somewhere in the middle of the array required subsequent
element to be moved one location downward or upward in order to fill up the array.

P a g e | 27
Computer Science ( D9) : XII - Paper –I

Inserting
serting Logic Deleting Logic

Algorithm 1.2: (Inserting


Inserting into a Linear Array
Array) INSERT (LA, N, K, ITEM)
Here LA is a Linear array with N elements and K is a positive integer such that K<=N. This
algorithm inserts an element ITEM into the Kth position in LA.
Step 1 : [Initialize counter
counter]Set J := N
Step 2 :Repeat Steps 3 and 4 While J >= K:
Step 3 :[Move Jth element downward]
downward]Set LA[J+1] := LA[J]
Step 4 : [Decrease Counter ] SetJ := J -1
[End of While loop]
Step 5 : [Insert Element] SetLA[K] := ITEM
Step 6 : [Reset N] Set
SetN:= N+1
Step 7:Exit.

Algorithm 1.3: (Deleting into a Linear Array


Array)DELETE (LA, N, K, ITEM)
Here LA is a Linear array with N elements and K is a positive integer such that K<=N. This
algorithm deletes the Kthelement from LA.
Step 1 :[Initialization]]SetITEM := LA[K] and J :=K
Step 2 :Repeat Steps 3 and 4 While J <= N-1:
Step 3 : [Move J+1st element upward] SetLA[J] := LA[J+1]
Step 4 : [Increment Counter ] SetJ := J +1
[End of While loop]
Step 5 : [Reset N] Set
SetN:= N-1
Step 6:Exit.

Q9. Write an Algorithm to sort the element of Array using? Mar 02, 05, 08,17, 20 Oct
05,08(5), 14(4).
SORTING; BUBBLE SORT
Let A be a list of n number. Sorting A refers to the operation of rearranging the elements of
A, so they are in increasing order i.e.
A[1]< A[2] < A[3] < ------ < A[N]
Let us discuss Bubble Sort,suppose the list of numbers A[1], A[2], A[3] ------ A[N] is in
memory. The bubble sort algorithm works as follows:
Iteration 1: Compare A[1] and A[2] aand
nd arrange them in the desired order, so that A[1] <
A[2]. Then compare A[2] and A[3] and arrange them so that A[2] < A[3]. Then compare

P a g e | 28
Computer Science ( D9) : XII - Paper –I

A[3] and A[4]and arrange them so that A[3] < A[4]. Continue until we compare A[N-1] with
A[N] and arrange them so that A[N-1] < A[N].

Observe that iteration 1 involves N-1 comparisons (During iteration1, the smallest elements
are “Bubble Up” and largest elements sinks to the Nth position i.eA[N].)
Iteration 2: Repeat Iteration 1 with one less comparisons; that is, now we compare and
possibly rearrange A[N-2] with A[N-1]
Iteration 2 involves N-2 comparisons and when iteration 2 completed, the second largest
element will sinks A[N-1].
Iteration 3: Repeat Iteration 1 with two fewer comparisons; that is, we stop after we compare
and possibly rearrange A[N-3] with A[N-2]
Iteration 2 involves N-3 comparisons and when iteration 3 completed, thridlargest element
will sinks A[N-2].
………………………………………………………………………………………….
………………………………………………………………………………………….
Iteration N-1: Compare A[1] with A[2] and arrange them so that A[1] < A[2]
After iteration N-1, the list will be sorted in ascending order.
Algorithm 1.4: (Bubble Sort)BUBBLE (DATA, N) Here DATA is an array with N
elements. This algorithm sorts the elements in DATA.
Step 1 :Repeat Step 2 & 3For K:= 1 to N-1
Step 2 : [Initialize comparisons Pointer PTR] 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) [Increase Pointer] Set PTR:= PTR+ 1
[End of While loop]
[End of For loop]
Step 4: Exit.
Q10. Explain Linear Searchalgorithm with suitable example? Mar 03, 07, 09,10, Jul
16(3).
SEARCHING: LINEAR SEARCH
Let DATA be a collection of data elements in memory, and suppose a specific ITEM of
information is given. Searching refers to the operation of finding the locations LOC of ITEM
in DATA. The search is said to be successfully if ITEM does appear in DATA and
unsuccessfully otherwise. Linear search uses an insertion and search algorithm with traverses
DATA sequentially to locate ITEM, rather than simple a search algorithm is called Linear
Search or Sequential Search.
Example:Consider the array NAME in fig. where N =5 and n = 7

P a g e | 29
Computer Science ( D9) : XII - Paper –I

Suppose we want to know whether Paul appears in the array. This algorithm temporarily
place Paul at the end of the array as shown in the figure, by setting NAME[6]= Paul. Then the
algorithm searches the array from top to botto
bottom.
m. Since Paul first appears in NAME[N+1], so
Paul is not found in the list, search is unsuccessful.
Suppose
ppose we want to know whether Smith appears in the array. This algorithm temporarily
place Smith at the end of the array as shown in the figure, by setti
setting
ng NAME[6]= Smith. Then
the algorithm searches the array from top to bottom. Since Smith first appears in NAME
NAME[4],
so Smith is found in the list, search is successful.
Algorithm 1.5: (Linear Search)LINEAR (DATA (DATA, N, K, ITEM, LOC),, Here DATA is a
linear array
ray with N elements and ITEM is a given item of information. This algorithm finds
the location LOC of ITEM in DATA, or sets LOC:=0 if the search is unsuccessful.
Step 1 : [Insert
nsert ITEM at the end of DATA
DATA] Set DATA[N+1] :== ITEM
Step 2 : [Initialize
Initialize coun
counter]Set LOC : = 1
Step 3 :[Search
[Search for ITEM] RepeatWhileDATA[LOC] ≠ ITEM:
Step 4:: [Increase Counter] Set LOC:= LOC + 1
[End of While loop]
Step 5: [Successful?]] If LOC = N+1, Then:Set LOC:= 0
Step 6: Exit.

Q11. Explain Binary Searcha


Searchalgorithm with suitable example? Mar 13, 14, 15, 19 Oct 02,
06, 11, 12, 13(3).

SEARCHING: BINARY RY SEARCH


Suppose DATA is an array which is sorted in increasing numerical order (ascending).Then
there is an extremely efficient searching algorithm called Binary Search, which ca
can used to
find the location LOC of a given ITEM of information in DATA.
Algorithm 1.6: (Binary Search)
Search)BINARY (DATA, N, UB, LB, ITEM,, LOC), LOC Here DATA is
a linear
near array with N elements and ITEM is a given item of information. The variables BEG,
END and MID denote, respectively, the beginn
beginning,
ing, end and middle locations of a segment pd
elements of DATA. This algorithm finds the location LOC of ITEM in DATA, or sets
LOC:=NULL.

P a g e | 30
Computer Science ( D9) : XII - Paper –I

Step 1 : [Initialize segment variables] SetBEG: = LB, END := UB and


MID := INT((BEG+END)/2).
Step 2 :Repeat Step 3 & 4While BEG <= END and DATA[MID] ≠ ITEM:
Step 3: IfITEM< DATA [MID], Then:
SetEND := MID – 1
Else:
SetBEG := MID + 1
[End of If structure]
Step 4:SetMID := INT ((BEG + END)/2).
[End of While Loop]
Step 5: If DATA [MID] = ITEM, Then:
SetLOC := MID
Else:
SetLOC := NULL
[End of If structure]
Step 6: Exit.

Q12. Distinguish between linear and binary search. Or Write any three distinguishing
points between Linear Search and Binary Search. Mar 14, 17(3)
Comparison between "Linear Search" and "Binary Search"

Point Linear Search Binary Search


1 Linear Search Binary Search Binary Search is used to search an element from
Linear Search is used to search sorted array.
an element from sorted or
unsorted array.
2 In this method, the given element This method makes a comparison between the
is compared with each element ITEM to be searched and the MIDDLE element
of the list one by one. of the array.
3 The process of searching Comparisons may result in either a match or
terminates when the list is comparison could be continued with either half of
finished or a comparison results elements.
in success.
4 It is comparatively time Comparatively, it takes approximately half of the
consuming process. time in searching.
5 Time complexity: Best case: 1 Time complexity: Best case: 1 comparison Worst
comparison Worst case: 'N' case: Log2N comparison
comparison.
6 6 Example: Searching for the Example: Searching for the particular 'word' in a
particular item in the `Kirana' dictionary
list.

P a g e | 31
Computer Science ( D9) : XII - Paper –I

Q13. Explain pointers and pointer arrays with a suitable example. What is Data
structure? Define array and pointer array in data structure. Explain Pointer Array
with example. Mar12,15(3), 19,0ct02,06(3)

POINTERS; POINTER ARRAY:


Let DATA be any array. A variable P is called a
pointer if P “points” to an element in DATA i.e.
if P contains the address of an element in DATA.
An array PTR is called a Pointer array if each
element of PTR is a pointer. Pointer and pointer
array are used to facilitate the processing of the
information in DATA.
Q14.What is a Record? How it differs from a
Linear Array? Mar02 05 07,08,14,0ct10,11

RECORDS; RECORD STRUCTURES:


Collections of data are frequently organized into
a field, records and file. A Record is a collection
of related data items, each of which is called a
field or attribute, and a file is a collection of
similar records. Each data items may be a group
item composed of sub items and those items which are indecomposable are called elementary
items. The names given to the various data items are called identifiers.
A record is collection of data items; it differs from a linear array in the following ways.
a) A record may be a collection of non-homogeneous data i.e. the data items in a record
may have different data types.
b) The data items in a record are indexed by attribute names, so there may not be a
natural ordering of its elements.
Fields
SRNO NAME POST SALARY ADDRESS
records File
1 JOHN MANAGER 10,000 AKOLA
2 SAM CLERK 5000 NAGPUR
3 WAGNER JR. CLERK 5000 AKOLA

REPRESENTATION OF RECORDS IN MEMORY; PARALLEL ARRAY

P a g e | 32
Computer Science ( D9) : XII - Paper –I

Q15.Write four difference points between Record and Linear Array. Mar02 05
07,08,14,0ct10,11

Comparison between “Linear Array” and “Record”

Point Linear Array Records


1 Array is the collection of homogeneous data Record is the collection of non-
elements homogeneous data elements
2 Every element of an array can be accessed Every element of record can be accessed
and referenced by its index and referenced by its name.
3 The size allocated to store the record in 3 Size allocated to store an Array, in the
memory is the sum of memory required to memory, is the product of no of elements
store individual elements. of array and size required to store single
elements.
4 Elements of record can be group item and It is possible for array to have two or
can be decomposed in to sub items. more dimensions.
5 Example: Record of a student in the college. Example: Array to store marks of physics
(name, sex, age, per, etc) of 10 students. (Physics[101)

Q16. What is linked list? Explain with suitable example. OR Define Linked List. Draw
and explain labeled diagram of linked list with 5 nodes. Mar02,04,05,06,07,08,
13,14,15,20,0ct03,07,14(4)

LINKED LIST;
A Linked List or one way list is a linear collection of data elements called nodes, where the
linear order is given by means of pointers. That is, each node is divided into two parts; the
first part contains the information of the element and the second part, called the link field or
next-pointer field, contains the address of the next node in the list.

Next Pointer Field


Information Part

The above figure is a schematic diagram of a linked list with 6 nodes. Each node is pictured
with two parts. The left part represents the information part of the node, which may contain
an entire record of data items. The right part represents the next pointer field of the node, and
there is an arrow drawn from it to the next node in the list. The pointer of the last node
contains value called Null Pointer.

P a g e | 33
Computer Science ( D9) : XII - Paper –I

Q17. What is linked list? How they can be represented in memory. Oct 06, 07, 13 Mar03,
12, 14, 15(4), 17

REPRESENTATION OF LINKED LIST IN MEMORY:

Q18. What is BINARY TREE? Explain with suitable example.Mar02, 04, 05, 15(3), 17,
18(3), 19

BINARY TREES:
A binary tree T is defined as finite set of element, called nodes, such that:

a) T is empty (called the null tree or empty tree), or


b) T contains a distinguished node R, called the root of T, and the remaining nodes
of T form an ordered pair of disjoint binary trees T1 and T2.

If T does contain a root R, then the two trees T1 and T2are called, respectively, the left and
rightsub tree of R. If T1 is nonempty, then its root is called the left successor of R; similarly
itT2 is nonempty, then its root is called the right successor of R.

A binary tree T is frequently presented by means of a diagram.

B
C

D E G H

F J K

The above diagram represents the binary tree T as follows: 1) T consists of 11 nodes,
represented by the letters A through L, excluding 2) The root of T is node A at the top of the

P a g e | 34
Computer Science ( D9) : XII - Paper –I

diagram 3) A left downward slanted line indicate left successor of node A and a right
downward slant line indicate right successor of node A i.e. B is a left successor and C is right
successor of the node A. The left sub tree of the root A consists of nodes B, D, E and F and
the right sub tree of A consists of the nodes C, G, H, J, J, K and L.
Any node in a binary tree T has either 0, 1 , 2 successors. The nodes A, B, C and H has two
successors, the nodes E and J have only one successors, and the nodes D, F, G, L and K have
zero successors. The nodes with no successors are called terminal nodes.
REPRESENTATION OF BINARY TREES IN
MEMORY:
Let T be a binary tree. The ways of representing T in
memory a) Linked List representationb) Array
representation.

a) Linked List Representation: The first way is


called link represented of T in memory.
b) Array Representation: The Second way is called
Array represented of T in memory using three
arrays INFO, LEFT and RIGHT, and a pointer
variable called Start or Root.

i) Root: It is a special node in a tree structure and the entire tree is referenced through it. This
node does not have parent.[Oct06,10,Mar:17(1)]
(ii) Parent Node (Predecessor): It is an immediate predecessor of a node. In fig., A is the
parent of B,C and D.
(iii) Child (Successor) Node: All immediate successors of a node are its children. In the fig.
B,C and D are children of A.
(iv) Siblings:Nodes with the same parent are siblings. H, I and J are siblings as they are the
children of the same parent D.[Oct06,10,Mar17(1)]
v) Path: It is number of successive edges from source Ancestor node to destination node.
(vi) Degree of a node: The degree of a node is a number of children of a node.
(vii) Level of tree: Each node in a tree is assigned a level number. Generally the level
number of Root A of the tree is zero (0) and every other node is assigned to level number
which is one more than the level number of its parent. It is a distance from root.

P a g e | 35
Computer Science ( D9) : XII - Paper –I

(vii) Depth of a node: The depth of a node is the number of edges from the root to the node.
Hence Depth of Root node is "0".
(ix) Height of a node: The height of a node is the number of edges from the node to the
deepest leaf. Hence Height of Leaf node is "0". [Mar17(1)]
(x) Height of tree (Depth of tree): The height of a tree is a height of the root. If Root is level
0 then height of tree is equal to one more than the largest level number. The depth ' D' of the
above tree is 5 (largest level+1).
(xi) Leaf /Terminal/ External Node: A node with degree 0 is known as leaf node. A leaf
node is a terminal node and also called as External Node, it has no child.
(xii) Internal Nodes: A node with at least 1 child is known as internal node.
(xiii) Edge: The line drawn from a node to its successor is called Edge.
(xiv) Degree of tree: Degree of a tree is the highest degree of any node. The degree of tree of
the above figure is 3.
(xv) Forest: A forest is defined as a set of trees. A forest is a graph comprised of a collection
of trees. A forest with one connected component is a tree. In other words if you remove any
edge in the tree, it becomes forest. [Mar17(1)][Oct06,10,Mar 17, Ju118(1)]

ALGEBRAIC EXPRESSIONS:
Consider any algebraic expressions E involving
only binary operations, such as
E= (a-b) / ((c* d)+e)
E can be represented by means of - +
the binary tree T as shown in the figure.
That is each variable or content in E
a b * e
appears as an “Internal” node in T
whose left and right corresponds to c d
the operands of the operation.

1) E = (a-b)2/3 / ((c* d)+e)


/
+

- / * e

a b
c d

Solve: 1) E= (P-Q)/(R * S) +T 2)(2x+Y)(a=&b)33) [(a+b)*c]/[a*((b-c)+a)] Mar 02, 07 08


Oct 04, 114)E=(2X+y)/(5A-B)3 5) C= (X/Y)3– (5A+B2)

STACKS;
A stack is a list of elements in which an element may be inserted or deleted only at one end,
called the top of the stacks. This means, in particular, that elements are removed from stack
in reverse order in which they were inserted into the stack. Stacks are also called Last In First
Out (LIFO). Two basic operations associated with stacks are:
a) “PUSH” is the term used to insert an element into a stack.
b) “POP” is the term used to delete an element from a stack.

P a g e | 36
Computer Science ( D9) : XII - Paper –I

Examples: Stack of Dishes, Stack of Coins, Stack of Books.


QUEUES;
A Queue is linear list of elements in which deletions can take place only at one end called,
the front, and insertions can take place only at the other end, called the rear.Queues are also
called First In First Out (FIFO). Two basic operations associated with queue are:
a) “Rear” is the term used to insert an element into a queue.
b) “Front” is the term used to delete an element from a queue.
Examples: The people waiting in line at a bus stop form a queue.

P a g e | 37

You might also like