Professional Documents
Culture Documents
Unit 1
TCC 236/05
Introduction to
Data Structures and
Algorithms
COURSE TEAM
Course Team Coordinator: Mr. Ishan Sudeera Abeywardena
Content Writers: Ms. Neeta Deshpande, Ms. Seema Gondhalekar, Dr. Lichade and Mr. Ishan Sudeera
Abeywardena
Instructional Designer: Ms. Marnisya Rahim
Academic Member: Mr. Vincent Chung Sheng Hung
COURSE COORDINATOR
Dr. Lim Ting Yee
PRODUCTION
In-house Editor: Ms. Marnisya Rahim
Graphic Designer: Ms. Audrey Yeong
Wawasan Open University is Malaysias first private not-for-profit tertiary institution dedicated to
adult learners. It is funded by the Wawasan Education Foundation, a tax-exempt entity established
by the Malaysian Peoples Movement Party (Gerakan) and supported by the Yeap Chor Ee Charitable
and Endowment Trusts, other charities, corporations, members of the public and occasional grants
from the Government of Malaysia.
The course material development of the university is funded by Yeap Chor Ee Charitable and
Endowment Trusts.
UNIT 1 iii
Introduction to data structures and algorithms
Contents
Unit 1 Introduction to Data
Structures and Algorithms
Course overview
Unit overview
Unit objectives
Unit introduction
Objectives
Introduction
Definition of ADT
12
ADT list
13
16
19
Objectives
19
Introduction
19
19
Implementation of an ADT
20
22
25
Objectives
25
Introduction
25
26
26
26
27
27
28
28
31
32
33
33
Interfaces
Logical level
Application level
33
33
34
Arrays
Logical level
Application level
Type hierarchies
Aggregate objects
35
35
36
37
37
Arrays of objects
38
Two-dimensional arrays
39
42
Summary of Unit 1
43
45
47
51
References
55
Glossary
57
UNIT 1 1
Introduction to data structures and algorithms
Course Overview
UNIT 1 3
Introduction to data structures and algorithms
Unit Overview
What is a data structure?
A data structure is an organised collection of data in a computer memory or
a disk file. The correct choice of data structure allows major improvements in
program efficiency. Data structures include linked lists, stacks, binary trees and
hash tables, among others. You will first be introduced to abstract data types. All
subsequent units depend on the foundation laid in Unit 1.
Therefore, study it thoroughly.
Data abstraction is required for modularity of the program and hiding data. Several
operations can be done using data. ADT (Abstract Data Type) is used to define
operations done on data. This unit will elaborate on how to design ADTs and the
advantages of ADT in general.
After specifying operations of an ADT, you can choose the data structure in
order to implement that ADT. This unit gives ideas about Java classes as a way
to implement an ADT.
Unit Objectives
By the end of Unit 1, you should be able to:
1. Explain what data structures are.
2. Discuss Abstract Data Types (ADT).
3. Explain what algorithms are.
4. Describe and apply recursion.
Unit Introduction
This is an introductory unit and it gives you an understanding of what a data
structure is. Knowledge of data structures is required for people who design
and develop computer programs of any kind: system software or application
software. As you have learnt in earlier courses, data are represented by data values
held temporarily within a programs data area or recorded permanently on a file.
Often the different data values are related to each other. When we talk about the
function of a program, we usually use words like add, read, multiply, write, do,
and so on. The function of a program describes what it does in programming
language.
Humans have created many ways of encoding information for analysis and
communication, for example letters, words, and numbers. In the context of a
programming language, the term data refers to the representation of such facts or
figures from the problem domain, by the data types available in the language.
These data values must be in an organised form so that programs make use of
these relationships. The organised collection of data and operations allowed in the
program is called a data structure. The programs have to follow certain rules to
access and process the structured data.
Data structure = Organised data + Allowed operations
UNIT 1 5
Introduction to data structures and algorithms
A binary tree
However, this is not the only way to classify data structures. Data structures may
also be classified as homogeneous and non-homogeneous data structures. An
array is a homogeneous structure in which all elements are of the same type. In a
non-homogeneous structure, the elements may or may not be of the same type.
Classes are common examples of a non-homogeneous data structure. Another
way of classifying data structures is to classify them as static or dynamic data
structures. Static structures are fixed at compile time, with sizes associated with
memory location. Dynamic structures can expand or shrink as required during
the program execution and when their associated memory locations change.
UNIT 1 7
Introduction to data structures and algorithms
Introduction
The term abstraction will make you think in terms of what you can do to a
collection rather than how you can do it. You learnt encapsulation (information
hiding) in your previous programming course. It is the same as it involves not
only hiding the details, but also restricting access to the details from outside of
the module. Abstraction allows one to collect instances of entities into groups in
which their common attributes need not be considered. There are two kinds of
abstractions in programming languages. They are data abstraction and process
abstraction. Data abstraction is a property that will help construct an isolated
structure from a given solution. (Refer to Figure 1.1). The concept of process
abstraction is one of the oldest. All subprograms are process abstractions because
they provide a way for a program to specify that some processes are to be done,
without providing the details of how it is to be done. Process abstraction is crucial
to the programming process. The ability to abstract away many of the details of
algorithms in subprograms makes it possible to construct, read, and understand
large programs. All subprograms, including concurrent subprograms, and
exception handlers, are process abstractions.
Abstraction
Problem
Model
All user-defined data types in Java are classes and all objects are allocated from
the heap and accessed through reference variables. The support for abstract data
types in Java can only be defined in classes. Java also includes packages as one of
its encapsulation constructs. The model defines an abstract view to the problem
and describes a subset of a system. For example, if we need to create an aircraft
system, we have different types of models: flight simulator and view model. This
implies that the model focuses only on problem-related stuff and that you should
try to define properties of the problem. These properties include data which are
affected and the operations which are identified by the problem.
As an example, consider the management of students in an educational institution.
The dean of the institute comes to you and asks you to create a program which will
allow you to manage student information. Well, this is not a very specific problem.
For example, what student information is needed by the management? What tasks
should be allowed?
Students are real persons who can be characterised with many properties such as:
Name.
Height.
Date of birth.
Weight.
ID number.
Address.
Sex.
Branch of study.
Certainly not all of these properties are necessary to solve the problem given by
the dean. Only some of the properties are problem specific. Consequently, you
create a model of a student for the problem. This model only implies properties
which are needed to fulfil the requirements of the dean, for instance name, date of
birth, identification number and branch of study. These properties are called the
data of the (student) model. Now you have described real persons with the help
of an abstract student.
Of course, the pure description is not enough. There must be some operations
defined with which the dean is able to handle the abstract students. For example,
there must be an operation which allows you to create a new student once a new
student enters the institution. Consequently, you have to identify the operations
which can be performed on an abstract student. You can also decide to allow access
to the students data only with associated operations. This allows you to ensure that
data elements are always in a proper state. For example, you are able to check if a
provided date is valid.
UNIT 1 9
Introduction to data structures and algorithms
This unit focuses on data and the language structures used to organise data. In
problem solving, the way you view the data of your problem domain and how you
structure the data that your programs manipulate will greatly influence your success.
Here, you learn how to deal with the complexity of your data by using abstraction
and how to use the Java language mechanisms that support data abstraction.
Definition of ADT
ADT is a collection of data and a set of operations defined by the programmer. It
may refer to an object class in object-oriented programming or to a special data
type created in traditional, non-OOP languages. ADT is not another name for
data structures. You should understand that a data structure is a construct within a
programming language that stores a collection of data.
An ADT is characterised by the following properties:
1. It exports a type.
2. It exports a set of operations. This set is called interface.
3. Operations of the interface are the only access mechanisms to the types
of data structures.
4. Axioms and preconditions define the application domain of the type.
Most programming languages provide simple data types for representing basic
information-types like integers, real numbers, and characters. For example, an
integer might represent a persons age; a real number might represent the amount
of money in a bank account. An integer data type in a programming language is
formally defined by listing the range of numbers it can represent and the standard
arithmetic operations it usually supports.
Primitive types
Composite types
The simple types of ADT are also called atomic types or primitive types, because
they can not be broken into parts. Programming languages usually provide ways
for a programmer to combine primitive types into more complex structures,
which can capture relationships among the individual data items. For example,
a programmer can combine two primitive integer values to represent a point in
the x-y plane or create a list of real numbers to represent the scores of a class of
students on an assignment. A data type composed of multiple elements is called
a composite type.
Just as primitive types are partially defined by describing their domain of values,
composite types are partially defined by the relationship between their element
values.
UNIT 1 11
Introduction to data structures and algorithms
Characteristics
Structured
Unstructured
With composite types, the main operation of interest is accessing the elements that
make up the collection.
The mechanisms for building composite types in the Java language are called
reference types. They include arrays and classes, which you are probably familiar
with, and interface. We will review all of these mechanisms in the next section.
primitive
integral
byte char
short
composite
floating point
int
long
float
double
Boolean
unstructured
class
interface
structured
array
Activity 1.1
1. Describe the properties of ADT.
2. Compare data structure and ADT.
Description
setzero( )
equals(integer,integer)
add(integer, integer)
subtract(integer, integer)
multiply(integer, integer)
divide(integer, integer)
Activity 1.2
Besides the operations that are given in Table 1.1, list three more
ADT operations on integers.
UNIT 1 13
Introduction to data structures and algorithms
All the operations above which are performed on the items form an ADT.
The encapsulation that we explained before is that you only specify the
behaviour of the ADTs operation on its data, that is, listing the item and not
the implementation of the data. When we create a list, we probably want to
access items anywhere in the list. We may also delete or insert an item in a list.
In the next subsection, we are going to look at ADT list operations and the
implementation in detail.
ADT list
The ADT list can consist of different types of lists such as in Table 1.2. You can
access the item anywhere on the list. The ADT list is simply an ordered collection
of items that you can refer to by position number.
Operations that are involved in ADT list are:
Create an empty list.
Check whether the list is empty.
Add an element in the list at a given position.
Remove the element from the list.
Retrieve the element at a particular position.
Retrieve the number of elements in the list.
We can write a pseudo code for the above ADT list operations. Table 1.2 shows
some of the functions that are used in ADT list operations.
Operation
Description
Create( )
isEmpty( )
Add(index,element)
Delete(index)
Get(index)
Size( )
By using the operations that are provided in Table 1.2, we can specify the operations
for the ADT list in detail. To get a clearer picture on how the operations work,
we will apply it to an example of a students list. Let us say we want to create
a list of students which consists of the following names:
Wern Eng, Stephen, Norleen, Rabikha, Sanjay
First, you should consider how to construct the list by using the operations of the
ADT list. You must begin by creating the list. We can model the student list as
an object called StudList. So, to create the empty student list, you need to use the
create() function and perform the operation as below:
StudList.Create()
Then, we can use a series of insertion operations to append the item to the list as
follows:
StudList.Add(1,Wern Eng)
StudList.Add(2,Stephen)
StudList.Add(3,Norleen)
StudList.Add(4,Rabikha)
StudList.Add(5,Sanjay)
We can delete the record of any of the students in the list by:
1. Identifying the index.
2. Using the Delete() operation.
UNIT 1 15
Introduction to data structures and algorithms
Let us say Norleen dropped out of the course. We will use the following statement:
StudList.Delete(3)
As Norleen was assigned as item number 3, we remove the item in 3. So, the list
will now look like this:
Wern Eng, Stephen, Rabikha, Sanjay
When we talk about ADT list, we only specify the effects of its operation without
indicating how to store the data. These clearly show us the concepts of the
encapsulation where the ADT specification does not include the implementation
issues. We will discuss the implementation on the ADT in the next section.
Activity 1.3
What is the result of student list from the following sequence of
ADT list operations?
StudList.createList()
StudList.add(1, Syitah)
StudList.add(1, Shah)
StudList.add(1, Sharon)
Summary
You have learnt abstract data type by defining appropriate data
types using specific operations and the combination of an atomic
data type and its associated methods. Although some notation
is unique to the standard, it is fairly intuitive. Indeed, it can be
useful in communicating a protocol in a verbal or written sense
between application developers. You also learnt that the language
provides a set of atomic and composite types. The composite types
are a collection of one or more atomic and/or composite types.
At the end of the section, you learnt ADT operation and ADT
list operation. In the next section, we are going to learn ADT
implementation.
Self-test 1.1
1. Write an ADT for sortedList from the list called aList by
using the operations of the ADTs list and sorted list.
2. Design an ADT for point (Point is having x, y coordinates).
Feedback
Activity 1.1
1. It exports a type.
It exports a set of operations.
Operations only access the types of data structure.
Axioms give the application of operations.
UNIT 1 17
Introduction to data structures and algorithms
Data
structure
Add
Delete
Find
......
Program
Activity 1.2
Lessthan (integer, integer)
// This function will return true if first
// value is less than second value else
// returns false
Greaterthan(integer,integer)
// This function will return true if first
// value is greater than second value else
// returns false
Mod(integer,integer)
// This function will give reminder after
// dividing first integer by second
// integer
isZero(integer)
// This function will check if the integer
// is zero or not & returns
// true if it is zero and false if
// it is not zero
Activity 1.3
Sharon
UNIT 1 19
Introduction to data structures and algorithms
Introduction
Data structures are responsible for modelling information within the context of
a program. There are many varieties of data structures, ranging from primitive
data types to arrays to more complex structures like hash tables. In this subunit,
you learn about the concept and construct of data structures provided by the Java
API. More specifically, you will learn how to use the data structures in the Java
utility package, also known as java.util. This package contains a wide range of
data structures applicable to many different programming scenarios. The goal of
this section is to familiarise you with these data structures and show you the basics
of how to use them. We use the Java class mechanism to encapsulate the data
structures you are studying, as ADTs, throughout the course.
To create an ADT, there are three steps to follow. The first step is to provide
an abstract description of the types properties and of the operations you can
perform on the type. The second step is to develop a programming interface
that implements the ADT. The last step is to write the code to implement the
interface. In implementing ADT you should follow some of the general steps
listed as follows:
1. Express the main ADT as an outline class declaration, showing only
a. public fields (refers to the attributes and method declared as public).
b. heading of constructor and methods.
Reminder
Recall the understanding of the keywords and concepts of the
private, public and the interface in TCC 121/05 Programming
Fundamentals with Java.
Implementation of an ADT
When we define an ADT, we concentrate on its operations but not on the
implementation, when in fact the implementation is also important. We should
know how to store ADT data and carry out all those operations on that data. You
should choose suitable data and functions to access it and perform operations
designed in ADT. After refinement of ADT, you can choose the data structure
which is available in the programming language you are using. Data structure and
the data should be hidden behind the operations.
In non-object-oriented programming, ADT operations and data structures are
separate, allowing other functions of the program to access the data directly and
data hiding is not done.
UNIT 1 21
Introduction to data structures and algorithms
Java provides object-oriented programming which hides the data from the
program behind the ADT operations. An example of the class is given below in
which the data members are integer values and the operations are:
1. Get a value.
2. Set the value.
3. Add the two values.
4. Check if value is zero.
5. Check if the two values are equal.
Although you have selected a data structure as an implementation for an ADT, the
remainder of the program should not depend on your particular choice. To access
the data structure, ADT operations must be used. You have to define the ADT as a
class to enforce the wall within Java. This class will hide the ADTs implementation
private from the program. The code below shows you an example of a value val that
is private so it is hidden from other parts of the program. It can only be accessed by
public methods of this class.
class Intclass {
// val is private so it is hidden from other parts of
// program
// val can be accessed by public methods of this class
private int val;
// such as setdata(), add(), equal(), isZero() etc
// constructor to initialise value 0
Intclass() {
val=0;
}
// method to set val with given value a
public void setdata(int a) {
val = a;
}
public boolean isZero( ) {
// method for checking if val is zero or not
if (val == 0) returns true;
return false;
}
// method for adding two objects
public add(Intclass obj){
val = val + obj.val;
}
// method for checking if both objects are equal
public boolean equals(Intclass obj){
if (val ==obj.val)
return equal;
else
return false;
}
}// end of Intclass
Reminder
Java is one of the programming languages that we can use to
implement an ADT. Other programming languages such as the
C and C++ are also commonly used for the implementation of
ADT. In this course, we will focus on Java as our implementation
tool.
UNIT 1 23
Introduction to data structures and algorithms
In addition to the built-in ADTs, Java programmers can use the Java class mechanism
to build their own ADTs. In the example of a student class list, a Java construct
describes students for the purpose of creating software for course evaluation
management system.
class student {
String Name;
String IDNo;
String Address;
String BranchOfStudy;
byte Age;
char Sex;
}
Yes, it is true that the programmers who created it need to know its underlying
implementation. For example, they need to know that class student is composed
of four String instance variables, one char and one byte instance variable, and they
need to know the names of the instance variables. The application programmers
who use the student class however do not need this information. They only need
to know how to create a student object and how to invoke the exported methods
to use the object.
An ADT specifies the logical properties of a data type. Its implementation provides
a specific representation such as a set of primitive variables, an array, or even
another ADT. A third view of a data type is how it is used in a program to solve
a particular problem: that is, its application. If we are writing a program to keep
track of the students marks, we would need a list of students and a way to record
the marks for each student. We may take a by-hand grade book and model it in
our program. The operations on the grade book may include adding a name,
adding a grade, averaging a students marks, and so on. Once we have written
a specification for our grade-book data type, we must choose an appropriate
data structure and design the algorithms to implement the operations on the
structure.
A way of modelling real-life data in a specific context which is also called the problem domain. At this
level, the application programmer uses the ADT to
solve a problem.
Implementation level
In the next section, you will learn how each of the ADT implementation is
represented in different perspectives.
Summary
At this stage you have learnt the common techniques for
representing ADT. We have also related the techniques by using
the implementation of an ADT. You have also reviewed the
characteristics of a Java class. At the later part of this section, you
learnt how we consider data from three different perspectives of
modelling data in a program. In the next section, you will learn
how different perspectives will affect different data types.
Self-test 1.2
1. List the general steps to implement ADT.
2. There are three different perspectives or levels when we
consider the data. Explain each one of them.
UNIT 1 25
Introduction to data structures and algorithms
Introduction
In this section, we will review all of the built-in data types. We will discuss
them from the view point of two of the levels defined in the previous section:
the logical (or abstract) level and the application level. We will not look at the
implementation level for the built in data types, since the Java environment hides
it and we, as programmers, do not need to understand this level. (However, note
that when we begin to build our own types and structures, the implementation
view becomes one of our major concerns). For the built-in data types, we can
interpret the two levels of ADT implementation as follows:
The logical or abstract level, which involves understanding the domain of
the data type and the operations that can be performed on data of that
type. For the composite data types, the main concern is how to access the
various components of the data type.
The application level, in other words, the view of how we use the data
types, including the rules for declaring and using variables of the type, in
addition to considerations of what data type we can use for modelling.
Logical level
In Java, variables of type int can hold an integer value between 2147483648
and 2147483647. Java provides the standard prefix operations of unary plus
(+) and unary minus (). The infix operations of addition (+), subtraction (),
multiplication (*), division (/), and modulus (%) are also included. We are sure
that you are familiar with all these operations.
Reminder
Note that integer division results in an integer, with no fractional
part.
Application level
We declare variables of type int by using the keyword int, followed by the name of
the variable, followed by a semicolon. For example,
int numData;
Variables of type int are handled within a program by value. This means
variable name represents the location in memory of the value of the variable. This
information may seem to belong to a subsection on implementation. However, it
does directly affect how we use the variables in our programs, which is of concern
at the application level. We will explore this topic more thoroughly when we learn
about Javas composite types, which are not handled by value.
UNIT 1 27
Introduction to data structures and algorithms
Reminder
Note that byte, short, and long types are also used to hold integer
values, char is used to store Unicode characters, float and double
are used to store real numbers, and the Boolean type represents
either true or false.
Logical level
A record is a composite data type made up of a finite collection of elements called
fields which are not necessarily homogeneous. Accessing is done directly through a
set of named members or field selectors.
We illustrate the syntax and semantics of the component selector within the
context of the following program:
public class changepoint{
Member
selector
int Y;
}
public static void main (String [ ] args) {
point P1 = new point () ;
P1.X = 5;
P1.Y = 3;
Component
selector
The above program declares a record structure called point. The main method
instantiates and initialises the fields of the point record P1. It then prints the
values of the x and y coordinates of the point.
The variables of the point record P1 comprises two components. They are the
coordinates X and Y respectively which are of integer data types. The syntax of
the component selector is the record variable name, followed by a period or dot,
followed by the member selector for the component.
For example in the above code, P1 is a component selector and X is a member
and can be accessed as P1.X.
Application level
The class is a composite item. The speciality of using it as a single item is by
referring to it by a single name. An object has a number of characteristics. Records
are used for object modelling purposes. Since the records associate various types
of data, they can be associated in the application level as a single data item. In
Java, the instance of record or object is created using the new command:
point P1 = new point ():
UNIT 1 29
Introduction to data structures and algorithms
Initial state
A
10
20
If we perform operation as
A=B
Initial state
A
10
20
P1
Object P1
P2
Object P2
P1
Object P1
P2
Object P2
Now the reference variable P1 is pointing to object P2. P1 does not become
a copy of P2. Instead, the reference associated with P1 becomes a copy of the
reference associated with P2. This means that both P1 and P2 now refer to the
same object.
The feature section below looks at the ramifications of using references from four
perspectives: aliases, garbage, comparison and parameters.
Java is having null value that indicates that there is no reference. If a reference
variable is declared without creating an instance of an object, it is automatically
initialised to the value null or you can also assign null to a variable, for example,
P1 = null ;
UNIT 1 31
Introduction to data structures and algorithms
P1
10
A == B
P2
evaluates to true
20
P1
Object P1
P1 == P2
evaluates to false
P1 == P2
evaluates to true
P2
Object P2
P1
Object P1
P2
Object P2
Garbage
When we create new objects using two reference variables for it, such as
point P1=new point();
point P2=new point();
after assignment statement P1 = P2,
P1 is referencing the same object.
P1
Object P1
P2
Object P2
What happens to the space being used by the object P1? After the assignment
statement, the program has lost its reference to the object P1, and so it can no
longer be accessed. Garbage is a memory space such as this, which has been
allocated to a program but can no longer be accessed by a program. There are
other ways that garbage can be created in a Java program. For example, the
following code would create 100 objects of class point; but only one of them can
be accessed through P1 after the loop has finished executing:
point P1 ;
for (i = 1 ; i <= 100 ; i++)
{
point P1 = new point ( );
}
The program cannot reach the other 99 objects. Those unreachable objects are
considered garbage.
Java runtime system marks the object when it is unreachable or when it is not
referenced as garbage.
The system of performing regular operations of checking garbage is called garbage
collection, in which it finds unreachable objects and de-allocates their storage
space, making it once again available in the free pool for the creation of new
objects.
UNIT 1 33
Introduction to data structures and algorithms
Comparing objects
Objects are handled by reference only. Hence, this affects the result returned
by the comparison operator to a large extent. Non-primitive data type variables
are considered to be identical only if they are aliases of each other. It is needed
whenever there is a need for comparison of the variables. So even if two
variables of type point have the same X and Y values, they are not considered
equal. Figure 1.5 shows the results of using the comparison opera tor in various
situations.
Parameter passing
When methods are invoked, they often pass information (arguments) through
their parameters. Some programming languages allow the programmer to control
whether arguments are passed by value (a copy of the arguments value is used) or
by reference (a copy of the arguments memory location is used). Java, however
does not allow such control. Whenever a variable is passed as an argument, the
value stored in that variable is copied into the methods corresponding parameter.
All Java arguments are passed by value. Therefore, if the variable is of a primitive
type, the actual value (int, double, and so on) is passed to the method; and if it
is a reference type, then the reference that it contains is passed to the method.
Notice that passing a reference variable as an argument causes the receiving
method to receive an alias of the object that is referenced by the variable. If it
uses the alias to make changes to the object, then when the method returns, an
access via the variable finds the object in its modified state.
Interfaces
The word interface means a common boundary shared by two interacting
systems. We use the term in many ways in computer science. For example, the
user interface of a program is the part of the program that interacts with the user,
and the interface of an objects method is its set of parameters and the return
value it provides. In Java, the word interface has a very specific meaning. In fact,
interface is a Java keyword. We look briefly at interfaces in this subsection.
Logical level
Java interface is similar to Java class as it includes data, that is, variable declarations
and methods. The variable declared in the interface must be static variables.
In the interface the methods are declared, but the body of the method is not
declared. Methods that are declared without bodies are called abstract methods.
Here is an example of an interface shape, with one constant, Pi, and two abstract
methods: Area and perimeter.
The area and the perimeter function may be different for different types of shapes
like squares, rectangles, triangles, circles, etc.
public interface shape
{
public static final float Pi = 3.14:
public abstract float Area ( ):
public abstract float perimeter();
}
Application level
In its most common form, an interface is a group of related methods. These
methods have empty bodies.
The following interface gives an example of a car.
Interface car{
Void change gear (int value);
Void increasespeed (int incrspeed);
Void setbrake (int decvalue);
}
The above interface can be implemented by changing the name of the class to
Newcar and the keyword implements have to be used in the declaration of the
class.
Class Newcar implements car {
}
UNIT 1 35
Introduction to data structures and algorithms
There is a contract between the class and the outside world that is formed by the
interface. If the class implements an interface, all the methods defined by the
interface appear in the source code before the successful compilation of the class.
The data of a specific type can be organised by designing an ADT. For example, an
ADT helps calculate the area, which in the generic case could be areas of a square,
a triangle, a rectangle, etc.
The interface of integer can be written as follows:
interface IntInterface {
public boolean isZero( );
public add(int a, int b);
public subtract(int a,int b);
public boolen equals(int a,int b);
}// end of interface IntInterface
Arrays
Classes provide programmers a way to collect several different attributes of an
entity and refer to those attributes by name. Many problems, however, have so
many components that it is difficult to process them because each one must have
a unique name. An array- the last of Javas built-in data types is the data type
that allows us to solve problems of this kind. We are sure that you have studied
and used arrays in your previous course. In general terminology, an array differs
from a class in three fundamental ways:
1. An array is a homogeneous structure (all components in the structure
are of the same data type) whereas classes are heterogeneous structures
(their components may be of different types).
2. A component of an array is accessed by its position in the structure,
whereas a component of a class is accessed by an identifier (the name).
3. Because array components are accessed by position, an array is a
structured composite data type.
Logical level
A one-dimensional array is a structured composite data type made up of a finite,
fixed-size collection of ordered homogeneous elements to which there is direct
access. Finite indicates that there is a last element. Fixed size means that the size
of the array must be known at compile time, but it does not mean that all of
the slots in the array must contain meaningful values. Ordered means that there
is a first element, a second element and so on. (It is the relative position of the
elements that is ordered, not necessarily the values stored there). Because all the
elements in an array must be of the same type, they are physically homogeneous;
that is, they are all of the same data type. In general, the array elements should be
logically homogeneous as well that is, for all of the elements to have the same
purpose. (If we keep a list of numbers in an array of integers, with the length of
the list an integer kept in the first array slot, the array elements would be
physically, but not logically, homogeneous).
The component selection mechanism of an array is direct access, which means
we can access any element directly, without first accessing the preceding
elements. The desired element is specified using an index, which gives its relative
position in the collection.
The semantics (meaning) of the component selector is Locate the element
associated with the index expression in the collection of elements identified by
the array name. Suppose, for example, we are using an array of integers, called
numbers, with 10 elements. The component selector can be used in two ways:
1. It can be used to specify a place into which a value is to be copied, such
as numbers[2] = 5;
2. It can be used to specify a place from which a value is to be retrieved,
such as value = numbers[4];
Application level
A one-dimensional array is the natural structure for the storage of lists. Some
examples are grocery lists, price lists, lists of phone numbers, and lists of student
records. You have probably used one-dimensional arrays in similar ways in some
of your programs.
The declaration of a one-dimensional array is similar to the declaration of a
simple variable (a variable of a primitive data type), with one exception. You must
indicate that it is an array by putting square brackets next to the type:
int [ ] numbers:
UNIT 1 37
Introduction to data structures and algorithms
Arrays are handled by reference, just like classes. This means that they need to
be treated carefully just like classes, in terms of aliases, comparison and their use
as parameters. And like classes, in addition to being declared, an array must be
instantiated. At instantiation you specify the size of the array:
numbers = new int[10];
As with objects, you can both declare and instantiate arrays with a single
command:
int numbers[ ] = new int[10];
If your program attempts to use an index that is not within the bounds of the
array, an ArraylndexoutofBoundsException is thrown.
Type hierarchies
In all of our examples of composite types, notably with records and arrays,
we have used composite types whose components have been primitive types.
We looked at a record Circle, that has four primitive-type fields, and an array,
numbers of the primitive int type. This approach is used to simplify the
discussion. It allowed us to concentrate on the structuring mechanism without
introducing unnecessary complications.
Aggregate objects
The instance variables of our objects can be a reference to objects. In fact, this
is a very common approach to the organisation of objects in our world. For
example, a page object might be part of a book object, which in turn is part of
a shelf. This would again be part of a library.
Consider the example from section 1.3 of a class modelling which shows a circle
that includes variable for horizontal and vertical positions. Instead of these two
instance variables, we could have defined a Point class to model a point in a
two-dimensional space, as follows.
public class Point{
public int X;
public int Y;
}
Activity 1.4
1. Define a class called library which comprises of the shelf
object and the book object.
Arrays of objects
Although arrays with atomic components are very common, many applications
require a collection of composite objects. For example, a business may need a list
of sales records or a teacher may need a list of students in a class. Arrays are ideal
for these applications. We simply define an array whose components are objects.
UNIT 1 39
Introduction to data structures and algorithms
Lets define an array of NewCircle objects. Declaring and creating the array of
objects is exactly like declaring and creating an array in which the components
are atomic types:
NewCircle Circles [ ] = new NewCircle[10];
is an array that can hold ten references to NewCircle objects. What are
the locations and radius of the circles? We do not know yet. The array of circles
has been instantiated, but the NewCircle objects themselves have not. Another
way of saying this is that the instance called Circles is an array of reference to
NewCircle objects, which are set to null when the array is instantiated. The
objects must be instantiated separately.
Circles
Keep in mind that an array name with no brackets is the array object. An array
name with brackets is a component. The component can be manipulated just like
any other variable of that type.
Two-dimensional arrays
A two-dimensional array is used to represent an item in a table with rows
and columns, provided each item in the table is of the same type or class. A
component in a two dimensional array is accessed by specifying the row and
column indexes of the item in the array. This is a familiar task. For example, if
you want to find a street on a map, you look up the street name on the back of
the map to find the coordinates of the street, usually a number and a letter. The
number specifies a row, and the letter specifies a column. You will find the street
where the row and column meet.
A two-dimensional array variable is declared in exactly the same way as a onedimensional array variable, except that there are two pairs of brackets.
A two-dimensional array object is instantiated in exactly the same way as a onedimensional array object except that sizes must be specified for two dimensions.
The following code fragment would create the array, where the data in the table are
floating-point numbers. The first dimension specifies the number of rows and the
second dimension specifies the number of columns.
double [ ] [ ] temperature;
temperature = new double [50] [7]
Summary
In this section you have learnt how we can interpret the two
levels: logical level and application level for the built-in data
types using different data types. We have also demonstrated
the effect of using references. In the next unit, we will look at
specific implementation using Java. You will learn how to analyse
the algorithm, be introduced to the tree graphs and so on in the
following units. Try the self test in this unit before you continue
to the next unit.
Self-test 1.3
1. Is it possible for a method of a class to activate another
method of the same class?
A.
B.
C.
D.
No.
Yes, but only public methods.
Yes, but only private methods.
Yes, both public and private methods can be activated
within another method.
UNIT 1 41
Introduction to data structures and algorithms
4. Suppose that the FFT class does not have a clone method.
What happens when an assignment x=y; is given for two
FFT objects?
A.
B.
C.
D.
5. Suppose that the FFT class has a clone method. What typically
happens when a call x= (FFT) y.clone( ); is given for two FFT
objects?
A.
B.
C.
D.
7. Suppose that the FFT class does not have an equal method.
What happens when an expression x==y; is evaluated for two
FFT objects?
A. The expression is true if x and y refer to the same object.
B. The expression is true if x and y refer to objects with the
same values.
C. Compiler error.
D. Run-time error.
8. Suppose that the FFT class has a typical equals method. What
happens when an expression x.equals(y); is evaluated for two
Foo objects?
A. The expression is true if x and y refer to the same object.
B. The expression is true if x and y refer to objects with the
same values.
C. Compiler error.
D. Run-time error.
Feedback
Activity 1.4
1. public class Book{
public int page;
public int title;
pulic int author;
}
public class Shelf{
public int row;
public int column;
pulic int section;
}
public class Library{
public Book bookinfo;
public Shelf shelfinfo;
}
UNIT 1 43
Introduction to data structures and algorithms
Summary of Unit 1
Summary
Software engineers construct code of programme based on
operations and mathematical model. Together these are known
as an ADT. For instance, our mathematical model may be a set
or a graph, and our operations may be to find the first element
or to list all the elements. The result of this phase is to express
the algorithm in a pseudo-language. This step is implementationindependent: an important feature if we need to organise big
software engineering projects. A person working on one ADT
need not know what others are doing with another ADT. In
an ADTs definition, there is nothing mentioned of how the
operations are implemented. ADTs are generalisations of primitive
operations. Programs that use them do not need to know which
implementation is used.
In this unit, you have learnt exactly what each data structure
provides in terms of modelling data in different ways. You then
moved on to the details of each ADT and how to use it to actually
model the data. Although this unit did not go into detail on the
use of ADT in your own programs, it did cover the basics of how
to use each of the ADT. The reality is that there are limitless uses
of the ADT in your own programs, so the best approach is to learn
how to use them in general terms and then apply them to your
own code as you see fit.
Well done! You have completed Unit 1. In the next unit, you will
be introduced to lists and list operations.
UNIT 1 45
Introduction to data structures and algorithms
UNIT 1 47
Introduction to data structures and algorithms
2. ADT Point
ADT point has x,y coordinates & ADT operations are
SetX(value)
// Set value for X
GetX()
// Return value of X
SetY(value)
// Set value for Y
GetY()
// Return value of Y
incrementX()
// Increment value of X
decrementX()
// Decrement value of X
Self-test 1.2
1. a. Express the main ADT as an outline class declaration,
showing only
i.
public fields.
UNIT 1 49
Introduction to data structures and algorithms
Self-test 1.3
1. D
2. D
3. C
4. B
5. A
6. B
7. A
8. B
UNIT 1 51
Introduction to data structures and algorithms
member(element)
// Checks whether the element is a member of a
given set or not.
isempty()
// Checks if set is empty or not.
size()
// Finds the number of elements in the set.
add(element)
// Adds an element in the set.
UNIT 1 53
Introduction to data structures and algorithms
}
class Circle implements Shape{
double radius;
double area() {
}
}
UNIT 1 55
Introduction to data structures and algorithms
References
Aho, A V (1983) Data Structures and Algorithms, Reading, Mass: Addison-Wesley,
10 13.
Dersham, H L and Jipping, M J (1995) Programming Languages: Structures and
Models, Boston: PWS Publishing, 175 185.
Friedman, D P, Wand, M and Haynes, C T (1982) Essentials of Programming
Languages, Cambridge, Mass: MIT Press, 88 98.
Friedman, L W (1991) Comparative Programming Languages, Generalizing the
Programming Function, New Jersey: Prentice-Hall, 7 9.
Ghezzi, C and Jazayeri, M (1987) Programming Language Concepts, 2nd edn, New
York: Wiley, 140 157.
Horowitz, E (1984) Fundamentals of Programming Languages, 2nd edn, Rockville,
MD: Computer Science Press, 256 260.
Nance, D W and Naps, T L (1995) Introduction to Computer Science: Problem Solving
and Data Structures, 3rd edn, St. Paul: West Publishing, 461 466, 848 853.
Pressman, R S (1997) Software Engineering, A Practitioners Approach, 4th edn,
Boston: McGraw-Hill, 347348.
Standish, T A (1994) Data Structures, Algorithms and Software Principles, Reading,
Mass: Addison-Wesley, 20 23.
UNIT 1 57
Introduction to data structures and algorithms
Glossary
Abstract Data Type (ADT)
Abstract method
Aliases
Constructor
Data abstraction
Data encapsulation
Data structure
Data type
Encapsulation
Garbage
Garbage collection
Iterator
Record
Reference types