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

CSC 204 FULL NOTE - Data Structure

200 Level 2nd Semester Computer Science Note for all Nigerian Universities

Uploaded by

comradeonboard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

CSC 204 FULL NOTE - Data Structure

200 Level 2nd Semester Computer Science Note for all Nigerian Universities

Uploaded by

comradeonboard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

CPE 202 : Algorithms and Data Structures

Chapter 1
Introduction to Algorithm and Data structure
Introduction
A computer is a machine that manipulates information. The study of Computer Science includes the study of
how information is organized in a Computer, how it can be manipulated and how it can be utilized. Thus, it is
exceedingly important to understand the concept of information organization and manipulation.

Data and Information


Data are simply values or sets 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 which are not are called elementary items. For example,
a student name may be divided into three subitems - first name, middle name and last name. But the student
id would normally be treated as a single item.
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, the following are possible attributes and their
corresponding values for an entity, student of University of Maiduguri:
Attributes: Id No. Name Age Sex course
Values : 05/05/04/001 Abdulhadi Abubakar 24 M CPE202
Entities with similar attributes (e.g. all the students of Computer Engineering) form an entity set.
The term information is sometimes used for data with given attributes, or, in other words, meaningful or
processed data.
Raw data is of little value in its present form unless it is organized into a meaningful format. If we organize
data or process data so that it reflects some meaning then this meaningful or processed data is called
Information.
For Example: If we say '60 students' of course it is data but it carries no useful meaning. But if we add a little
and say 'There are 60 students in CPE 202' then this statement is meaningful and is called information.
Collection of data are frequently organized into a hierarchy of fields, records, and files. The way that data are
organized into the hierarchy of fields, records and files reflects the relationship between attributes, entities and
entity sets. That is a field is a single elementary unit of information representing an attribute of an entity, a
record is the collection of field values of a given entity and a file is the collection of records of the entities in
a given entity set.
Each record in a file may contain many field items, but the value in a certain field may uniquely determine the
record in the file. Such a field is called primary key and the values in such a field are called keys or kay
values. For example in student file, Id No. is unique for all the students in the University of Maiduguri. Hence
Id No. can be considered as primary key and the value 05/05/04/001 is the kay value.

Data Structure
Data may be organized in many different ways; the logical or mathematical model of a particular organization
of data is called "Data Structure". The choice of a particular data model depends on two considerations:
It must be rich enough in structure to reflect the actual relationships of the data in the real world.
The structure should be simple enough that one can effectively process the data when necessary.
1- 1
CPE202 : Algorithms and Data Structures

Data Structure Operations


The particular data structure that one chooses for a given situation depends largely on the nature of specific
operations to be performed.
The following are the four major operations associated with any data structure:
i. Traversing : Accessing each record exactly once so that certain items in the record may be processed.
ii. 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.
iii. Inserting : Adding a new record to the structure.
iv. Deleting : Removing a record from the structure.

Student Activity 1
1. What is Data?
2. What is Information.
3. Explain entity, attribute and entity set with example.
4. Define field, record and file.
5. Identify the possible entities, attributes and entity sets in University of Maiduguri.
6. Define the term data structure.
7. What are the various data structure operations?

Algorithm
An algorithm is a finite list of well-defined steps for solving a particular problem. These instructions, when
executed in the specified order, solve a problem, which the algorithm intends to solve. The algorithm must be
expressed in a language that is understood by the problem solver.
You have learnt, in your school days, many such algorithms. Euclid’s algorithm, for instance, to compute
H.C.F of two given positive integers. The algorithm says: “Divide the greater of the two given numbers by the
other one. In next step divide the divisor of the previous step by the remainder. Repeat this until the remainder
becomes 0. The divisor at this step is the required H.C.F.”

Writing an algorithm (Elements of Program Style)


An algorithm has three execution-sequences:
1) Sequence Logic or Sequential Logic
2) Selection or Conditional Logic
3) Iteration or Repetitive Logic
In "Sequence Logic" execution the instructions are executed in the obvious linear sequence one by one. The
sequence may be presented explicitly, by means of numbered steps, or implicitly, by the order in which the
instructions are written.
"Selection Logic" employs a number of conditions, which lead to a selection of one out of several alternative
instructions.
"Iteration Logic" allows execution of one or more instructions repetitively, a specified number of times or
until some condition becomes true.
1- 2
CPE 202 : Algorithms and Data Structures

Any language may be employed to write an algorithm as long as the executor of the algorithm understands it.
We will, however, use the following convention in writing an algorithm:
1. An algorithm begins with a START instruction.
2. It ends with a STOP or END instruction.
3. One instruction is written on one line.
4. Each line is numbered sequentially for identification.
5. The instructions used are well understood by the problem-solver who will use it.
6. A container of a value (known as variable) is represented by any word that does not have language specific
meaning, e.g. A, B, and C etc.
7. A =: 6 or A  6 means: store 6 in the variable named A.
8. A=A+1 or A  A + 1 means: add 1 to the value contained in the variable A and store the result back into it.
Similarly, A = B * C or A  B * C would mean multiply values in B and C and store the result into
A.
9. Standard arithmetic and logical operators may be employed with their respective meanings as follows:
Arithmetical Operator Logical Operator
Add + Equal to =
Subtract - Not equal to <>
Multiply ¬ * Less than <
Divide / Greater than >
Remainder % Less than or equal to <=
Greater than or equal to >=

NOTE : Assignment operator  means store the right hand value into left hand variable.
: 19 % 5 = 4. A % B means the remainder left when B divides A.
: The arithmetic operator precedence (BODMAS) applies.

10. Input-Output instructions: Simple verbs like WRITE or PRINT is used to output value(s) to the user and
READ for reading input value(s) from the user.
WRITE A or PRINT A means: print the value stored in the variable A on the screen.
WRITE A , 3 or PRINT A, 3 means: print value stored in variable A and then print 3.
READ A means: take a value from the user and store this value in the variable A.
READ A, B means read two values from user, store first in variable A and second in variable B.
11. Conditional instruction: A condition is an expression that evaluates to either TRUE or FALSE. For
example, (A>5) is a condition expression. If the value stored in variable A is 3, then this condition is
FALSE (i.e. 3 > 5) and if it is 7 then the condition is TRUE (i.e. 7 > 5). An IF instruction is written as –
IF (<condition>) then <action>.
IF (A>4) THEN PRINT 5

1- 3
CPE202 : Algorithms and Data Structures

This instruction means, if the value contained in the variable A is more than 4, then print the value of A
on the screen. If not, do nothing. There is another form of IF instruction also.
IF (A>4) THEN PRINT 5 ELSE PRINT 10
The meaning of the above instruction is that if the value contained in the variable A is more than 4 then
print 5 on the screen otherwise print 10.
12. Loop instruction:
 A GOTO instruction takes the execution sequence control to the specified instruction No.
GOTO 200
The above instruction means that now on execute the instruction No. 200 onwards.
 A DO - WHILE instruction executes a given set of instructions repeatedly as long as the given
condition is true. Execution stops when the condition becomes false.
DO (STEP 2 TO 10) WHILE (A<5)
It means, execute step 2, step 3,…..step 10. Then check the condition (A<5). If the condition is true
then again execute step 2, step 3,…..step 10. Check the condition again. Repeat this process.
Execution stops when the condition (A<5) becomes false. The instructions enclosed within the loop
are executed once, at least.
 A WHILE – DO instruction executes a given set of instructions repeatedly as long as the given
condition is true. Execution stops when the condition becomes false.
WHILE (A<5) DO (STEP 2 TO 10)
It means, check the condition (A<5). If the condition is true then execute step 2, step 3,…..step 10.
Again check the condition. If the condition is true then again execute step 2, step 3,…..step 10.
Check the condition again. Repeat this process. Stop when the condition (A<5) becomes false.
 Loop instruction: REPEAT – UNTIL instruction executes a given set of instructions repeatedly until
the given condition becomes false.
REPEAT (STEP 2 TO 10) UNTIL (A<5)
It means, execute step 2, step 3,…..step 10. Then check the condition (A<5). If the condition is false
then again execute step 2, step 3,…..step 10. Check the condition again. Repeat this process.
Execution stops when the condition (A<5) becomes true.

