You are on page 1of 62

UNIT 1 i

Introduction to data structures and algorithms

Unit 1

TCC 236/05
Data Structures and
Algorithms

Introduction to
Data Structures and
Algorithms
ii WAWASAN OPEN UNIVERSITY
TCC 236/05 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

EXTERNAL COURSE ASSESSOR


Associate Professor Dr. Wong Yin Chai, Universiti Malaysia, Sarawak

PRODUCTION
In-house Editor: Ms. Marnisya Rahim
Graphic Designer: Ms. Audrey Yeong

Wawasan Open University is Malaysia’s 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 People’s 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.

© 2010 Wawasan Open University and


Yashwantrao Chavan Maharashtra Open University (YCMOU)

First revision 2013

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or
transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or
otherwise, without prior written permission from WOU.

Wawasan Open University


(KPT/JPT/DFT/US/P01)
Wholly owned by Wawasan Open University Sdn. Bhd. (700364-W)
54, Jalan Sultan Ahmad Shah, 10050 Penang.
Tel: (604) 2180333 Fax: (604) 2279214
Email: enquiry@wou.edu.my
Website: www.wou.edu.my
UNIT 1 iii
Introduction to data structures and algorithms

Contents
Unit 1 Introduction to Data
Structures and Algorithms
Course overview 1

Unit overview 3

Unit objectives 3

Unit introduction 4

1.1 Abstract data types  A brief description 7

Objectives 7

Introduction 7

Definition of ADT 9

Atomic and composite ADT 9

Operations defined on ADT 12

ADT list 13

Suggested answers to activities 16

1.2 General issues of ADT implementation 19

Objectives 19

Introduction 19

Techniques for representing defined ADT 19

Implementation of an ADT 20

Characteristics of a Java class 22


iv WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

1.3 An introduction to ADT implementation with 25


Java

Objectives 25

Introduction 25

Primitive data types 26


Logical level 26
Application level 26

Composite data types 27


The class type 27
Logical level 28
Application level 28
Ramifications of using references 31
Garbage 32
Comparing objects 33
Parameter passing 33

Interfaces 33
Logical level 33
Application level 34

Arrays 35
Logical level 35
Application level 36
Type hierarchies 37
Aggregate objects 37

Arrays of objects 38

Two-dimensional arrays 39

Suggested answers to activity 42

Summary of Unit 1 43

Unit practice exercises 45

Suggested answers to self-tests 47

Suggested answers to unit practice exercises 51

References 55

Glossary 57
UNIT 1 1
Introduction to data structures and algorithms

Course Overview

W elcome to TCC 236/05 Data Structures and Algorithms. In this course we


will discuss how to handle real-world data storage. In real life we may have
non-computer data storage such as a stack of index cards. These can be used to
hold a person’s name, ID card number, address and phone number. The result
of this compilation of data will produce an address book. You may have several
questions on how the data is stored in your computer memory, which method is
used to allow fast tracking for a specified card, and which method you should use
if there is a hundred, a thousand, or a million file cards. So you are now on the
right track to know how data structures are used to solve similar problems.

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 make sure you grasp the concepts of Unit 1 before commencing.

Once we have learnt how to create data structures and implement, we will be
looking into how the stored data can be retrieved efficiently. For this purpose
we will be introducing some common searching and sorting algorithms.

As there are many algorithms (a set of operations used to achieve an end objective)
which can be applied to a particular scenario, we will look at how to measure
the efficiency of an algorithm for identifying the best fit solution to particular
problem.

By the end of the course, you should be able to:

1. Design abstract data types and data sets to solve problems.

2. Analyse algorithm in term of space and time complexity.

3. Design and implement data structures such as List, Stacks and Queue in
Java.

4. Evaluate different techniques of sorting and select the proper techniques.

5. Compare and evaluate different searching algorithms.


2 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms
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.


4 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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 program’s 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

This is an extension of the concept of data type defined as

Data type = Permitted data values + Operations

Besides that, we have already learnt in TCC 121/05 Programming Fundamentals


