data structure
data structure
Suppose ITEM = 33
Ans.: Step 1: Set DATA [6] = 33, List becomes Q.representation of linked list in
1. Data may be organized in many different ways. 11 22 33 44 55 33 memory.
A data structure is the way in which different data Step 2: LOC = 1 Ans:Linked lists can be represented in
elements are logically related. Step 3: Since DATA [1] ≠ 33 ∴ LOC = 2 memory by using two arrays respectively
2. Collection of data elements forming an Since DATA [2] ≠ 33 ∴ LOC = 3 known as INFO and LINK, such that
organization characterized by the accessing Here DATA [3] = 33 ∴ 33 = ITEM INFO[K] and LINK[K] contains information
functions is called data structure. Step 4: Hence ITEM = 33 found at position, of element and next node address
3. The data structure should be simple and it LOC=3 respectively.
should show the relationship between data
The list also requires a variable 'Name' or
elements. Q. Explain Bubble sort algorithm with suitable
'Start', which contains address of first node.
4.Types: example.
Pointer field of last node denoted by NULL
i.Linear Data Structure: In linear data Ans.: Algorithm:
structures, data elements are stored in
which indicates the end of list. e.g. Consider
Bubble Sort (DATA, N)
consecutive memory locations or by using linked Here DATA is a linear array with N elements. a linked list given below:
representation, e.g., arrays, linked lists. This algorithm sorts elements of DATA in
ii.Non-linear Data Structure: In non-linear data ascending order.
structures, the linear order cannot be maintained Step 1: Repeat steps 2 and 3 for K := 1 To N -1:
between data elements. Generally, data Step 2: Set Ptr := 1
elements have hierarchical relationships between Step 3: Repeat While Ptr ≤ N - K:
them, e.g., trees. (a) If DATA [Ptr] > DATA [Ptr + 1], then
Examples: Computer languages provide different interchange Linked list can be represent in memory as:
data structures like arrays, stack, queue, tree, DATA [Ptr] and DATA [Ptr + 1]
etc. [End of If structure]
(b) [increment pointer]
Q. What is a record? How does it differ from a Set ptr := ptr + 1
linear array? [End of inner loop]
Ans.:A record is a collection of fields or [End of outer loop]
attributes, i.e., relative data items, frequently Step 4: Exit
organized into a hierarchy of fields, i.e., records.
A file is nothing but a collection of records.
Difference between records and linear arrays:
1. A record is a collection of fields, while an array
is a list of homogeneous data elements. Q. 22 Write an algorithm for binary search
2. A record may contain non-homogeneous data, technique with example.
i.e., data elements may be of different types. An Ans.: Binary search is used to search an element
array always contains homogeneous data. from sorted array. Above figure shows linked list. It indicates that
3. In a record, natural ordering of elements is not Algorithm: Binary search. the node of a list need not occupy adjacent
possible. Array elements can be naturally Binary (DATA, LB, UB, ITEM, LOC) elements in the array INFO and LINK.
ordered. Here, DATA is a sorted array with lower bound
Q. Explain with flowcharts the following
4. Elements of a record are referenced by level (LB) and upper bound (UB). ITEM is the given
control structures:
number, while those of an array are referenced element. BEG denotes beginning, MID denotes
(i) Sequence logic, (ii) Selection logic, (iii)
by an index set consisting of n consecutive middle, and END denotes end location of DATA.
Iteration logic
numbers. This algorithm finds the location (LOC) of ITEM in
OR
DATA or sets LOC to NULL if the search is
Explain 3 types of control structures used for
Q.What do you understand by the term unsuccessful.
flow of control.
searching? What are the different types of Step 1: [Initialize Variables]Set BEG := LB,
Ans.:
searching algorithms? Explain the linear END:= UB, and MID := INT((BEG + END) / 2)
(i) Sequence logic:
searching algorithm. Step 2: Repeat steps 3 and 4 while BEG = END
In sequence logic, modules are executed
Ans. : Searching: Searching means to find out AND DATA[MID] != ITEM
sequentially, one after the other. The sequence
particular element from a given list of elements or Step 3: If ITEM < DATA[MID], then:
may be present explicitly by means of numbered
check whether required element is present or not Set END := MID - 1
steps or by the order in which modules are
in an array. There are two types of searching Else:
written. Flowchart equivalent
algorithms: Set BEG := MID + 1
1. Linear search [End of If structure]
Module A
2. Binary search Step 4: Set MID := INT((BEG + END) / 2)
Linear searching algorithm: [End of step 2 loop]
In linear search the given element is compared Step 5: If DATA[MID] = ITEM, then:
Module B
with each element of list one by one. For Set LOC := MID
algorithm, refer to next Q. Else:
LOC := NULL
Module c
Q. Write an algorithm for linear search [End of If structure]
technique with suitable example. Step 6: Exit
(ii) Selection logic:
Ans. : Example:
Selection logic uses a number of conditions,
Algorithm: Linear Search Given DATA be the following sorted 13-element
which cause the selection of one out of several
LINEAR(DATA, N, ITEM, LOC) array:
alternative modules.
Here DATA is a linear array with N elements and 11 22 30 33 40 44 55
(a) Single alternative:
ITEM is given element. This algorithm finds the 60 66 77 80 88 99
If the condition is satisfied, then module A, which
location LOC of ITEM in DATA or sets LOC = 0, if Suppose ITEM = 40
consists of a number of statements, is executed.
search is unsuccessful. Step 1: Initially, BEG = 1 and END = 13
Otherwise, module A is skipped and the next
Step 1: [Insert ITEM at the end of DATA] Hence MID = INT((1 + 13) / 2) = 7
module is
Set DATA [N + 1] := ITEM So, DATA[MID] = DATA[7] = 55
executed.
Step 2: [Initialize counter] Step 2: Since 40 < 55, END has its value
Set LOC := 1 changed to:
Step 3: [Search for item] END = MID - 1 = 7 - 1 = 6
Repeat While DATA [LOC] ≠ ITEM : Hence MID = INT((1 + 6) / 2) = 3
Set LOC := LOC + 1 So, DATA[MID] = DATA[3] = 30
[End of loop] Step 3:Since 40 > 30, BEG has its value
Double alternative:
Step 4: If LOC = N + 1, then : changed to:
If the condition is
Set LOC := 0 BEG = MID + 1 = 3 + 1 = 4
satisfied, then
Step 5: Exit Hence MID = INT((4 + 6) / 2) = 5
module A will be
For example: Given DATA array with following 5 So, DATA[MID] = DATA[5] = 40
executed; otherwise,
elements Result: Found ITEM in location LOC = MID = 5
module B will be
11 22 33 44 55
executed.
Iteration logic: Here, a certain module is 1. Level of a Tree: Example: Consider a tree with depth 3. The total
executed repeatedly until the condition satisfies. number of nodes in a given tree with depth 3 is:
At first, the body of the loop (i.e., the module) will Each node in a tree is assigned a level 2n−1=
23−1
be executed with K = R and then with K = R + T number. Generally, the level number =8−1
, then with K = R + 2T , and so on until K = S .
The loop ends when K > S . of the root 'R' of the tree is zero, and =7
The tree with depth n having 2n−1 total nodes.
every other node is assigned a level A level 1
number that is one more than the level / \
B C level 2
number of its parent. It represents the /\ / \
distance from the root. D E F G level 3
Depth
Example:
Q.12: What are linear arrays?
A Level 0 Ans:
/ | 1.A data structure is said to be linear if its elements
form a sequence.
B C Level 1 2.A linear array is a data structure that consists of a
/\ /\ finite, ordered set of homogeneous data elements such
that:
Question:What is a tree? What do you D E F G Level 2 3.The elements of the array are referenced respectively
mean by root, leaf, siblings, and child 2. Depth / Height: by an index set (subscript) consisting of 'n'
consecutive numbers.
about a tree? The depth of a tree is defined as the 4.The elements of the array are stored respectively in
Answer: maximum level of any node in the tree. successive memory locations.
If the root is level 0, the depth or 5.The number 'n' of the elements is called the length or
Tree: A tree is a non-linear hierarchical size of the array.
data structure consisting of a finite set height of the tree is equal to 1 plus the 6.Formula:
largest level number. In general, the size or length of the array can be
of one or more nodes (i.e., collected obtained from the index set by the formula: Length =
data items) such that: UB − LB + 1 where UB is the largest index called
Upper Bound and LB is the smallest index called
i. There is a specially designated node 3. Degree: Lower Bound.
called the root. The number of subtrees of a node is 7.Example:
Let DATA be a 5-
ii.The remaining nodes are partitioned called the degree of a node. The element linear
into n (0 finite disjoint sets T1, T2, ..., degree of a tree is the maximum array as follows:
Tn), where each of these sets is a tree. degree of any node in the tree.
T1, T2, ..., Tn are called 'subtrees' of Example: 8.The element of an
the root. array may be denoted by the subscript notation such
that: DATA[3] = 600.
iii.The figure shows a tree with 11 A
nodes, each item of data being a single / \ In C++:An array is declared as: int data[100]; which
specifies an array 'data' of 100 integers.
letter. B C
Diagram of a tree with nodes labeled /\ / \ \ Q. 11 Write an algorithm to find
solutions of the quadratic equation
as follows: D EF GH 𝑨𝒙𝟐 + 𝑩𝒙 + 𝑪 = 𝟎 when 𝑨 ≠ 𝟎.
A Answer: The algorithm inputs the coefficients
/\ Node Degree 𝐴, 𝐵, 𝐶 of a quadratic equation and outputs
A: 2 real solutions, if any.
B C
1.Read 𝐴, 𝐵, 𝐶.
/| |\ B: 2 2.Set 𝐷: = 𝐵2 − 4 ∗ 𝐴 ∗ 𝐶.
DE FG C: 3 3.If 𝐷 > 0, then:
−𝐵+√𝐷
/| |\ D, E, F, G 0 a) Set 𝑋1 : =
2𝐴
and 𝑋2 : =
HI JK The tree has a maximum degree of 3. −𝐵−√𝐷
.
2𝐴
Root: A node which has no parent. Q.What is a binary tree? b) Write: 𝑋1 , 𝑋2 .
−𝐵
Generally, the first node is called the ans.: Binary tree is a finite set of elements called 4.Else if 𝐷 = 0, then: a) Set 𝑋: = . b) Write:
2𝐴
nodes such that is: It may be empty or It is
'root node.' In the figure, node A is the "UNIQUE SOLUTION", 𝑋.
partitioned into three disjoint subsets: a) there is
a single distinguished element called the root of 5.Else: Write: "NO REAL SOLUTION".
root of the tree. tree. b) other two subsets are themselves binary 𝐸𝑛𝑑𝑜𝑓𝐼𝑓𝑠𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒.
Leaf: A node which has no children. tree called left subtree and right subtree of the 6.Exit.
original tree. A left and right subtree can be Types of Tree
Such nodes have a degree of zero. In empty. In binary tree, there is no node with
the figure D, F, J, and K are the leaf degree greater than two. •Null Tree:
Example; A tree without node or with only
nodes, also called terminal nodes.
Child: The nodes that are reachable A root node
from a node, say u, through a single
/ \ • Ordered Tree:
B C
edge are called children of u. For
Children from each node are
/\ / \
D E F G ordered from left to right
example, in the figure, the children of
/ / •Non-ordered Tree:
node C are F, G, and H. H I Children of each node are not
Sibling: Children of the same parent Q.Relationship between total number of
nodes and depth of a tree: Depth of a tree ordered from left to right.
are said to be siblings. For example, means the maximum level of any node in a
the nodes D and E are the children of tree. The maximum number of nodes in a binary
tree with depth n is 2n - 1.
node B. So D and E are siblings.