Example

To print all even numbers from 2 to 16 on the screen. Assume that Print instruction prints the given value on
the screen. Use sequential logic.

1. START Begin the algorithm execution


2. PRINT 2 Print value 2
3. PRINT 4 Print value 4
4. PRINT 6 Print value 6
5. PRINT 8 Print value 8
6. PRINT 10 Print value 10
1- 4
CPE 202 : Algorithms and Data Structures

7. PRINT 12 Print value 12


8. PRINT 14 Print value 14
9. PRINT 16 Print value 16
10. STOP Terminate the algorithm
As, is evident, this sequential method of printing, is not very nice way of doing the job. If you have to print
values up to 200, you may have to write as many as 100 such lines. Let us see how this algorithm may be
improved using branching (If – then) and looping (go to) in the next example.
Another algorithm for the same problem using selection and iteration logic:

1. START Begin the algorithm execution


2. A1 Store 1 in the variable 1
3. IF (A % 2 = 0) THEN PRINT If (A%2 is 0 then A contains even number) is true then print
A the value stored in A, otherwise move to next line without
doing anything
4. AA+1 Add 1 to value A contains and store the result back into A
5. IF (A <= 16) THEN GOTO 3 If the value contained in A is smaller than or equal to 16 then
do not go to next line, go to line number 3 instead, otherwise
move to next line. In other words, repeat steps (3,4,5) as long
as value contained in A is less than or equal to 16
6. STOP Terminate the algorithm
Obviously, this algorithm is far better than the previous one. If you have to print the values up to 200, all you
have to do is to change the loop termination condition (A<=16) to (A<=200) instead of writing 100 lines of
code.
Another algorithm for the same problem using selection and iteration logic without checking for the condition
for A being even or odd:

1. START Begin the algorithm execution


2. A2 Store 2 in the variable A
3. PRINT A Print the value stored in A
4. AA+2 Add 2 to value A contains and store the result back into A
5. IF (A <= 16) THEN GOTO 3 If the value contained in A is smaller than or equal to 16 then
do not go to next line, go to line number 3 instead, otherwise
move to next line. In other words, repeat steps (3,4,5) as long
as value contained in A is less than or equal to 16
6. STOP Terminate the algorithm

The above algorithm may also be written using while-do looping logic as below:
1- 5
CPE202 : Algorithms and Data Structures

1. START Begin the algorithm execution


2. A2 Store 1 in the variable 1
3. WHILE (A <=16) DO Keep on executing steps 4 and 5 as long as condition (A <=
(STEPS 4 TO 5) 16) is true, i.e. as long as value stored in A is less than or
equal to 16, otherwise go to step next to the one specified in
the loop condition, i.e. No. 6
4. PRINT A Print the value stored in A
5. AA+2 Add 2 to value A contains and store the result back into A
6. STOP Terminate the algorithm

Student Activity 2
1. What is the output of the following algorithms:
(a)
1. START
2. A  30
3. AA+2
4. B2
5. AA+B
6. PRINT B
7. STOP
(b)
1. START
2. A3
3. A  A * 12
4. BA%7
5. AA+B
6. PRINT A, B
7. STOP

(c)
1. START
2. A1
3. BA+3
4. DO (STEP 5 TO 5) WHILE (B < 15)
5. B  (A * B + 3) – 2
6. PRINT B
7. STOP
(d)
1. START
2. A5
3. IF (A > 40) THEN GOTO 5
4. AA+3
5. GOTO 3
6. PRINT A
1- 6
CPE 202 : Algorithms and Data Structures

7. STOP
2. Write an algorithm to read three numbers from the user and to print the greatest of them.
3. Write an algorithm to read 5 numbers and to print their arithmetic mean.

Implementation of Algorithm
Starting from a problem to be solved, constructing an algorithm for the solution of the same and solving the
problem using the algorithm, is known as algorithm implementation. Algorithm implementation involves the
following different phases of activities:
Problem definition: Understanding the information given and what result is asked for.
Problem Analysis: To analyze the problem to understand and formulate a possible line of attack in order
to solve it.
Algorithm Design: Applying top-down or bottom-up approach and describing the algorithm.
Algorithm analysis: To evaluate the designed algorithm and if necessary design alternative algorithm to
suit the requirements better.
Execution: Algorithms, thus developed, may be employed by a person or a machine to solve the
problem.
As, stated earlier, the executor of the algorithm must understand the language in which the algorithm has been
expressed. In case, the person executing the algorithm understands Russian language only, the algorithm must
be translated in Russian, before that person may execute it.
In order that a computer may execute the algorithm, the algorithm must be translated into a language that
computers understand. An algorithm written in a computer language is known as a program. And using
an algorithm by translating it into a suitable computer language is known as its implementation. It involves,
among others, conversion of the data variables stated in the algorithm into the language specific data types,
selecting suitable data structures etc.
Developing an algorithm needs no knowledge of any computer language. But its implementation requires the
knowledge of the target computer language. Computer languages have different capabilities. Each computer
language has been designed to facilitate a particular type of algorithm implementation. Hence, COBOL is
more suited for business-related algorithms while FORTRAN for engineering-related algorithms. C is, in this
regard, a general-purpose language and is suitable for all types of algorithms.
Here is a small example of algorithm to print 10 numbers (1 through 10). The same has been implemented in
computer languages C.

Algorithm:
1. START
2. A1
3. DO (STEP 3 TO 5) WHILE (A < 11)
4. PRINT A
5. AA+1
6. STOP
Implementation in C language:
#include<stdioh>
main()
{
int a;
a=1;
while(a<11)
1- 7
CPE202 : Algorithms and Data Structures

{
printf(“%d”,a);
a++;
}
}
Note that the form and features of an algorithm is very much different from its implementation in a particular
language.
Note that the only language a digital computer understands is binary coded instructions. Even the above
implementation will not execute on a computer without further translation into binary (machine) code. This
translation is not done manually, however. There are programs available to do this job. These translation
programs are called compilers and interpreters.
Compilers and interpreters are programs that take a program written in a language as input and translate it into
machine language. Thus a program that translates a C program into machine code is called C compiler;
Therefore, to implement an algorithm on a computer you need to have a compiler for the language you have
chosen for writing the program for the algorithm.

Analysis and Efficiency of Algorithm (Complexity and Time-Space


Tradeoff)
The correctness of an algorithm in solving a problem is of paramount importance. An algorithm giving
incorrect solution is no better than no algorithm at all.
Besides, an algorithm may solve a problem but the method may not be cost effective. The cost that incurs in
executing an algorithm is in terms of how much space and how much time is required in its execution. Each
variable requires space and each instruction takes some time to execute.
Therefore, analysis of algorithms for their correctness and space and time requirements is a major task in
algorithm design. In order to compare correct algorithms these two bases of space and time are taken into
consideration.
To determine the correctness of an algorithm several methods are available.
To determine the execution time the following information is required:
 Time taken by the executor of the algorithm to read one instruction.
 Time taken to understand and interpret the instruction.
 Time taken to execute the instruction
The actual time is not easy to determine because each executor of the algorithm takes its own time. The time
taken to execute an instruction also depends on instructions themselves. Some instructions take more time to
execute than others. Another approach called Frequency Count method also exists. In this method number of
operations are counted. The actual time will be proportional to this count and can be computed by suitably
multiplying by some factor. The highest order of the frequency count variable in the total time expression is
known as Order of the complexity denoted by O(). Less the order of complexity, more efficient the algorithm
is.
The space requirement can be calculated by maximum number of variables used by the algorithm in some
arbitrary space unit for the purpose of comparison.
These measurements may be different in different situations, but in case, a computer executes the algorithm,
these values may be found out more accurately.
Consider the following examples:

1- 8
CPE 202 : Algorithms and Data Structures

Example

Consider the algorithm to print character “A” N square times:


1. START
2. I1
3. IF (I > N) THEN GOTO 11
4. J1
5. IF (J > N) THEN GOTO 9
6. PRINT “A”
7. JJ+1
8. GOTO 5
9. II+1
10. GOTO 3
11. STOP
Let us count the number of times operations are executed. Assume that START takes 1 unit of time,
assignment () takes one unit of time, comparison (>=) takes two units of time, arithmetic operation (+) takes
one unit of time, GOTO and STOP take one unit of time each. Let us compute the frequency count of this
algorithm.
1. START : 1 unit
2. I1 : 1 unit
3. IF (I > N) THEN GOTO 11 : (2 + 1 = 3) units
4. J1 : (1 * N) units
5. IF (J > N) THEN GOTO 9 : (2 + 1) * N units
6. XX+1 : (N * N) * 1 units
7. JJ+1 : (N * N) * 1 units
8. GOTO 5 : (1 * N) units
9. II+1 : (1 * N) units
10. GOTO 3 : (1 * N) units
11. STOP : 1 units
Total units of time taken = 1+1+3+N+3*N+N*N+N*N+N+N+N+1
= 5+7*N+2*N2
The order of complexity = highest order of the frequency count variable
= 2
= O(N2)

Example

Consider the algorithm along with their frequency counts:


1- 9
CPE202 : Algorithms and Data Structures

1. START : 1 unit
2. I1 : 1 unit
3. IF (I > N) THEN GOTO 15 : (2 + 1 = 3) units
4. J1 : (1 * N) units
5. IF (J > N) THEN GOTO 13 : (2 + 1) * N * N units
6. K1 : (N * N) units
7. IF (K > N) THEN GOTO 11 : (2 + 1) * N * N units
8. XX+1 : (N * N * N) units
9. KK+1 : (N * N * N) units
10. GOTO 7 : (N * N * N) units
11. JJ+1 : (N * N) * 1 units
12. GOTO 5 : (N * N) units
13. II+1 : (1 * N) units
14. GOTO 3 : (1 * N) units
15. STOP : 1 units
Frequency Count for Ist for Loop (Line Nos 3 – 14) = N units
Frequency Count for IInd for Loop (Line Nos 5 – 12) = N * N units
Frequency Count for IIIrd for Loop (Line Nos. 7 – 11) = N * N * N units
Order of complexity of the algorithm = 3 or O(N3)

Student Activity 3
1. Write an algorithm to find the Greatest Number from 10 Numbers. Find the order of complexity of
this algorithm.
2. Compute the order of complexity of the following algorithm.
a. START
b. I1
c. IF (I > N * N) THEN GOTO 11
d. J1
e. IF (J > N * N) THEN GOTO 9
f. XX+1
g. JJ+1
h. GOTO 5
i. II+1
j. GOTO 3
k. STOP
1 - 10
CPE 202 : Algorithms and Data Structures

3. Consider the following algorithm. It exchanges the values contained in the two variables A and B
using a third variable C.
a. START
b. CA
c. AB
d. BC
e. STOP
Re-write this algorithm using the two variables A and B alone.

Summary
The logical or mathematical model of a particular organization of data is called Data Structure.
 The major operations, associated with any Data Structure are Traversing, Searching, Inserting and Deleting.

Chapter 2
Arrays
Linear Data Structures
A "data structure" which displays the relationship of adjacency between elements is said to be "linear".
This type of data structure is used to represent one of the simplest and most commonly found data object
i.e. ordered or linear list.
Examples are Months of the Year
[January, February, March.............., December]
or the values in a card deck.
[2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace]
or the group of friends
[Abdul, Rahab, Yaqub Ahmad, Rafey, Maryam].

Operations on Linear Data Structures


There are a variety of operations that can be performed on these data structures. These operations include :
i) Traversal: Processing each element in the list.
ii) Searching: Finding the location of the element with a given value or the record with a given key.
iii) Insertion: Adding a new element to the list.
iv) Deletion: Removing an element from the list.
v) Sorting: Arranging the elements in some type of order.
vi) Merging: Combining two lists into a single list.
1 - 11
CPE202 : Algorithms and Data Structures

Student Activity 1
1. Define linear data structure with an example.
2. List out the various operations on linear data structures.

Linear Arrays or One Dimensional Arrays


The simplest data structure that makes use of computed addresses to locate its elements is the One
Dimensional Array. Normally a number of (contiguous) memory locations are sequentially allocated to the
Array. Assuming that each element requires one word of memory, an n element Array will occupy n
consecutive words in memory. Array size is always fixed; and hence requires a fixed number of memory
locations.
A one dimensional array is a list of finite number n of Homogeneous data elements (i.e. data elements of the
same type) 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 the length or size of the Array.
If not explicitly stated, we will assume the index set consists of the integers 1, 2, ...n. In general, the length
or the number of data elements of the Array can be obtained from the index set by the formula
Length = UB-LB+1 ________________________ (2.1)
where UB is the largest index, called the upper bound and LB is the smallest index, called the lower
bound, of the Array. Note that length = UB when LB=1.
The elements of an Array A may be denoted by the subscript notation
A1, A2, A3,………An
Or by the parentheses notation
A(1), A(2), A(3),…………, A(n)
Or by the bracket notation (used in C and Pascal)
A[1], A[2], A[3], ………… , A[n]

Representation of One-Dimensional Array in Memory


In general a linear Array A with a subscript lower bound of "one" can be represented pictorially as in Fig. 2.1
given below.

A[1] A[2] A[3] A[n]

1000 1002 1003 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


Figure 2.1

If w words are allocated for each element or node (i.e. size of data types of elements of Array is w), then
total memory space allocated to array is given by :
Memory Space Required = (UB-LB+1)* w ___________________ (2.2)

1 - 12
CPE 202 : Algorithms and Data Structures

If we want to calculate the memory space required for first K elements of an Array then slight
modification in formula (2.2) will be needed. i.e.
Space Required = (K-LB+1)* w _________________________(2.3)
If the Address of A[LB] is Base(A) then the Address of kth element of Array will be given by:
Address of A[K] = Base(A)+(K-LB)* w ________________________(2.4)

Example

Consider a One-Dimensional Array. It has 8 elements. Each element takes 4 bytes to store and Address of
A[1]=1000.
Calculate
i. Length of the array
ii. memory space required
iii. memory space required for first 5 elements
iv. the address of A[7]
Sol: .
i. From the formula 2.1 we have,
Length=UB-LB+1
In this problem LB=1
UB=8
Hence, length=UB-LB+1= 8-1+1= 8
ii. From the formula 2.2 we have,
Memory Space Required = (UB-LB+1)* w
In this problem w =4 bytes
Hence, Memory Space Required = (UB-LB+1)* w
= (8-1+1)*4=8*4=32 bytes
iii. From the formula 2.3 we have,
Space Required = (K-LB+1)* w
In this problem K=5
Hence, Space Required = (K-LB+1)* w = (5-1+1)*4= 5*4=20 bytes
iv. From the formula 2.4 we have,
Address of A[K] = Base(A)+(K-LB)* w
In this problem K=7
 Base(A) = 1000
LB = 1,

1 - 13
CPE202 : Algorithms and Data Structures

w=4
Hence, Address of A[7] = 1000+(7-1)*4 = 1024.

Example

Consider the linear Array A(5:50), B(-5:10) and C(18).


Suppose Base(A)=300 and w=4, Base(B)=150 and w=2, Base(C)=120 and w=1.
i. Find the number of elements in each array.
ii. Find the memory space required in each array.
iii. Find the address of A(9), B(4) and C(11).
Sol: .
i. From the formula 2.1 we have,
Length=UB-LB+1
In array A(5:50)
LB=5
UB=50
Hence, Length(A)=UB-LB+1= 50-5+1= 46
In array B(-5:10)
LB=-5
UB=10
Hence, Length(B)=UB-LB+1= 10-(-5)+1= 10+5+1=16
In array C(18)
LB=1
UB=18
Hence, Length(C)=UB-LB+1= 18-1+1= 18
ii. From the formula 2.2 we have,
Memory Space Required = (UB-LB+1)* w
In array A(5:50)
LB=5
UB=50
w=4
Hence, Memory Space Required(A) = (UB-LB+1)* w
= (50-5+1)*4=46*4=184
1 - 14
CPE 202 : Algorithms and Data Structures