with Java that simple data type can be used to build new scalar data types, and
there are standard data structures which are often used in their own right and
can form the basis for complex data structures. One such basic and popular data
structure is called array and it is the basic building block for more complex data
structures. Designing and using data structures are important programming
skills needed to produce efficient software. We are going to discuss various data
structures. We may classify these data structures as linear and non-linear data
structures. In linear data structure, the data items are arranged in a linear sequence
like in an array. In a non-linear data structure, the data items are not arranged in
sequence. An example of a non-linear data structure is a tree.
UNIT 1 5
Introduction to data structures and algorithms

B C

D E F G

H I

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.
6 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms
UNIT 1 7
Introduction to data structures and algorithms

1.1 Abstract Data Types  A Brief


Description
Objectives
By the end of this section, you should be able to:

1. Describe the benefits of using an abstract data type (ADT).

2. Explain the difference between data structure and ADT.

3. Discuss ADT operation and ADT list operation.

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

Figure 1.1 Create a model from a problem with abstraction


8 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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.

Let us look at each of these properties in the following subsections.

Atomic and composite ADT


A data type can be used to characterise and manipulate a certain variety of data. It
is formally defined by describing:

1. The collection of elements that it can represent.

2. The operations that may be performed on those elements.

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 person’s 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.
10 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

Primitive types Composite types

Figure 1.2 Primitive and composite data 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.

Unstructured composite types Structured composite types

Figure 1.3 Structured and unstructured composite data types

Composite data types come in two forms: unstructured and structured.


UNIT 1 11
Introduction to data structures and algorithms

Composite data type Characteristics

Structured A type where an organised collection of components in which the


organisation determines the means of accessing individual data
components or subsets of the collection. In addition to describing
their domain of values, primitive types are defined by describing
permitted operations.
Unstructured A type where a collection of components that are not organised.

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.

Java data types

primitive composite

integral floating point Boolean unstructured structured

byte char short int long float double class interface array

Figure 1.4 Java data types

Any data processed by a computer, whether it is primitive or composite, is just a


collection of bits that can be turned on or off. The computer itself needs to have
data in this form. Human beings however tend to think of information in terms of
larger units like numbers and lists, and thus we want at least the human readable
portions of our programs to refer to data in a way that makes sense to us. To
separate the computer’s view of data from our own, we use data abstraction to create
another view.

Activity 1.1

1. Describe the properties of ADT.

2. Compare data structure and ADT.


12 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

Operations defined on ADT


We refer to the set of all possible values (the domain) of an encapsulated data
“object,” plus the specifications of the operations that are provided to create and
manipulate the data, as an ADT.

Let us look at the operations Java provides for the encapsulated data type int.
You can create variables of type int using declarations in your program. Then you
can assign values to these integer variables by using the assignment operator and
perform arithmetic operations on them using +, −, •, /, and %. You have been
dealing with a logical data abstraction of integer since the very beginning.

The advantages of doing so are clear. You think of the data and the operations in a
logical sense and consider their use without having to worry about the implementation
details. The lower level data are still there. They are just hidden from you.

Remember that the goal in design is to reduce complexity through abstraction. We


extend this goal further: to protect our data abstraction through encapsulation.
The term encapsulation was covered in the course TCC 121/05 Programming
Fundamentals with Java. You will discover the concepts of encapsulation in detail
at the end of this section. There are different types of operations that can be dealt
with using a logical data abstraction of integers. Some examples of the operation
are shown in Table 1.1.

Operation Description

setzero( ) Sets the value of integer 0.


equals(integer,integer) Returns Boolean answer that is true if both values are
equal and returns false if both are not equal.
add(integer, integer) Adds two values of integer and returns integer value
after addition.
subtract(integer, integer) Subtracts two values of integer and returns integer
value after subtraction.
multiply(integer, integer) Multiplies two values of integer and returns integer
value after subtraction.
divide(integer, integer) Divides two values of integer and returns integer
value after subtraction.

Table 1.1 ADT operations for 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

To further elaborate the notation of an ADT, we can consider different types of


list, such as a list of groceries, a list of workers, a list of addresses and also a list
of students. Let us consider storing a list of students and perform the following
operations.

• If a new student has been admitted, then we have to add a record for that
student to the list.

• If a student cancels his admission then the system will delete the record of
that student from the list.

• Search for the name of a student by using his/her address.

• Find the number of students in the list.

• Perform update operation in cases where a student changes his/her address.

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 ADT’s 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.


