Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Data are simply values or set of values. A data item refers to a single unit of
values. Data items that are divided into sub items are called group items, those are
not are called elementary items.
For example:
An employee’s name may be divided into three sub items
first name, middle initial and last name, but the employee id would normally treated
as a single item. Collections of data are frequently
organized into a hierarchy of fields, records and files. An entity is something that
has certain attributes or properties which may be assigned values. The values
themselves may be either numeric or nonnumeric.
For example:
STUDENT
John Brown
Sandra
Gold
Tom Jones
June Kelly
Mary Reed
Alan Smith
2) Linked List:
Suppose a brokerage firm maintains a file where each record contains a
customer’s name and his or her salesperson. The data is as follows,
Customer Salesperson
1 Adams Smith
.
2 Brown Ray
.
3 Clark Jones
.
4 Drew Ray
.
5 Evans Smith
.
6 Farmer Jones
.
7 Geller Ray
.
8 Hill Smith
.
9 Infeld Ray
.
Another way of storing data in above table is to have a separate array for the
salespeople and an entry in the customer file which gives the location of each
customer’s salesperson.
For example:
Customer Pointer
Adams 3
Brown 2
Clark 1
Drew 2
Evans 3
Farmer 1 Salesperson
Geller 2 1. Jones
Hill 3 2. Ray
Infeld 2 3. Smith
In this structure, an integer used as pointer requires less space than a name,
hence this representation saves space, suppose the firm wants the list of customers
for a given salesperson, the firm would have to search through the entire customer
file. One way to simplify such a search is to have the arrows point the other way,
each salesperson would now have a set pointers giving the positions of his or her
customers, as shown below,
Salesperson Pointer
Jones 3,6
Ray 2,4,7,9
Smith 1,5,8
Customer Link
1 Adams 5
.
2 Brown 4
.
3 Clark 6
.
4 Drew 7
.
5 Evans 8
.
6 Farmer 0
.
7 Geller 9
.
8 Hill 0
Salesper Point
. son er
9 Infeld 0 Jones 3
. Ray 2
Smith 1
3) Trees:
Data frequently contain a hierarchical relationship between various elements.
The data structure which reflects this relationship is called a rooted tree
graph or tree
4) Stack:
A stack, also called a last in lat out (LIFO) system, is a linear list in which
insertions and deletions can take place only at one end, called the top. This
structure is similar in its operation to a stack of dishes on a spring system.
5) Queues:
A queue, also called a first in first out (FIFO) system, is a linear list in which
deletions can take place only at one end of the “front” of the list, and
insertions can take place only at the other end of the list, the “rear” of the
list. This structure operates in much the same way as a line of people waiting
at a bus stop.
6) Graph:
Data sometimes contain a relationship between pairs of elements which is
not necessarily hierarchical in nature. The data structure which reflects this
type of relationship is called a graph.
1. Traversing: Accessing each record exactly once so that certain items in the
record may be processed.
2. 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
3. Insertion: Adding a new record to the structure.
4. Deleting: Removing record from the structure.
5. Sorting: Arranging the record in some logical order.
6. Merging: Combing the records in two different sorted files into a single
sorted file
Algorithm notations:
1. Steps, Control, Exit: The steps of the algorithm are executed one after the
other. Control may be transferred to Step n of the algorithm by the
statement “Go to Step n”. The algorithm is completed when the statement
Exit is encountered.
2. Comments: Each step may contain a comment in brackets which in brackets
which indicate the main purpose of the step. The comment will usually
appear at the beginning or the end of the step.
3. Variable names: Variable names will use capital letters. Single names of
variables used as counters or subscripts will also be capitalized in the
algorithms even though lowercase may be used for these same variables in
the accompanying mathematical description and analysis.
4. Assignment Statement: Assignment statements will use the dots-equal
notation (:=) . For example, MAX := DATA[1], assigns the value in DATA[1] to
MAX. Some text s use the backward arrow ( ) or the equal sign ( = ) for
this operation.
5. Input and Output: Data may be input and assigned to variables by means
of Read statement with the following form:
Read: Variable names
Similarly, message placed in quotation marks, and data in variables may be
output by means of a Write or Print statement with the following form:
Write: Message and/or variable names
For example:
Algorithm: (Largest Element in Array) A nonempty array DATA with N
numerical values is given. This algorithm finds the location LOC and the value
MAX of the largest element of DATA. The variable K is used as a counter.
Step1. [Initialize.] Set K:= 1, LOC:= 1 and MAX:= DATA[1]
Step 2. [Increament counter] Set K := K + 1.
Step 3. [Test counter.] If K > N then:
Write: LOC, MAX, and Exit.
Step 4. [Compare and update] If MAX < DATA[K], then:
Set LOC := K and MAX :+ DATA[K].
Step 5. [Repeat loop] Go to step 2.
Control Structures:
Algorithms and their equivalent computer programs are more easily
understood if they mainly use self-contained modules and three types of logic
or flow of control,
1. Sequence logic or Sequential flow
2. Selection logic or conditional flow
3. Iteration logic or repetitive flow
Sequence logic:
Unless instructions are given to the contrary,, the modules are
executed in the obvious sequence.
(Sequential logic)
Selection logic:
Selection logic has employs a number of conditions which leads to a
selection of one out of several alternatives modules. The structures which
implement this logic are called conditional structure of If structure. These
conditional structures fall into three types,
1. Single Alternative: This structure has the form,
If condition, then:
[Module A]
[End of If structure]
If condition holds, then Module A, which may contain one or more
statements, is executed; otherwise Module A is skipped and control
transfers to the next step of the algorithm.
No
Condition
Module
Yes
(Single Alternative)
No
Condition
Yes
Module
Module B
A
(Double Alternative)
The logic of this structure allows only one of the modules to be executed .
Specifically, either the module which follows the first condition which
holds is executed, or the module which follows the final Else statement is
executed.
For example:
Algorithm: (Quadratic equation) This algorithm inputs the coefficients A,
B, C of a quadratic equation and outputs the real solutions, if any.
Step 1. Read: A, B, C.
Step 2. Set D := B2 – 4AC
Step3. If D > 0 then:
(a) Set X1 := (-B + √D ) / 2A. and
X2 := (-B - √D ) /2A.
(b) Write: X1, X2.
Else if D=0, then:
(a) Set X:= -B/2A.
(b) Write: ‘UNIQUE SOLUTION’, X.
Else:
Write: ‘NO REAL SOLUTIONS’
[End If structure.]
Step 4. Exit.
Iteration Logic:
The third kind of logic refers to either of two types of structures
involving loops. Each type begins with a Repeat statement and is followed
by a module, callled body of the loop.
The Repeat-for loop uses an index variable, such as K, to control the loop.
The loop will usually have the form:
Repeat for K = R to S by T:
[Module]
[End of loop]
Here R is called the initial value, S the end value or test value, and T the
increment. The body of the loop is executed first with K = R, then with K =
R + T, then with K = R + 2T, and so on. The cycling ends when K > S.
The repeat-while loop uses a condition to control the loop. The loop will
usually have the form,
Repeat while condition:
[Module]
[End of loop]
The cycling continues until the condition is false.
(Repeat-for loop)
(Repeat-while loop)
For example:
Algorithm: (Largest Element in Array) Given a nonempty array DATA with N
numerical values, this algorithm finds the location LOC and the value MAX of
the element of DATA.
Step 1. [Initialize.] Set K: = 1, Loc: = 1, and MAX: = DATA [1].
Step 2. Repeat Step 3 and 4, while K ≤ N:
Step 3. If MAX < DATA [K], then:
Set LOC := K and MAX := DATA[K].
[End of If structure.]
Step 4. Set k := K+ 1.
[End of Step 2 loop.]
Step 5. Write: LOC, MAX.
Step 6. Exit
Linear Array
A linear array a list of a finite number of homogeneous data elements such
that,
i. The elements of the array are referenced respectively by an index set
consisting of n consecutive numbers
ii. The elements of the array are stored respectively in successive memory
locations.
The number n of elements is called length or size of the array. In general, the
length or the number of data elements of the array can be obtained from the
index set by formula,
Length = UB – LB + 1
where UB is the largest index, called the upper bound and LB is the smallest
index, called the lower bound of the array.
For example:
Let DATA be a 6 elements array of integers such that,
Length = UB – LB + 1
=6–1+1
=6
The length of DATA array is 6.
200
201
202 AUTO[1]
203
204
205 AUTO[2]
206
207
208
209 AUTO[3]
210
211
. .
. .
. .
Algorithm:
(Traversing a linear array) Here LA is a linear array with lower bound LB and
Upper bound UB.
Step 1. [Initialize counter] Set K := LB.
Step 2. Repeat Step 3 and 4 while K ≤ UB
Step 3. [Visit element] Apply PROCESS to LA[K].
Step 4. [Increase Counter] Set K := K + 1.
[End of Step 2 loop]
Step 5. Exit
Name Name
Name
1 Brown 1 Brown
2 Ford
2 Davis 3 Johnson
4 Smith
3 Johnson
5 Wagner
4 Smith 6
7
5 Wagner 8
1 Brown
2 Davis
3 Ford
4 Johnson
5 Smith
6 Wagner
7
8
Algorithm:
(Inserting into a linear 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] J := N.
Step 2. Repeat step 3 and 4 while J ≥ K.
Step 3. [Move Jth element downward] Set LA[J + 1] := LA[J]
Step 4. [Decrease counter] Set J := J + 1.
[End of step 2 loop]
Step 5. [Insert element] Set LA[K] := ITEM
Step 6. [Reset N] Set N := N + 1
Step 7. Exit.
Bubble Sort:
Algorithm:
(Bubble Sort) BUBBLE (DATA, N)
Here DATA is an array with elements. This algorithm sorts the elements in
DATA.
Step 1: Repeat Step 2 and 3 for K = 1 to N-1.
Step 2: Set PTR: = 1 [Initializes pass pointer PTR]]
Step 3: Repeat while PTR ≤ N – K.
(a) If DATA[PTR] > DATA[PTR + 1], then
Interchange DATA[PTR] and DATA[PTR + 1]
[End of if Structure]
(b) Set PTR := PTR + 1
[End of Inner loop]
[End of outer loop]
Step 4: Exit.
For example:
Suppose the following numbers are stored in an array A.
32, 51, 27, 85, 66, 23, 13, 57.
b) Compare A2 and A3. Since 51 > 27, interchange 51 and 27, interchange
51 and 27 as follows, 32, 27, 51, 85, 66, 23, 13, 57.
c) Compare A3 and A4. Since 52 < 85, the list is not altered.
d) Compare A4 and A5. Since 85 > 66, interchange 85 and 66 as follows,
32, 27, 51, 66, 85, 23, 13, 57.
e) Compare A5 and A6. Since 85>23
32, 27, 51, 66, 23, 85, 13, 57
f) Compare A6 and A7. Since 85>13, interchange 85 and 13,
32, 27, 51, 66, 23, 13, 85, 57
g) Compare A7 and A8. Since 85.57, interchange 85 and 57
32, 27, 51, 66, 23, 13, 57, 85
At the end of this first pass, the largest number, 85 has moved to last position.
However the rest of the numbers are not sorted, even though some of them have
changed their positions.
For the remainder of the passes, we show only interchanges,
Pass 2:
27, 33, 51, 66, 23, 13, 57, 85
Pass 3:
27, 33, 23, 51, 13, 57, 66, 85
27, 33, 23, 13, 51, 57, 66, 85
Pass 4:
27, 23, 33, 13, 51, 57, 66, 85
27, 223, 13, 33, 51, 57, 66, 85
Pass 5:
23, 27, 13, 33, 51, 57, 66, 85
23, 13, 27, 33, 51, 57, 66, 85
Pass 6:
13, 23, 27, 33, 51, 57, 66, 85
Searching:
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 LOC of
ITEM in DATA or printing some message that ITEM does not appear there.
Linear search:
Suppose DATA is a linear array with n elements. To search ITEM in DATA,
compare ITEM with each element of DATA one by one. That is, first we test whether
DATA [1] = ITEM and then whether
DATA [2] =ITEM, and so on. This methods, which traverse DATA sequentially to
locate ITEM, is called linear search
Example:
Binary Search:
Algorithm: (Binary Search) Binary (DATA, LB, UB, ITEM, LOC)
Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a
given item of information. The variable BEG, END and MID denotes beginning, end
and middle locations of a segment of elements of DATA. This algorithm finds the
location LOC of ITEM in DATA or sets LOC = NULL.
1. [Initialize segment variable] 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 following sorted 13 elements array
11, 22, 30, 33, 40, 55, 60, 66, 77, 80, 88, 99
Suppose ITEM = 40,
i. Initially BEG = 1 and END = 13, hence
MID = INT [( 1 + 13 ) / 2] = 7 and so DATA [MID] = 55
ii. Since 40 < 55, END has its value changed by END = MID – 1 = 6.
Hence MID = INT [( 1 + 6) /2 ] = 3 and so DATA [MID] = 30.
iii. Since 30 < 40, BEG has its value changed by BEG = MID + 1 = 4.
Hence MID = INT [( 4 + 6) /2 ] = 5 and so DATA [MID] = 40.
We have found ITEM in location LOC = MID = 5.
1. 11 22 30 33 40 44 55 60 66 77 80 88 99
2. 11 22 30 33 40 44 55 60 66 77 80 88 99
3. 11 22 30 33 40 44 55 60 66 77 80 88 99
The way of storing membership list is showing in following fig a. This method
is space efficient. Also the entire list can easily be processed. On the other hand,
there is no way to access any particulars group, e.g. there is no way to find and
print only the names in third group.
Fig b
The modified version of fig (a) is pictured in fig (b). that is, the names are
listed in linear array, group by group, except now some marker, such as the
three dollar signs used here, will indicate the end of a group. The main
drawback of this representation is that the list still must be traversed form
beginning in order to recognize a certain group.
Pointer Array:
To overcome above problem, we used pointer array, which contains the
locations of the different groups or more specifically, the locations of the first
elements in the different groups.
For example, GROUP [ L ] and GROUP [ L + 1] – 1 contain the first and last
elements in group L.
MEMBER
1. Evans
2. Harris
3. Lewis Group 1
4. Shaw
5. Cornad
GROUP . .
. .
Group 2
1. 1 . .
2. 5 1 Wagner
3. 1 3.
4 1 Davis Group 3
4. 1 4.
6 1 Segal
5. 2 5.
2 1 Baker Group 4
6.
. .
. .
. .
2 Reed
1.
2 $$$
2.
Suppose a hospital keeps a record on each new born baby which contains the
following data items: Name, Sex, Birthday, Father, Mother. Suppose further that
Birthday is a group item with subitems Month, Day and Year, and Father and Mother
are group items, each with subitems Name and Age.
The structure of the above record is usually described as follows.
1 New born
2 Name
2 Sex
2 Birthday
3 Month
3 Day
3 Year
2 Father
3 Name
3 Age
2 Mother
3 Name
3 Age
The number to the left of each identifier is called a level number .Each group
item is followed by its subitems and the level of the subitems is 1 more than the
level of the group item.
LINKED LIST
A linked list is a 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 second, called the link field
contains the address of the next in the list.
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 link 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 a special value, called the null pointer
which is any invalid address.
The null pointer, denoted by x in the diagram, signals the end of the list. The linked
list also contains a list pointer. The linked list also contains a list pointer variable
called START which contains the address of the first node in the list.
For example:
A hospital ward contains 12 beds of which 9 are occupied. Suppose we want an
alphabetical listing of the patients. This listing may be given by the pointer field,
called Next. We use the variable START to point to the first patient.
Example:
Consider a linked list in memory where each node of the list contains a single
character. We can obtain the string as follows:
INFO LINK
START 6
O
T 5 0
11
X 10
N 3
I 4
E 7
For example:
The following procedure prints the information at each node of a linked list.
Procedure: PRINT (INFO, LINK, START)
1. Set PTR := START
2. Repeat Step 3 and 4 while PTR ≠ NULL:
3. Write: INFO[PTR].
4. Set PTR := LINK[PTR]. [Updates pointer]
[End of Step 2 loop]
5. Exit.
Binary Trees
A binary tree T is defined as a finite set of elements, called node, such that:
a. T is empty (called the null tree or empty tree) or
b. T contains a distinguished node R, called 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 T2 are called, respectively, the
left subtree and right subtree of R. if T1 is nonempty, then its root is called the left
successor of R similarly T2 is nonempty, then its root is called the right successor of
R.
The following diagram represents a binary tree T as follows
i. T consists of 11 nodes, represented by the letters A through L, excluding L.
ii. The root of T is the node A at the top of the diagram.
iii. A left downward slantes line fro a node N indicates a left successor of N, and
a right downward slanted line from N indicates a right successor of N.
Observe that:
a. B is a left successor and C is right successor of the node A.
b. The subtree of the root A consists of the nodes B, D, E and F, and the
right subtree of A consists the nodes C, G, H, J, k and L.
Any node N is a binary tree T has either 0, 1 or 2 successors. The nodes A, B, C and
H have two successors, the nodes E and J have only one successor, and the nodes
D, F, G, L and K have no successors. The nodes with no successors are called
terminal nodes.
B C
D E G H
F J K
Example:
Consider any algebraic expression E involving only binary operations, such as
E = (a – b) / ((c *d)+e)
E can be represented by means of the binary tree T.
_ +
a b *
e
c d
Terminology:
Terminology describing family relationships is frequently used to describe
relationships between the nodes of a tree T. Suppose N is a node in T with left
successor S1 and right successor S2. Then N is called the parent of S1 and S2.
Analogously, S1 is called the left child of N and S2 is called the right child of N.
The terms descendant and ancestor have their usual meaning. That is, node L
is called a descendant of a node N if there is a succession of children from N to L.
Terminology from graph theory and horticulture is also used with a binary
tree T. Specifically, the line drawn from a node N of T to successor is called an edge,
and a sequence of consecutive edges is called a path. A terminal node is called a
leaf and a path ending in leaf is called a branch.