In array B(-5:10)
LB=-5
UB=10
w=2
Hence, Memory Space Required(B) = (UB-LB+1)* w
= [10-(-5)+1]*2=16*2=32
In array C(18)
LB=1
UB=18
w=1
Hence, Memory Space Required(C) = (UB-LB+1)* w
= (18-1+1)*1=18
iii. From the formula 2.4 we have,
Address of A[K] = Base(A)+(K-LB)* w
In array A(5:50)
LB=5
Base(A)=300
w=4
K=9
Hence, Address of A[9] = 300+(9-5)*4 = 300+16=316.
In array B(-5:10)
LB=-5
Base(B)=150
w=2
K=4
Hence, Address of B[4] = 150+(4-(-5))*2 = 150+18=168.
In array C(18)
LB=1
Base(C)=120
w=1
K=11
Hence, Address of C[11] = 120+(11-1)*1 = 120+10=130.
Student Activity 2
1 - 15
CPE202 : Algorithms and Data Structures

1. Give an example representing the memory allocation in one-dimensional array.


2. Consider the linear Array X(-2:42), Y(7:22) and Z(12).
Suppose Base(X)=100 and w=2, Base(Y)=150 and w=3, Base(C)=170 and w=5.
i. Find the number of elements in each array.
ii. Find the memory space required in each array.
iii. Find the address of X(4), Y(10) and C(7).

Traversing Linear Arrays


Let A be a collection of data elements stored in the memory of the computer. Suppose we want to print
the contents of each element of A or suppose we want to count the number of elements of A with a given
property. This can be accomplished by traversing A, that is, by accessing and processing (frequently
called visiting) each element of A exactly once.

Following is the algorithm for traversing a linear array A.

Algorithm: (Traversing a linear array) Here A is a linear array with lower bound LB and upper bound
UB. This algorithm traverses A applying PROCESS to each element of A.

Step 1: START

Step 2: K=LB [Initialize counter]

Step 3: Repeat Steps 4 and 5 while K<= UB.

Step 4: Apply PROCESS to A[K]. [Visit element]

Step 5: K=K+1 [Increase counter]

Step 6: Exit

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 array A, and deleting refers to the operation of removing one of the
elements from array A.

Inserting an element at the end of the linear array can be easily done provided the memory space allocated
for the array is large enough to accommodate the additional element. On the other hand, suppose we need
to insert an element in the middle of the array. Then, on the average, half of the elements must be moved
downward to new locations to accommodate the new element and keep the order of the other elements.
Following is the algorithm of inserting an element in a linear array.

Algorithm: (Inserting into a linear array) Here A 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 A.

Step 1: START

Step 2: I=N [Initialize counter]

Step 3: Repeat Steps 4 and 5 while I >= K.

1 - 16
CPE 202 : Algorithms and Data Structures

Step 4: A[I+1]=A[I]. [Move Ith element downward]

Step 5: I=I-1 [decrease counter].

Step 6: A[K]=ITEM. [Insert element]

Step 7: N=N+1 [Reset N]

Step 8: Exit

Similarly, deleting an element at the end of an array presents no difficulties, but deleting an element
somewhere in the middle of the array would require that each subsequent element be moved one location
upward in order to fill up the array. Following is the algorithm of deleting an element from a linear array .

Algorithm: (Deleting from a linear array) Here A is a linear array with N elements and K is a positive
integer such that K<=N. This algorithm deletes the Kth element from A.

Step 1: START

Step 2: Repeat for I = K to N-1:

A[I]=A[I+1].

Step 3: N=N-1 [Reset N]

Step 4: Exit

Two Dimensional Arrays


Even though Multidimensional Arrays are provided as a standard data object in most of the high level
languages, it is interesting to see how they are represented in memory. Memory may be regarded as one
dimensional with words numbered from 1 to m. So we are concerned with representing n dimensional
Array in a one dimensional memory.

A two dimensional 'm x n' Array A is a collection of m.n data elements such that each element is specified
by a pair of integers (such as j, k), called subscripts, with property that

1jm and 1kn.

The element of A with first subscript j and second subscript k will be denoted by

AJ,K or A[J, K].

Two dimensional arrays are called matrices in mathematics and tables in Business Applications; hence
two-dimensional arrays are sometimes called matrix arrays.

There is a standard way of drawing a two-dimensional m× n array A where the elements of A form a
rectangular array with m rows and n columns and where the element A[J,K] appears in row J and column
K. Following figure shows the case where A has 3 rows and 4 columns.

Columns

1 - 17
CPE202 : Algorithms and Data Structures

1 2 3 4

1 A [1, 1] A [1, 2] A [1, 3] A [1, 4]

Rows 2 A [2, 1] A [2, 2] A [2, 3] A [2, 4]

3 A [3, 1] A [3, 2] A [3, 3] A [3, 4]

Two Dimensional 3x4 Array A

Suppose A is a two-dimensional m× n array. The first dimension of A contains the index set 1,…...,m,
with lower bound 1 and upper bound m; and the second dimension of A contains the index set 1,………,n,
with lower bound 1 and upper bound n. The length of a dimension is the number of integers in its index
set. The pair of lengths m× n is called the size of the array.

The length of a given dimension can be obtained from the formula:

Length = UB-LB+1 ________________________ (2.5)


Note: This formula is the same which was used for linear arrays.
Example:
Find the length of both dimensions of a two dimensional array N(4,6).
Sol: In this array index of dimensions are 1, Hence,
Length of first dimension=UB-LB+1=4-1+1=4
and length of second dimension=UB-LB+1=6-1+1=6.
Example:
Find the length of both dimensions of a two dimensional array Num(3:9,-2:7).
Sol: In the array Num(3:9,-2:7)
For first dimension LB=3, UB=9
Hence length of first dimension=UB-LB+1=9-3+1=7
For second dimension LB=-2, UB=7
Hence length of second dimension=UB-LB+1=7-(-2)+1=10

Representation of Two-Dimensional Array in Memory


Let A be a two-dimensional m× n array. Although A is pictured as a rectangular array of elements with m
rows and n columns, the array will be represented in memory by a block of m.n sequential memory locations.

Programming language stores the array in either of the two way :


i) Row Major Order (row by row)

ii) Column Major Order (Column by column)

1 - 18
CPE 202 : Algorithms and Data Structures

In Row Major Order elements of 1st Row are stored first in linear order and then comes elements of next
Row and so on. When Above Matrix is stored in memory using Row Major Order form then the
representation will be as shown below:

Row 1 Row 2 Row 3

A(1,1) A(1,2) A(1,3) A(1,4) A(2,1) A(2,2) A(2,3) A(2,4) A(3,1) A(3,2) A(3,3) A(3,4)

In Column Major Order elements of 1st column are stored first linearly and then comes elements of next
column and so on. When Above Matrix is stored in memory using Column Major Order form then the
representation will be as shown below:

Column 1 Column 2 Column 3 Column 4

A(1,1) A(2,1) A(3,1) A(1,2) A(2,2) A(3,2) A(1,3) A(2,3) A(3,3) A(1,4) A(2,4) A(3,4)

Number of elements in Any 2 Dimensional Array can be given by :


No. of elements = (UB1-LB1+1) * (UB2-LB2+1)_______________ (2.6)
where, UB1 is upper bound of 1st dimension
LB1 is lower bound of 1st dimension
UB2 is upper bound of 2nd dimension
LB2 is lower bound of 2nddimension

If we want to calculate the number of elements till Ist Row then.


No. of elements = (UB2-LB2+1) * (1-1+1)
Or No. of elements = UB2-LB2+1 _______________________(2.7)
No. of elements in j Rows = j (UB2-LB2+1).
If w be the size of data types of Array elements then Memory space required for storing i Rows will be.
Space Required = (UB2-LB2+1) (i)*w _______________________(2.8)

A similar situation also holds for any two-dimensional array A. That is the computer keeps track of
Base(A) – the address of the first element A(1,1) of A and computes the address of A[i, j] using the
formula
Address of A(i, j) = Base(A) +[(UB2-LB2+1) (i-1) + (j-1)]*w ___________(2.9)
This is Address Scheme for Row Major order.
For Column Major order