14 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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( ) Creates an empty list.


isEmpty( ) Checks whether the list is empty.
Add(index,element) Adds the element at index position and shift the elements
from index position onwards by +1.
Delete(index) Removes the element which is at index position and shift
the elements from index +1 position onwards by −1.
Get(index) Retrieves the element at index position.
Size( ) Retrieves the number of elements in the list.

Table 1.2 ADT list operations

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

Each time a deletion operation is performed, the position number is decreased by


1 for the item after the deletion. For insertion operation in the list, the position
will be increased by 1.

If we want to get the size of the list, we perform this operation:

StudList.size ( )

and we get the size, which is 4.

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)
16 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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 ADT’s list and sorted list.

2. Design an ADT for point (Point is having x, y coordinates).

Suggested answers to activities

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.

2. • Abstract data type is a collection of data and a set of


operations on that data.

• A data structure is a construct within a programming


language that stores a data.
UNIT 1 17
Introduction to data structures and algorithms

Add
Data Delete
Program
structure Find
......

A program cannot access data directly. It can only access data


structures using ADT operations.

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
18 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms
UNIT 1 19
Introduction to data structures and algorithms

1.2 General Issues of ADT Implementation


Objectives
Upon completing this section, you should be able to:

1. Explain the general/common techniques for representing ADT.

2. Identify major issues in implementing ADT.

3. Relate the implementation issues with Java implementation.

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.

Techniques for representing defined ADT


There are several techniques for representing defined ADT. Here, we show what
you need to consider before implementing ADT. In representing defined ADT, the
main concern is the data representation. It must be able to represent all possible
values of ADT. The data members that we represent should be private, so that this
data can only be accessed by the class member itself. Besides, an algorithm must
be designed for each of the possible operations. The algorithm must be consistent
with the chosen representation. There should be auxiliary (helper) operations
which are called by the main operations and declared as private. These auxiliary
operations are not visible to users of the ADT.
20 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

To create an ADT, there are three steps to follow. The first step is to provide
an abstract description of the type’s 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.

2. Express the implementation as a complete class declaration.

3. Express the operation as an interface.

4. Express the implementation as a class that implements the interface.

The disadvantage of an interface is that you cannot describe constructors in an


interface. In the next subsection, you are going to learn the implementation of
ADT in detail.

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 ADT’s 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;
22 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

// 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.

Characteristics of a Java class


All the Java built-in data types are ADTs such as int to hold integer value. A
Java programmer can declare variables of those types without understanding the
underlying implementation. The programmer can initialise, modify and access the
information held by the variables using the operations provided. Before we discuss
further, let us consider the characteristics of Java class.

1. A class data member should be protected from careless or malicious use.

2. Class should be written for useful tasks and it should be reusable.

3. A Java class contains at least one constructor, which is an initialisation


method.

4. Java built-in classes are well designed. You should design your classes as
Java in building the classes.
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 student’s 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.
24 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

In modelling data in a program, we adapt many schemas. We must determine the


abstract properties of the data, choose the representation of the data, and develop
the operations that encapsulate this arrangement. During this process, we consider
data from three different perspectives, or levels:

Logical (or abstract) level An abstract view of the data values (the domain) and
the set of operations to manipulate them. The ADT is
defined at this level.
Application (or user) level A way of modelling real-life data in a specific con-
text which is also called the problem domain. At this
level, the application programmer uses the ADT to
solve a problem.
Implementation level A specific representation of the structure to hold
the data items, and the coding of the operations in
a programming language. This is how we actually
represent and manipulate the data in memory:
the underlying structure and the algorithms for
the operations that manipulate the items on the
structure. For the built-in data types, this level is
hidden from the programmer.

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

1.3 An Introduction to ADT


Implementation with Java
Objectives
Upon completing this section, you should be able to:

1. Illustrate the built-in data types in Java.

2. Explain the logical and application level of the Java built-in data types.

3. Demonstrate the effect of using references.

4. Describe several hierarchical types, including aggregate objects and


multidimensional arrays.

5. Use the Java Library classes such as string and array list.

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.
26 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

Primitive data types


