Data Structure: Computer Science (D9) : XII - Paper - I
Data Structure: 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.
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]
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
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
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.
P a g e | 30
Computer Science ( D9) : XII - Paper –I
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"
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)
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
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.
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
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:
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.
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.
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.
- / * e
a b
c d
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
P a g e | 37