1 - 19
CPE202 : Algorithms and Data Structures

Address of A(i, j) = Base(A) + [(UB1-LB1+1) (j-1) + (i-1)]*w__________(2.10)

Example

Suppose two dimensional array A is declared using


A(3, 20)
i. Find the length of each dimension of A.
ii. Find the number of elements in A.
iii. Assume Base(A)=200, w=4 words per memory location and array is stored in row major order then
find space required and the address of A(4,9).
iv. Assume Base(A)=200, w=4 words per memory location and array is stored in column major order
then find the address of A(4,9).
Sol:
i. In array A(3,20)
For first dimension LB1= 1, UB1=3
Hence length of first dimension=UB1-LB1+1=3-1+1=3
For second dimension LB2=1, UB2=20
Hence length of Second dimension=UB2-LB2+1=20-1+1=20
ii. Number of elements in A= Length of first dimension * Length of second dimension
= 3*20 = 60
iii. Base(A)=200, w=4, array is stored in row major order.
LB2=1, UB2=20
Number of rows i= rows from 1 to 3 = 3.
Space Required = (UB2-LB2+1) (i)*w = (20-1+1)(3)*4=20*3*4= 240
Addres of A(i,j) = Base(A) +[(UB2-LB2+1) (i-1) + (j-1)]*w
Hence Addres of A(4,9) = Base(A) +[(UB2-LB2+1) (4-1) + (9-1)]*w
= 200+[(20-1+1)(3)+(8)]*4=200+[68]*4=472.
iv. Base(A)=200, w=4, array is stored in column major order.
LB1= 1, UB1=3
Addres of A(i,j) = Base(A) + [(UB1-LB1+1) (j-1) + (i-1)]*w
Hence Addres of A(4,9) = Base(A) + [(UB1-LB1+1) (9-1) + (4-1)]*w
= 200+[(3-1+1)(8)+(3)]*4=200+[27]*4=308.

Example

Suppose two dimensional arrays A and B are declared using


A(-2:2, 2:22) and B(1:8, -5:5)
1 - 20
CPE 202 : Algorithms and Data Structures

i. Find the length of each dimension of A and B.


ii. Find the number of elements in A and B.
iii. Assume Base(A)=200, w=4 words per memory location and array is stored in row major order then
find space required and the address of A(2,5).
iv. Assume Base(B)=120, w=2 words per memory location and array is stored in column major order
then find space required and the address of B(6,2).
Sol:
i. In array A(-2:2, 2:22)
For first dimension LB1= -2, UB1=2
Hence length of first dimension=UB1-LB1+1=2-(-2)+1=5
For second dimension LB2=2, UB2=22
Hence length of Second dimension=UB2-LB2+1=22-2+1=21

In array B(1:8, -5:5)


For first dimension LB1= 1, UB1=8
Hence length of first dimension=UB1-LB1+1=8-1+1=8
For second dimension LB2= -5, UB2=5
Hence length of Second dimension=UB2-LB2+1=5-(-5)+1=11
ii. Number of elements in A= Length of first dimension * Length of second dimension
= 5*21 = 105
Number of elements in B= Length of first dimension * Length of second dimension
= 8*11 = 88
iii. Base(A)=200, w=4, array is stored in row major order.
LB2=2, UB2=22
Number of rows i= rows from -2 to 2 = 5.
Space Required = (UB2-LB2+1) (i)*w = (22-2+1)(5)*4=21*5*4= 420
Addres of A(i,j) = Base(A) +[(UB2-LB2+1) (i-1) + (j-1)]*w
Hence Addres of A(2,5) = Base(A) +[(UB2-LB2+1) (2-1) + (5-1)]*w
= 200+[(22-2+1)(1)+(4)]*4=200+[25]*4=300.
iv. Base(B)=120, w=2, array is stored in column major order.
LB2= -5, UB2= 5
LB1= 1, UB1=8
Number of rows i= rows from 1 to 8 = 8.
Space Required = (UB2-LB2+1) (i)*w = (5-(-5)+1)(8)*2=11*8*2= 176
1 - 21
CPE202 : Algorithms and Data Structures

Addres of B(i,j) = Base(B) + [(UB1-LB1+1) (j-1) + (i-1)]*w


Hence Addres of B(6,2) = Base(B) + [(UB1-LB1+1) (2-1) + (6-1)]*w
= 120+[(8-1+1)(1)+(5)]*2=120+[13]*4=172.

Student Activity 3
1. What are the two ways in which a programming language stores the array? Explain with an example.
2. Suppose two dimensional array X is declared using
X(-2, 24)
i. Find the length of each dimension of X.
ii. Find the number of elements in X.
iii. Assume Base(X)=150, w=2 words per memory location and array is stored in row major order then
find space required and the address of A(1,18).
iv. Assume Base(X)=200, w=3 words per memory location and array is stored in column major order
then find the address of A(1,20).
3. Suppose two dimensional arrays A and B are declared using
A(1:6, -3:22) and B(-4:8, -5:5)
i. Find the length of each dimension of A and B.
ii. Find the number of elements in A and B.
iii. Assume Base(A)=100, w=4 words per memory location and array is stored in row major order then
find space required and the address of A(3,7).
iv. Assume Base(B)=120, w=3 words per memory location and array is stored in column major order
then find space required and the address of B(7,2).

Summary
 Data Structure which displays the relationship of adjacency between elements is said to be
"Linear".
 Length finding, traversing from Left to Right, Retrieval of any element, storing any element,
Deleting any element are the main operations which can be performed on any linear Data Structure.
 Arrays are one of the Linear Data Structures.
 Single Dimension as well as Multidimension Arrays are represented in memory as one dimension
array.
 Elements of any Multidimensional Array can be stored in two forms Row major and column
major.

Self-assessment Questions
1. Consider the linear arrays X(-10:10), Y(1935:1985), Z(35).
(a) Find the number of elements in each array.

1 - 22
CPE 202 : Algorithms and Data Structures

(b) Suppose Base(Y) = 400 and w = 4 words per memory cell for Y. Find the address of Y(1942), Y(1977)
and Y(1988) and memory space required in array Y.
2. Consider the following two-dimensional arrays:
A(-5:5,3:33) and B(3:10, 1:15)
(a) Find the length of each dimension and the number of elements in A and B.
(b) Suppose Base(A) = 400 and there are w=4 words per memory location and array is stored in row major
order then find space required and the address of A(3,8).
(c) Assume Base(B)=120, w=8 words per memory location and array is stored in column major order
then find space required and the address of B(6,13).