Primitive data types of Java are Boolean. These are byte, char, double, float, int,
long, and short. These primitive types share similar properties. We will first look
closely at the int type from our two points of view, and then give a summary
of all the others. We understand that you are already familiar with the int type.
We are using this opportunity to show you how we apply the level of ADT
implementation to the built-in data types.

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 Java’s 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.

Composite data types


The class type

As stated in section 1.1 regarding the concept of composite ADT type, primitive
data types are the building blocks for composite types. A composite type gathers
a set of component values. If the composite type is a built-in type such as an
array, the accessing mechanism is provided in the syntax of the language. If the
composite type is a user-defined type, the accessing mechanism is built into the
methods provided with the class.

You are already familiar with the Java class construct from your previous course
TCC 121/05 Programming Fundamentals with Java. The class can be a mechanism
for creating composite data types. A specific class has a name and is composed
of named data fields (class and instance variables — sometimes called attributes)
and methods. The data elements and methods are also known as members of the
class. The members of a class can be accessed individually by name. A class is
unstructured because the meaning is not dependent on the order of the members
within the source code. That is, the order in which the members of the class are
listed can be changed without changing the function of the class.

In object-oriented programming, classes are usually defined to hold and hide data
and to provide operations on that data. In this case, we say that the programmer
has used the class construct to build his or her own ADT and this is the focus of
this unit. However, in this section on built-in data types, we use the class strictly
to hold data. We do not hide the data and do not define any methods for our
classes. The class variables are public, not private. We use a class strictly to provide
unstructured composite data collections. This type of construct has classically
been called a record. The record is not available in all programming languages.
Java classes provide the Java programmer with a record mechanism.

In the following discussion, to differentiate the simple use of class construct in


contrast with its later use in creating ADTs, we will use the generic term record
instead of class.
28 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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


static class point{ selector
int X; // Horizontal position of centre
int Y; // Vertical position of centre
}
public static void main (String [ ] args) {
point P1 = new point () ;
P1.X = 5; Component
P1.Y = 3; selector

System.out.println (“P1(x,y) :” + P1.X + P1.Y) ;


}
}

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

A constructor is used to initialise the data members of the class. A constructor is


a function used for initialisation. The main feature of the constructor is that it
has the same name as that of a class. If no constructor is defined, Java provides
a default constructor that initialises the constituent parts of the record to their
default values.

Primitive data types such as int,char,Boolean hold the value of it, as we have seen
in the previous section. In other words, primitive types such as int’s are handled
“by value”. Records or objects which are composite and not primitive data type
cannot be handled by value. The variable of a primitive type holds the value of the
variable, whereas a variable of type which is not of primitive type holds a reference
to the value of the variable. That is, the variable of record holds the address where
the system can find the value of the variable. Data types which are not primitive
are handled “by reference”.

In Java composite types are known as reference types. Understanding the ramifications
of handling variables by reference is very important, whether we are dealing with
records, other objects, or arrays. Using the following example, primitive data type
storage and record or object handling can be illustrated.

An example for primitive data types is as follows:

• We define two variables of int types A and B.

int A, B;
A = 10; B = 20;

Initial state

A 10

B 20

• If we perform operation as

A=B

• The content of memory will be

Initial state

A 10

B 20
30 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

The following is an example of object/record handling. When we define two objects


of point as P1 and P2,

point P1=new point();


point P2=new point();

this will create two reference variables P1 and P2, and these two reference variables
point to actual object contents.

P1

Object P1

P2

Object P2

When we assign object P2 to object P1 as P1 = 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

You can also use null in a comparison as follows:

if (P1 == null)
System.out.println(“The Point is not instantiated”) ;

Ramifications of using references

When you assign one object to another object as P1=P2, the result is that both
reference variables are referencing to the same object. This gives two names
for one object. In this case, we say that we have an “alias” of the object. Alias
is avoided by good programmers because the program will become difficult to
understand. An object’s state can change, even though it appears that the program
did not access the object when the object is accessed through the alias.

The following figure illustrates the ramification of using references.

Consider A and B as two integer variables and P1, P2 as reference variables for
two objects.

A 10

A == B evaluates to true

B 10

P1

Object P1
P1 == P2 evaluates to false

P2

Object P2

P1

Object P1
P1 == P2 evaluates to true

P2

Object P2

Figure 1.5 Ramification of using references


32 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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

Dynamic memory management deals with the allocation and de-allocation of


space in the memory. The objects are created and destroyed in the free pool of
memory in the dynamic memory management.

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 argument’s value is used) or
by reference (a copy of the argument’s 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 method’s 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 object’s 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.
34 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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();
}

Whenever an abstract method is to be declared in Java, a keyword abstract is to be


used. This is not the requirement of the interface.

The logical level deals with the domain of values of a data type and the available
operations to manipulate them. The domain of values for an interface consists of
classes. Classes implement interfaces. All the constants defined in the interface are
accessed by a class when it implements an interface.

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 Java’s 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
36 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

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];

If the component selector is used on the left-hand side of the assignment


statement, it is being used as a transformer which means that the storage structure
is changing. If the component selector is used on the right-hand side of the
assignment statement, it is being used as an observer. It returns the value stored
in a place in the array without changing it. Declaring an array and accessing
individual array elements are operations predefined in nearly all high-level
programming languages.

In addition to component selection, there is another “operation” available for


arrays. In Java, each array that is instantiated has a public instance variable, called
length, associated with it that contains the number of components in the array.
You can access the variable by using the same syntax you use to invoke object
methods, that is the name of the object followed by a period, followed by the
name of the instance. For example, the expression: numbers.length would have
the value 10.

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

Alternately, the brackets can go next to the name of the array:

int numbers[ ];

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;
}
38 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

Then, we could define a new circle class as:

public class NewCircle{


public Point location;
public float radius;
public boolean solid;
}

An object of class NewCircle has three instance variables, one of which is an


object of class Point, which in turn has two instance variables. An object like
NewCircle, made up of other objects is called an aggregate object. We call the
relationship between the classes NewCircle and Point a “has a” relationship, as
in “a NewCircle object has a Point object” as an instance variable.

When we instantiate and initialise an object of type NewCircle , we must


remember to also instantiate the composite Point object. For example, to create
a solid circle at position (55, 77) with a radius of 2.5, we will have to code:

NewCircle myNewCircle = new NewCircle();


myNewCircle.location = new Point();
myNewCircle.location.X -=55;
myNewCircle.location.Y = 77:
myNewCircle.radius -=2.5f;
myNewCircle.solid = true:

Although this is a syntactically correct approach to structuring data, the use of


composite objects in this fashion becomes tedious for the application programmer.
It is much easier if we define methods, such as a constructor method, to access
and manipulate our objects. This approach is what we took in the section on user-
defined types, when we moved from using classes as records to using classes to
create true ADTs.

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

Let’s 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];

Circles 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.

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 one-


dimensional array variable, except that there are two pairs of brackets.

A two-dimensional array object is instantiated in exactly the same way as a one-


dimensional 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]

To access an individual component of the temperature array, two expressions


(one for each dimension) are used to specify its position. We place each expression
in its own pair of brackets next to the name of the array as shown below.

T= temperature[5][6];
40 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

This gives value of 5th row and 6th col position.

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. No.
B. Yes, but only public methods.
C. Yes, but only private methods.
D. Yes, both public and private methods can be activated
within another method.

2. Can two different classes contain methods with the same


name?

A. No.
B. Yes, but only if the two classes have the same name.
C. Yes, but only if the main program does not create objects
of both kinds.
D. Yes, this is always allowed.

3. What is the primary purpose of a constructor?

A. To allow multiple classes to be used in a single program.


B. To copy an actual argument to a method’s parameter.
C. To initialise each object as it is declared.
D. To maintain a count of how many objects of a class have
been created.
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. x is set to refer to a copy of y’s object.


B. x is set to refer to the same object that y refers to.
C. Compiler error.
D. Run-time error.

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. x is set to refer to a copy of y’s object.


B. x is set to refer to the same object that y refers to.
C. Compiler error.
D. Run-time error.

6. Consider this class definition:

public class Quiz {


private double score;
public int f( )...
public static int g( )...
}

Where could the assignment score=1.0; appear for the private


instance variable score?

A. Both f and g can carry out the assignment.


B. f can carry out the assignment, but not g.
C. g can carry out the assignment, but not f.
D. Neither f nor g can carry out the assignment.

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.
42 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

8. Suppose that the FFT class has a typical equal’s 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.