Chapter 3
Stacks and Queues
Introduction`
There are certain frequent situations in computer science when one wants to restrict insertions and
deletions so that they can take place only at the beginning or at the end of the list, not in the middle. Two
of the data structures that are useful in such situations are stacks and Queues.

Stacks
Stack is an ordered list in which there are only one end, for both insertions and deletions. Elements are
inserted and deleted from the same end called top of the stack. Stack is called Last In First Out (LIFO)
list. Since the first element in the stack will be the last element out of the stack.
In particular, the elements are removed from a stack in the reverse order of that in which they were
inserted into the stack.
Basic operations associated with stacks are:
 Push: Insertion of any element is called "Push" operation.
 Pop: Deletion from the stack is called the "Pop" operation.
The most and least accessible elements in a stack are known as the "Top" and "Bottom" of the stack
respectively.
A common example of a stack phenomenon, which permits the selection of only its end element, is a pile
of trays in a cafeteria. Plates can be added to this pile only on the top and removed only from the top.

Example

Suppose following 6 digits are pushed, in order, onto an empty stack: 1, 2, 3, 4, 5, 6.


Following figure shows the three ways of picturing such a stack. When these elements will be popped
from stack the order will be: 6, 5, 4, 3, 2, 1.

2 1 - 23

4 6

5 5
CPE202 : Algorithms and Data Structures

TOP

TOP 1 2 3 4 5 6

TOP

Fig: Diagrams of Stack


The implication is that the right most element is the top element. Regardless of the way a stack is
described, its underlying property is that insertions and deletions can occur only at the top of the stack.
This means 5 can not be deleted before 6, 4 can not be deleted before 5 and 6 are deleted, and so on.
Consequently, the elements may be popped from the stack only in the reverse order of what in which they
were pushed onto the stack.

Representation of Stacks
Stacks may be represented in the computer in various ways, usually by means of one-way list or a linear
Array.
The location of the top element of the stack is stored in an integer variable TOP.
The condition : TOP = 0 or TOP = NULL will indicate that the stack is empty.
When we represent any stack through an array, we have to predefine the size of stack and we can not enter
more elements than that predefined size say MAX.
Whenever any element is added to the stack the value of TOP is increased by 1. This can be implemented
as :
TOP = TOP+1
and whenever any element is deleted from the stack the value of TOP is decreased by 1, this can be
implemented as :
TOP = TOP-1
The operation of adding (pushing) an item onto a stack and the operation of removing (popping) an item
from a stack may be implemented respectively by the following algorithms, called PUSH and POP. In
executing the procedure PUSH, we must first test whether there is room in the stack for the new item; if
not, then we have the condition known as overflow. Analogously, in execution the procedure POP, we
must first test whether there is an element in the stack to be deleted; if not, then we have the condition
known as underflow.
Algorithm: [This algorithm pushes an item onto a STACK.]
Step 1: START
Step 2: PUSH (STACK, ITEM)
Step 3: [stack already full?]

1 - 24
CPE 202 : Algorithms and Data Structures

If TOP = MAX, then Print: Overflow and goto Step 6.


Step 4: TOP = TOP + 1 [Increase TOP by 1]
Step 5: STACK [TOP] = ITEM [Insert ITEM in new TOP position]
Step 6: STOP

Algorithm: [This algorithm deletes the top element of STACK.]


Step 1: START
Step 2: POP (STACK, ITEM)
Step 3: [stack has an item to be removed?]
If TOP = 0, then Print: Underflow and goto Step 5.
Step 4: TOP = TOP - 1 [Decrease TOP by 1]
Step 5: STOP

Example

Consider the following stack of characters, where STACK is allocated N = 8 memory cells:
STACK: A C D F K
Describe the stack as the following operations take place:
a) POP
b) POP
c) PUSH (L)
d) PUSH (P)
e) POP
f) PUSH (R)
g) PUSH (S)
h) POP
Solution: The POP always deletes the top element from the stack, and the PUSH always adds the
elements to the top of the stack.
a) POP : A C D F

b) POP : A C D

c) PUSH (L) : A C D L

d) PUSH (P) : A C D L P
1 - 25
CPE202 : Algorithms and Data Structures

e) POP: A C D L

f) PUSH (R) : A C D L R

g) PUSH (S) : A C D L R S

h) POP: A C D L R

Student Activity 3.1


1. What are stacks?
2. With an example, explain the representation of stacks.
3. Consider the following stack, where STACK is allocated N = 10 memory cells:
STACK: X P Y 6 M B 8

Describe the stack as the following operations take place:


i) PUSH (T)
j) POP
k) PUSH (S)
l) POP
m) POP
n) PUSH (Q)
o) PUSH (9)

Queues
Queues arise quite naturally in the computer solution of many problems. Perhaps the most common
occurrence of a queue in Computer Applications is for the scheduling of jobs.
Queue is a Linear list which has two ends, one for insertion of elements called REAR and other for
deletion of elements called FRONT. Elements are inserted from Rear End and Deleted from Front End.
Queues are called First In First Out (FIFO) List, since the first element in a queue will be the first
element out of the queue. In other words, the order in which the elements enter a queue is the order in
which they leave.
Queues abound in everyday life. For example the people waiting in line at a bank form a queue, where the
first person in the line is the first person to be waited on.

1 - 26
CPE 202 : Algorithms and Data Structures

Representation of Queues
Queues may be represented in the computer in various ways, usually by means of one way lists or linear
Arrays. Each Queue will be maintained by a linear array queue [ ] and two integer variables FRONT AND
REAR containing the location of the front element of the queue and the location of the Rear element of
the queue. The condition FRONT=NULL will indicate that the queue is empty.
Following figure shows the three ways of picturing such a queue for the elements: 1, 2, 3, 4, 5, 6.

FRONT 1

2
6
3 REAR 1 2 3 4 5 6
5
4
4
5
3
REAR 6 FRONT REAR
2

1
FRONT
Fig: Diagrams of Queue

In executing the insertion operation, we must first test whether there is room in the queue for the new
item; if not, then we have the condition known as overflow. Analogously, in execution the deletion
operation, we must first test whether there is an element in the queue to be deleted; if not, then we have
the condition known as underflow.
Whenever an element is added to the queue, the value of REAR is decreased by 1. means
REAR = REAR + 1

Example

Consider the following queue, where QUEUE is allocated N = 8 memory cells:


STACK: FRONT=1 M O R D
REAR= 4

1 - 27
CPE202 : Algorithms and Data Structures

Describe the queue as the following operations take place:


a) F is added to the queue.
b) L, T are added to the queue.
c) Two elements are deleted.
d) X is added to the queue.
e) U is added to the queue.
f) One element is deleted.

Solution:
a) F is added to the queue: FRONT=1
M O R D F
REAR= 5
b) L, T are added to the queue: FRONT=1
M O R D F L T
REAR= 7
c) Two elements are deleted: FRONT=1
R D F L T
REAR= 5
d) X is added to the queue: FRONT=1
R D F L T X
REAR= 6
e) U is added to the queue: FRONT=1
R D F L T X U
REAR= 7
f) One element is deleted: FRONT=1
D F L T X U
REAR= 6

Student Activity 3.2


1. What is queue?
2. Diagramatically represent a Queue with an example.
3. Consider the following queue, where QUEUE is allocated N = 11 memory cells:
STACK: FRONT=1 9 10 5
REAR= 3
Describe the queue as the following operations take place:

1 - 28
CPE 202 : Algorithms and Data Structures

g) One element is deleted.


h) 18 is added to the queue.
i) 20 is added to the queue.
j) Two elements are deleted.
k) 12, 4, 23 are added to the queue.
l) Three elements are deleted.

m) Chapter 4
n) Linked List
o) Linked List
p) A linked 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.
q) The following 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 (e.g., name, address,….). 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 a special value, called the null pointer, which is any invalid address.
r) The linked list also contains a list pointer variable called START or NAME which contains the
address of the first node in the list. We need only this address in START to trace through the list.
s) Following figure is the schematic diagram of a linked list with 6 nodes.

t)
u) Example: A hospital ward contains 12 beds, of which 9 are occupied as shown in the following
figure.

1 - 29
CPE202 : Algorithms and Data Structures

v)
w)
x) Suppose we want an alphabetical listing of the patients. This listing may be given by the pointer
field, called Next in the figure. We use the variable START to point to the first patient. Hence
START contains 5, science the first patient, Abubakar occupies bed 5. Also, Abubakar pointer is
equal to 3, science Rahab the next patient, occupies bed 3; Rahab’s pointer is 11, science Ahmad,
the next patient, occupies bed 11; and so on. The entry for the last patient Zara contains the null
pointer, denoted by 0.
y)

z) Representation of Linked Lists in Memory


aa) Let LIST be a linked list. Then LIST will be maintained in memory as follows. First of all, LIST
requires two linear arrays – we will call them here INFO and LINK – such that INFO[K] contains
the information part and the LINK[K] contains next pointer field of a node of LIST. LIST also
requires a variable name – such as START – which contains the location of the beginning of the
list and a next pointer sentinel – denoted by NULL – which indicates the end of the list. We will
represent NULL by 0.
bb) The following example of linked lists indicate that the nodes of a list need not occupy adjacent
elements in the arrays INFO and LINK, and that more than one list may be maintained in the
same linear arrays INFO and LINK. However, each list must have its own pointer variable giving
the location of its first node.

1 - 30
CPE 202 : Algorithms and Data Structures

cc)
dd)
ee) We can obtain the actual list of characters as follows:
ff) START = 9, so INFO[9] = N is the first character.
gg) LINK[9] = 3, so INFO[3] = O is the second character.
hh) LINK[3] = 6, so INFO[6] = blank.
ii)LINK[6] = 11, so INFO[11] = E is the fourth character.
jj)LINK[11] = 7, so INFO[7] = X is the fifth character.
kk) LINK[7] = 10, so INFO[10] = I is the sixth character.
ll)LINK[10] = 4, so INFO[4] = T is the seventh character.
mm) LINK[4] = 0, the NULL value, so the list has ended.
nn)
oo)
pp) Example: Find the character strings stored in the following linked lists:
qq)

1 - 31
CPE202 : Algorithms and Data Structures

START

rr)
ss)
tt)Solution: We can obtain the actual string as follows:
uu) START = 5, so INFO[5] = Abuja is the first value.
vv) LINK[5] = 2, so INFO[2] = Delhi is the second value.
ww) LINK[2] = 4, so INFO[4] = Lagos is the third value.
xx) LINK[4] = 6, so INFO[6] = Lucknow is the fourth value.
yy) LINK[6] = 9, so INFO[9] = Deoria is the fifth value.
zz) LINK[9] = 10, so INFO[10] = Noida is the sixth character.
aaa) LINK[10] = 0, the NULL value, so the list has ended.
bbb) Hence the string is Abuja, Delhi, Lagos, Lucknow, Deoria, Noida.

ccc)
ddd) Student Activity 1
eee) 1. Define linked list with an example.
fff) 2. Find the character strings stored in the following linked lists:
ggg)
hhh)
iii)
jjj)
kkk)
lll)
mmm)

1 - 32
CPE 202 : Algorithms and Data Structures

nnn)

Chapter 5
Trees
Introduction
So far we have been studying mainly linear types of data structures such as Arrays, Lists, Stacks and Queues,
now we will study a Non Linear Data Structure called a Tree. This structure is mainly used to represent data
containing a hierarchical relationship between elements, familiar examples of such structure are: family trees,
the hierarchy of positions in an organization, an algebraic expression involving operations for which certain
rules of precedence are prescribed etc. For example suppose we wish to use a data structure to represent a
person and all of his or her descendants. Assume that the person's name is Alan and he has 3 children, John,
Joe and Johnson. Also suppose that joe has 3 children, Alec, Marc and Chris and Johnson has one child Peter.
We can represent Alan and his descendants quite naturally with the tree structure shown in Figure 5.1.

Figure 5.1

Basic Terminology about Trees


Definition of Tree: A tree is a finite set of one or more nodes such that there is a specially designated node
called the Root and remaining nodes are partitioned into n>0 disjoint sets S1....Sn where each of these sets is a
tree. S1...Sn are called the subtrees of the root. If we look at Fig 5.1 we see that the root of the tree is Alan .
Tree has three subtrees whose roots are Joe, John and Johnson.
1 - 33
CPE202 : Algorithms and Data Structures

The condition that S1....Sn be disjoint sets prohibits subtrees from ever connecting together. It means a tree
does not contain cycle.

Node (Vertex): A node stands for the item of information plus the branches to other items. Consider the Tree
of Fig 5.1 it has 8 nodes.

Edge: An edge is a line that connects two nodes. Consider the Tree of Fig 5.1 it has 7 nodes.

Degree: The number of edges connected to a given node is called its degree. In Fig 5.1 the degree of node
Alan is 3.

Leaf or Terminal Nodes: Nodes with no successors are called leaf or Terminal nodes. In Fig 5.1, John, Alec,
Marc, Chris and Peter are 'Leaf' nodes; other nodes of Tree are called 'NonLeaf' nodes.

Children: The roots of the subtrees of a node I are called the children of node I. I is the 'parent' of its children.

Siblings: Children of the same parent are called 'Siblings'. Alec, Marc, Chris are Siblings.

Level: The 'level' of a node is defined by initially letting the root be at level 1. If a node is at level l, then its
children are at level l+1.

Height or Depth : The height or depth of a Tree is defined as the maximum level of any node in the Tree.

Forest : A 'forest' is a set of n>0 disjoint trees.

Binary Trees
A Binary Tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The
first subset contains a single element called the Root of the tree. The other two subsets are themselves Binary
Trees, called the left and right subtrees of the original tree. A left or right subtree can be empty. Fig 5.2 shows
a typical Binary Tree. A node of a Binary Tree can have at most two Branches.

Figure 5.2 : A Binary Tree

If A is the root of a Binary Tree and B,C are the roots of its left and right subtrees respectively then A is said
to be the father of B, C and B, C are called Left and Right Sons respectively. If every Non Leaf node in a
Binary Tree has Non Empty Left and Right subtrees the tree is termed as Strictly Binary Tree. The Binary
Tree of Fig 5.2 is Strictly Binary Tree. A Strictly Binary Tree with n leaves always contains 2n-1 nodes.
A Complete Binary Tree is a Strictly Binary Tree of depth 'd' whose all leaves are at level d. Fig 5.3
represents complete Binary tree.

1 - 34
CPE 202 : Algorithms and Data Structures

Figure 5.3 : A Complete Binary Tree

Student Activity 1
1. What is a node?
2. What is the difference between leaf and children?
3. What is forest?
4. What is a binary tree?
5. What are the various forms of binary tree?
Theorem 1: The maximum number of nodes on Level i of a Binary Tree is 2i-1, i>1.

Binary Tree Traversal


The traversal of a Binary Tree is to visit each node in the tree exactly once. A full traversal produces a linear
order for the information in a tree. When traversing a binary tree we want to treat each node and its subtrees in
the same fashion. There are three standard ways of traversing a binary tree. These are called PREORDER,
INORDER and POSTORDER.

Preorder
To traverse a non empty tree in preorder we perform the following three operations:
i. Visit the root
ii. Traverse the left Subtree in Preorder
iii. Traverse the right Subtree in Preorder.

Inorder
i. Traverse the left Subtree in Inorder
ii. Visit the root
iii. Traverse the right Subtree in Inorder.

Postorder
i. Traverse the Left Subtree in Postorder.

1 - 35
CPE202 : Algorithms and Data Structures

ii. Traverse the right Subtree in Postorder.


iii. Visit the root.

Example 1

Consider the following Binary Tree

 In Preorder Traversal of the above Binary tree will be


ABDECFHIG
 In Inorder Traversal of the above Binary tree will be
DBEAHFICG
 In Postorder Traversal of the above Binary tree will be
DEBHIFGCA

Student Activity 2
1. Why is the traversal of binary tree required?
2. What are the various traversal orders of a tree ?

Binary Search Tree


A binary tree is called a binary search tree if all elements in the left subtree of a node N are less than the
contents of N, and all elements in the right subtree of N are greater than or equal to the contents of N.
Binary Tree with this property is called a Binary Search Tree.
Consider the following binary tree.

1 - 36
CPE 202 : Algorithms and Data Structures

The above tree is a binary search tree; that is every node exceeds every number in its left subtree and is
less than every number in its right subtree. Suppose the 23 were replaced by 35. Then the above binary
tree would still be a binary search tree. On the other hand; suppose the 23 were replaced by 40. Then the
above binary tree would not be a binary search tree, since the 38 would not be greater than the 40 in its
left subtree.

Searching and inserting in Binary Search Trees


Suppose T is a binary search tree. Suppose an ITEM of information is given. The following algorithm
finds the location of ITEM in the binary search tree T, or inserts ITEM as a new node in its appropriate
place in the tree.
a) Compare ITEM with the root node N of the tree
i. If ITEM < N, proceed to the left child of N.
ii. If ITEM > N, proceed to the right child of N.
b) Repeat Step (a) until one of the following occurs:
i. We met a node N such that ITEM = N. In this case the search is successful.
ii.We meet an empty subtree, which indicates that the search is unsuccessful, and we insert ITEM in
the place of the empty subtree.

Example 2

Consider the following Binary Search Tree. Suppose ITEM = 20 is given.

Simulating the above algorithm, we obtain the following steps:


1. Compare ITEM = 20 with the root, 38. Since 20<38, proceed to the left child of 38, which is 14.
2. Compare ITEM = 20 with 14. Since 20>14, proceed to the right child of 14, which is 23.
3. Compare ITEM = 20 with23. Since 20<23, proceed to the left child of 23, which is 18.
4. Compare ITEM = 20 with 18. Since 20>18, and 18 does not have a right child, insert 20 as the
right child of 18.
The following figure shows the new tree

1 - 37
CPE202 : Algorithms and Data Structures

Example 3

Suppose the following six numbers are inserted in order into an empty binary search tree:
40, 60, 50, 33, 55, 11
The following figure shows the six stages of the tree.

1 - 38
CPE 202 : Algorithms and Data Structures

Student Activity 3
1. Explain the similarity and difference between Binary tree and Binary search tree?
2. Suppose the following list of letters is inserted in order into an empty binary search tree:
J, R, D, G, T, E, M, H, P, A, F, Q
Find the final tree (show all steps) and find the inorder traversal of the resultant tree.

Summary
Tree is one of the most important Non linear Data Structures.
 Tree is used to represent hierarchical relationship between data items.
 Binary Tree is a tree in which each node has only two children i.e. left child and right child.
1 - 39
CPE202 : Algorithms and Data Structures

 Left child of any node is the root of left subtree of that node, similarly right child of any node is the root of
right subtree of that node.
 A Binary Search Tree T is a binary tree in which all identifiers in the left subtree of T are less than the
identifier in the root node and all identifiers in the right subtree of T are greater than the identifier in the root.
 A Binary Search Tree can be traversed in 3 ways:
i) Preorder Traversal in which root is traversed first and then left subtree and then Right subtree.
ii) Inorder Traversal in which left subtree is traversed first then root and then right subtree.
iii) Postorder Traversal in which left subtree is traversed first then right subtree and then root.

Chapter 6
Graphs
Definition and Terminology
A graph G consists of a non empty set V called the set of nodes (points, vertices) of the graph, a set E, which
is the set of edges of the graph and a mapping from the set of edges E to a pair of elements of V.
Any two nodes, which are connected by an edge in a graph are called "adjacent nodes".
In a graph G(V,E) an edge which is directed from one node to another is called a "directed edge", while an
edge which has no specific direction is called an "undirected edge". A graph in which every edge is directed
is called a "directed graph or digraph". A graph in which every edge is undirected is called an "undirected
graph".
If some of edges are directed and some are undirected in a graph then the graph is called a "mixed graph".
Let (V,E) be a graph and xE be a directed edge associated with the ordered pair of nodes (u,v), then the edge
is said to "initiating" or "originating" in the node u and "terminating" or "ending" in the node y. The nodes u
and v are also called "initial or terminal" nodes of the edge x. An edge xE which joins the nodes u and v,
whether it be directed or undirected, is said to be "incidents" to the node u and v. An edge of a graph which
joins a node to itself is called a "loop".
In some directed as well as undirected graphs we may have certain pairs of nodes joined by more than one
edge. Such edges are called "Parallel edges".
Any graph which contains some parallel edges is called a "multigraph".
If there is no more than one edge between a pair of nodes then, such a graph is called "simple graph."
A graph in which weights are assigned to every edge is called a "weighted graph".
In a graph, a node which is not adjacent to any other node is called "isolated node".
A graph containing only isolated nodes is called a "null graph". In a directed graph for any node v the
number of edges which have v as initial node is called the "outdegree" of the node v. The number of edges to
have v as their terminal node is called the "Indegree" of v and sum of outdegree and indegree of a node v is
called its total degree.
In the case of undirected graph the total degree of v is equal to the number of edges incident on v. The total
degree of a loop is 2 and that of an isolated node is 0.

1 - 40
CPE 202 : Algorithms and Data Structures

Any sequence of edges of a digraph such that the terminal node of the edge if any, appearing next in the
sequence defines path of the graph. A path is said to traverse through the nodes appearing in the sequence
originating in the initial node of the first edge and ending in the terminal node of the first edge and ending and
at the terminal node of the last edge in the sequence. The number of edges in the sequence of a path is called
the "length" of the path.
A path of a digraph in which the edges are distinct is called simple path (edge simple). A path in which all the
nodes through which traversing is done, are distinct is called "elementary path (node simple)".
A path which originates and ends in the same node is called "cycle (circuit)".

Definition and Terminology


A GRAPH G, consists of two sets V and E. V is a finite non-empty set of vertices. E is a set of pairs of
vertices, these pairs are called edges. V(G) and E(G) will represent the sets of vertices and edges of graph G.
In an undirected graph the pair of vertices representing any edge is unordered. Thus, the pair (V 1 , V2) and (V2,
V1) represent the same edge.
In a directed graph each edge is represented by a directed pair <V1, V2>, V1 is the tail and V2 the head of edge.
Thus <V2, V1> and <V1, V2> represent two different edges.

G1 G2
Figure 6.1 : Two Sample Graph

The graph G1 is undirected while G2 is a directed graph.


 V(G1) = {1, 2, 3, 4,} ; E(G1) = {(1, 2), (1, 3), (1, 4), (2, 3), (2, 4) (3, 4)}
 V(G2) = {1, 2, 3}; E(G2) = {<1, 2>, <2, 1>, <2, 3>}
The length of path is the number of edges on it. A simple path is a path in which all vertices except possibly
the first and the last are distinct.
e.g., Path 1, 2, 4, 3 and 1, 3, 4, 2 are both of length 3 in G1. The first is a simple path while the other is not.
A cycle is a simple path in which the first and last vertices are the same. In an undirected graph, G, two
vertices V1 and V2 are said to be connected if there is a path in G from V1 to V2. If the graph is undirected then
there must also be a path from V1 to V2.
An undirected graph is said to be connected if for every pair of distinct vertices Vi, Vj in V(G) there is a path
from Vi to Vj in G.
A tree is connected acyclic graph. A directed graph G is said to be strongly connected if for every pair of
distinct vertices Vi, Vj in V(G) there is a directed path from Vi to Vj and also from Vj to Vi.
The degree of a vertex is the number of edges incident to that vertex. In case G is a directed graph, in-degree
of vertex V is defined to be the number of edges for which V is the head. The outdegree is defined to be the
number of edges for which V is the tail. Directed graphs are also known as digraphs.

Student Activity 1
1 - 41
CPE202 : Algorithms and Data Structures

1. Define a graph.
2. Define its various terminologies.

Representation of Graphs
Two most commonly used representations are :
i) Adjacency matrix
ii) Path matrix

Adjacency Matrix
Let G = (V, E) be a graph with n vertices, n  1. The adjacency matrix of (G) is a 2-dimensional nxn array,
say A, with the property that A(ij) = 1 if the edge (V i, Vj) for a directed graph G is in E(G). The adjacency
matrix for an undirected graph is symmetric as the edge (Vi, Vj) is in E(G) if the edge (Vj, Vi) is also in E(G).
The adjacency matrix for a directed graph need not be symmetric.

Example 1

Let, V(G) ={v1, v2, v3, v4}


E(G) ={(v1 v2), (v2 v3), (v4 v1),(v4 v2), (v4 v3)}

Write the adjacency matrix of the above graph.

Solution:

The adjacency matrix of the above graph is

A=

Path Matrix
Let G be a simple directed graph with m nodes (vertices), V1, V2, V3, ……,Vm. The path matrix of G is the
m-square matrix Pij is 1 if there is a path from Vi to Vj otherwise Pij is 0.

1 - 42
CPE 202 : Algorithms and Data Structures

Example 2

Find the path matrix of the following graph:

Solution:

The path matrix of the above graph is

P=

Student Activity 2
1. Define adjacency and path matrix representation of a graph with example.
2. Find the adjacency and path matrix of the following Graph:

3. Draw the Directed Graph using the following adjacency matrix:

Summary

1 - 43
CPE202 : Algorithms and Data Structures

 A graph consists of two Non empty subsets E(G) and V(G), where V(G) is a set of vertices and E(G)
is a set of edges connecting those vertices.
 Graph is a superset of Tree. Every tree is a Graph but every graph is not necessarily a tree.
 A graph in which every edge is directed is called Directed Graph or Digraph A graph in which every
edge is undirected is called an undirected Graph.

1 - 44

You might also like