9. What are the initial values in an array instantiated by using new?

10. Can you provide the initial values for an array?

Suggested answers to activity

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 implementation-
independent: 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 ADT’s 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.
44 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms
UNIT 1 45
Introduction to data structures and algorithms

Unit Practice Exercise


1. In a computer network, messages from one computer to another are generally
transmitted asynchronously. These messages therefore need to be buffered
until the receiving computer is ready for it. These communication buffers
make extensive use of Queues by storing these messages in a Queue. The
messages need to be sent to the receiving computer in the same order in which
they are created, i.e., FIFO order. Design an ADT for the communication
buffer Queue with the messages.

2. Design an ADT for SET.

3. Develop an interface from which all shapes descend. It should incorporate


all the methods and data that are common to all shapes. For example, you
should be able to reference the show and hide methods of any object shape,
even if you do not know the exact type of the object (Rectangle, Circle
Triangle).

4. Modify the following Date class so that it implements the Comparable


interface. The order of the objects of type Date should follow the natural and
chronological ordering.

class Date {
int month; // Month number in range 1 to 12.
int day; // Day number in range 1 to 31.
int year; // Year number.

Date(int m, int d, int y) {


month = m;
day = d;
year = y;
}
}

5. What are the three levels for data abstraction?


46 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms
UNIT 1 47
Introduction to data structures and algorithms

Suggested Answers to Self-tests

Feedback

Self-test 1.1

1. ADT sorted list

There is a requirement that the collection of data that is listed


should be sorted in ascending order by key.

For example, the list of students should be sorted by name, item


cost list may be sorted by cost of each item, etc.

The problem of maintaining sorted data requires inserting


record at the proper position.

ADT sorted list operations will be

SortedListCreate()
// This function creates a sorted list.

SortedListEmpty()
// This function checks if sorted list is empty
or not.

SortedListAdd(item)
// This function adds items at proper position and
the list remains
// sorted.

SortedListRemove(item)
// Removes item from sorted list.

SortedListSearch(item)
// Searches a list for a particular item, returns
true if found and
// returns false if not found.

SortedListGet(index)
// Gets the element at a particular index from the
sorted list.
48 WAWASAN OPEN UNIVERSITY
TCC 236/05 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.

ii. heading of constructor & methods.

b. Express the implementation as a complete class declaration.

c. Express the operation as an interface.

d. Express the implementation as a class that implements the


interface.

2. Logical (or abstract) level: An abstract view of the data values


(the domain) and the set of operations to manipulate them.
At this level, we define the ADT.
UNIT 1 49
Introduction to data structures and algorithms

Application (or user) level: A way of modelling real-life


data in a specific context; also called the problem domain.
Here the application programmer uses the ADT to solve a
problem.

Implementation level: A specific representation of the


structure to hold the data items, and the coding of the
operations in a programming language. This is how we
actually represent and manipulate the data in memory: the
underlying structure and the algorithms for the operations
that manipulate the items on the structure. For the built-in
data types, this level is hidden from the programmer.

Self-test 1.3

1. D

2. D

3. C

4. B

5. A

6. B

7. A

8. B

9. If the array components are primitive types, they are set to


their default value. If the array components are reference
types, the components are set to null.
50 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

10. An alternative way to create an array is with an initialise list.


For example, the following line of code declares, instantiates,
and initialises the array numbers:

int numbers[ ] = {5, 32, -23, 57, 10, 27, 13, 32, 32};
UNIT 1 51
Introduction to data structures and algorithms

Suggested Answers to Unit Practice


Exercise

Feedback

1. ADT for message buffer queue.

Pseudocode for the ADT queue operations.

Create()
// Creates an empty queue.

IsEmpty()
// Finds out whether the queue is empty.

enqueue(data)
// Add an item to the rear position of the queue.
It throws an exception
// if the data is not added.

dequeue()
// Removes the front element from the queue. It
throws an exception
// if the element is not removed.

2. SET, a collection of some elements # integers, float, or other


data type. Hence, we can decide what kinds of operation are
needed.

We need the following operations on set:

• create a new SET. This is done by having an operation


called newset.

• add elements to a set. Thus, we need an operation called


add.

• delete an element from a set. Thus, we need an operation


called delete.

• check to see if some given element is a member of a set.


Another operation called member must be used.

• verify if a set is empty. This requires the operation empty.


52 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

• count how many elements a set contains. The function


size can be used.

ADT operations are:

• delete(element)
// Deletes the value from the given set.

• 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.

3. Interface for Shape

interface Shape{
void getdata();
void displaydata();
double area();
double perimeter();
}
class Triangle implements Shape{
double side1;
double side2;
double side3;
Triangle(double x,double y,double z) {
side1=x;
side2=y;
side3=z;
}
public double area() {
double S;
S=(side1 + side2 + side3) / 2;
S=Math.sqrt(S*(S-side1)*(S-side2)*(Sside3));
return(S);
}

public double perimeter() {


return(side1+side2+side3);
}
UNIT 1 53
Introduction to data structures and algorithms

class Circle implements Shape{


double radius;
double area() {
}
}

4. Comparable interface for date

public class Date implements Comparable {


int month; // Month number in range 1 to 12.
int day; // Day number in range 1 to 31.
int year; // Year number.
Date(int m, int d, int y){
month = m;
day = d;
year = y;
}

public int compareTo(Object obj) {


if ((this.day == ((Date) obj).day) &&
(this.month == ((Date) obj).month) &&
(this.year == ((Date) obj).year))
return 0;
else
return 1;
}
}

5. Three levels for data abstraction are:

• Logical (or abstract) level: An abstract view of the data


values (the domain) and the set of operations to manipulate
them. At this level, we define the ADT.

• Application (or user) level: A way of modelling real-life


data in a specific context; also called the problem domain.
Here the application programmer uses the ADT to solve a
problem.

• Implementation level: A specific representation of the


structure to hold the data items, and the coding of the
operations in a programming language. This is how we
actually represent and manipulate the data in memory: the
underlying structure and the algorithms for the operations
that manipulate the items on the structure. For the built-in
data types, this level is hidden from the programmer.
54 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms
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 Practitioner’s Approach, 4th edn,


Boston: McGraw-Hill, 347–348.

Standish, T A (1994) Data Structures, Algorithms and Software Principles, Reading,


Mass: Addison-Wesley, 20 – 23.
56 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms
UNIT 1 57
Introduction to data structures and algorithms

Glossary
Abstract Data Type (ADT) A collection of data values together with a set
of operations on that data.

Abstract method A method declared in a class or an interface


without a method body.

Aliases Two names for the same object.

Application (or user) level ADT Here the application programmer uses the
ADT to solve a problem.

Atomic or primitive data type A data type whose elements are single, non-
decomposable data items.

Composite data type A data type whose elements are composed of


multiple data items.

Constructor An operation that initialises a new instance of


an object.

Data abstraction The separation of a data type’s logical properties


from its implementation that is a design
principle that separates the operations that
can be performed on a collection of data from
implementation of the operations.

Data encapsulation The separation of the representation of data


from the applications that use the data at
a logical level; a feature of programming
language that enforces information hiding.

Data structure A construct that is defined within programming


language to store a collection of data.

Data type A category of data characterised by the


supported elements of the category and the
supported operations on those elements. Simple
data type examples: integers, real numbers, and
characters.

Encapsulation Is the ability of an object to be a container


(or capsule) for related properties (ie. data
variables) and methods (ie. functions).
58 WAWASAN OPEN UNIVERSITY
TCC 236/05 Data Structures and Algorithms

Garbage Memory space that has been allocated to a


program but can no longer be accessed by a
program.

Garbage collection The process of finding all unreachable objects


and deallocating their storage space.

Implementation level ADT A specific representation of the structure to


hold the data items, and the coding of the
operations in a programming language.

Iterator An operation that allows us to process all the


components in a data structure sequentially.

Logical (or abstract) level ADT Abstract view of the data values (the domain)
and the set of operations to manipulate them.

Observer (sometimes called an An operation that allows us to observe the


accessor) state of one or more of the data values without
changing them.

Record A composite data type made up of a collection


of homogeneous elements called fields.

Reference types A ‘reference type’ is an object allocated in


main memory. Reference types include objects,
arrays and interfaces. They all inherit from the
class Object.

Transformers (sometimes called Operations that change the state of one or


mutators) more of the data values.

You might also like